Git Aliases
Once you start developing some experience with using git on the command line, you will sometimes find yourself typing some long commands over and over again. You can create aliases in git that are very similar to the bash alias shortcuts you use in the shell.
Steps to do this are highlighted in chapter 2.7 of the Git Book authored by Scott Chacon and Ben Straub and published by Apress.
One of the commands that I use a lot is:
$ git log --graph --decorate --pretty=oneline --abbrev-commit --all --date=local
which provides a nice colorized listing of the project’s activity and looks something like this:
* 2f6cc07 (HEAD -> automation, origin/master, origin/HEAD, master) Updated merge for README.md
|\
| * 84e20c2 Initial commit
* 8dc456f Completed migration for github site.
* bf67601 fixed head before sed
* 89780c3 Merge pull request #124 from thalios1973/feat/pandoc-test-fix
|\
| * c9a297c Added additional check that will allow the use of pandoc without the --strict flag or 'hsmarkdown' hack.
* | 025cf79 Merge pull request #125 from thalios1973/feat/body-end-file
|\ \
| |/
|/|
| * 272b1b6 Added ability to include custom code just before the </body> tag. body_end_file global config variable added.
|/
* 9f66ad0 README formatting
* 500253e Support for static, not managed by bashblog html files. Close #92
* 3c73ef6 Deleted the now defunct Twitter JSON API for share count. Fix #117
* b5a4590 bump version to 2.8
* 5c8e0e5 Revert changes in #116
* 5fc037f Merge branch 'master' of github.com:cfenollosa/bashblog
|\
| * c6a9bef Revert tag management from #116
| * 6222672 Better error message for $EDITOR. Close #112
| * 36d79b5 support Markdown.pl in bashblog folder. Close #113
| * 7154c07 Merge pull request #116 from McDutchie/master
| |\
| | * f50a17c tags_in_post(): bugfix for non-GNU 'sed'
| | * 2a29b22 Fix renaming using 'bb.sh edit -n'. Suppress 'which' errmsg.
| | * 62a26bb Merge remote-tracking branch 'upstream/master' Resolve minor editing 2.6-to-2.7 editing conflict in bb.sh
| | |\
| | * | 54cc0c8 More code refactoring. Limit word splitting and disable globbing by default.
| | * | d1a84d6 Merge remote-tracking branch 'upstream/master'
| | |\ \
| | * | | a674ec5 rebuild_tags(): use array for more robust file handling
| * | | | 2157b92 Slavic language support, thanks to Tomasz Jadowski
| | |_|/
| |/| |
but nicer than this website can depict (for now).
Rather than typing this long command every time, I just create an alias:
$ git config --global alias.lola 'log --graph --decorate --pretty=oneline --abbrev-commit --all --date=local'
Then all I need to type from now on is:
$ git lola
Setting Up Git to Ignore ELF Binaries (C/C++ Output on Linux)
The starting point for this experiment was from here
An Example using C Program Source
Let’s say we have a pre-existing directory of some C source code files:
$ ls
Makefile print print.c print.h print.o
And we initialize a new git repository in that directory:
$ git init
Initialized empty Git repository in /home/user/tmp/printer/.git/
$ cat .gitignore
# Ignore all
*
# Unignore all with extensions
!*.*
# Unignore all dirs
!*/
# Unignore make files
!Makefile
# Ignore .o files
*.o
# Ignore
bindir
bin/
# or
*/bin/*
Let’s see how we did:
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
Makefile
print.c
print.h
nothing added to commit but untracked files present (use "git add" to track)
So the print
and the print.o
files are not
showing up, which was our initial goal.
You may have to tweak the settings in the above
.gitignore
file to your own situation, but as you can see
it is possible to setup git to ignore the ELF binaries output
by gcc. I would probably also add a.out
to the
list of unignored files just to cover those times when you’re
not using a Makefile
. YMMV.
Convert a Git Repo to a Bare Repo
If you have a git repository that you haven’t cloned from a remote location, i.e., one that was created locally, it is easy to convert it to a bare repository. This process describes how to:
- Take a “normal” git repository
- Move the .git directory to another location
- Convert it to a bare repository
Suppose you have a git repository called repo. To convert it to a bare repository, execute the following commands:
cd repo
mv .git ../repo.git # renaming just for clarity
cd ..
rm -fr repo
cd repo.git
git config --bool core.bare true
Now you can clone it like a “normal” repository:
$ cd ..
$ git clone repo.git myrepo
$ cd myrepo/
$ ls
file-1.txt file-2.txt file-3.txt file-4.txt file-5.txt
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
$
Please note that repositories from a remote location do not have all of their git branch contents and history stored locally, so doing this to a repository cloned from a remote will result in a repository that will be missing data.
Git: Working with Git for the First Time
Here’s the first step every user should start with after downloading and installing git but BEFORE doing anything else: Set up your global environment variables! If you don’t do this git will abort your commit and give you an annoying message like this one:
$ git commit file.txt
Author identity unknown
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: empty ident name (for <user@localhost>) not allowed
You only should have to do the git config --global...
commands one time. If you want to use a different user ID and email
address in each repository then don’t include the --global
parameter and it will only update your current repository leaving your
global setting alone. If you’re contributing to different repositories
using different email addresses or IDs then you definitely want to get
into this habit.
Git: Committing to a Different Branch
Let’s imaging that you’ve made some changes to files in your work directory and then realized that you aren’t in the correct branch. Now you want to get the changes you’ve made to the right branch but no change the current branch. What do you do?
There is no git command to do this but you can use the git stash command to fix this.
This involves:
- Using the git stash command to temporarily store our changes elsewhere,
- Checkout the correct branch, and
- “Unstash” the changes to the correct branch.
Let’s say that we’ve made some changes in our repository’s new-release branch but they should have been made in the new-feature branch.
git stash
git checkout new-feature
git stash pop
We can now perform a commit, and update this branch.
git commit -a -m "Updated functions in new-feature branch."
Git: Deleting Multiple Commits
Let’s say that you want to delete the last 3 commits that you’ve
already pushed to the remote repository. In this example, you want
566dab6
to be the new HEAD revision.
$ git log --pretty=oneline --abbrev-commit
57bc36b (HEAD -> master, origin/master, origin/HEAD) 3nd set of bad entries on 6th commit
dfb4bd3 2nd set of bad entries on 5th commit
0fd1e16 First set of bad entries on 4th commit
566dab6 Yet more good entries on third commit
d50370a More good entries on second commit
b5fbc6d Good entries on first commit
2cad4c7 Initial commit
You can use git reset
, git revert
, or
git checkout
to achieve this goal.
Using reset:
$ git reset --hard HEAD~3
$ git log --pretty=oneline --abbrev-commit
566dab6 (HEAD -> master) Yet more good entries on third commit
d50370a More good entries on second commit
b5fbc6d Good entries on first commit
2cad4c7 Initial commit
Then you have to force push to the origin:
$ git push --force
Using revert:
$ git revert --no-commit 57bc36b
$ git revert --no-commit dfb4bd3
$ git revert --no-commit 0fd1e16
or do all 3 at once:
$ git revert --no-commit HEAD~3..
Then do the commit:
$ git commit -m "Fixed commits"
$ git log --pretty=oneline --abbrev-commit
0da0f42 (HEAD -> master) Fixed commits
57bc36b 3nd set of bad entries on 6th commit
dfb4bd3 2nd set of bad entries on 5th commit
0fd1e16 First set of bad entries on 4th commit
566dab6 Yet more good entries on third commit
d50370a More good entries on second commit
b5fbc6d Good entries on first commit
2cad4c7 Initial commit
Using checkout:
Checkout that revision over the top of local files
$ git checkout -f 566dab6 -- .
$ git commit -a
$ git log --pretty=oneline --abbrev-commit
0da0f42 (HEAD -> master) Fixed commits
57bc36b 3nd set of bad entries on 6th commit
dfb4bd3 2nd set of bad entries on 5th commit
0fd1e16 First set of bad entries on 4th commit
566dab6 Yet more good entries on third commit
d50370a More good entries on second commit
b5fbc6d Good entries on first commit
2cad4c7 Initial commit
Tags: cli, git, source-control, motd
Gitea Server Installation on a Raspberry Pi
Create a 'git' user:
$ sudo adduser git
Login as the 'git' user and download the gitea binary from the gitea website (choose the latest version):
wget -O gitea https://dl.gitea.io/gitea/1.14.5/gitea-1.14.5-linux-amd64
chmod +x gitea
Create a service. We're keeping it simple here so we don't need a lot of the database-specific stuff:
[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/home/git/
# If using Unix socket: tells systemd to create the /run/gitea folder, which will contain the gitea.sock file
# (manually creating /run/gitea doesn't work, because it would not persist across reboots)
#RuntimeDirectory=gitea
ExecStart=/home/git/gitea web --config /home/git/custom/conf/app.ini
Restart=always
Environment=USER=git HOME=/home/git GITEAWORKDIR=/home/git
[Install]
WantedBy=multi-user.target
Enable and start Gitea at boot:
sudo systemctl enable gitea
sudo systemctl start gitea
Tags: git, gitea, raspberry-pi, motd
git Tips
A useful tip to beautify your output is to use:
$ git log --graph --decorate --oneline --abbrev-commit --all --date=local
when looking at your log entries.
Or you can add
[alias]
lola = log --graph --decorate --pretty=oneline --abbrev-commit --all --date=local
to your ~/.gitconfig file to use $ git lola
as an alias.