Git config
This article covers:
Git config
I like to have a global git config which takes care of my usual git setup like typical commands and abbreviations I use, my username and my email address.
It can be helpful to adjust some of this information for a local project, e.g. when you are normally having your regular email address setup, but in one of the local folders you develop for a company you work for and you want to have your work email address instead.
Global git config
The global git config can be setup by adding a file to your home directory: ~/.gitconfig
.
This is usually the file where I input most of the information and for me it will look somewhat like this:
[user]
email = marc@my-email-provider.com
name = Marc Päpper
[alias]
unadd = reset HEAD
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
c = commit -m
co = checkout
coh = checkout HEAD
cob = checkout -b
b = branch
s = status
a = add
d = diff
df = diff --cached
pl = pull --rebase
ps = push
undo = "!f() { git reset --hard $(git rev-parse --abbrev-ref HEAD)@{${1-1}}; }; f"
[push]
default = current
[credential]
helper = store
I will break the different parts down next, but first let’s look at the local / per directory setup.
Local git config
How do you overwrite values from your global git config like your email address?
It’s quite simple: inside the folder of your project, you add / edit a file called .git/config
, so if your project is in /home/marc/project
, then this file would be at /home/marc/project/.git/config
.
Inside, you simply add the same section and change the values, e.g.:
...
[user]
email = marc@company.com
name = Marc Päpper
When you use git commit
or other commands in this repository, it will overwrite the global values, so you will commit as marc@company.com
now.
Aliases
As shown above, I specify several aliases in the global git config and in this section I will highlight why I do that and which ones I would recommend.
Abbreviations
Many of my aliases are actually just because I’m too lazy to type the full commands all the time as I use them so often during the day.
When working on a new task, I use git cob new-branch
to create a new branch and immediately check it out (shortcut for git checkout -b new-branch
).
While working on the chunk of work, I check the repository status with git s
(shortcut for git status
) and to see the changes I use git d
(short for git diff
).
When I am happy with changes, I add and commit them: git a x
(git add x
) followed by git c "My commit message"
(git commit -m "My commit message"
).
Then I push the branch git ps
(git push
).
Other abbreviations I often use are git pl
for git pull
, git df
to show the diff for already added files (git diff --cached
), git coh x
to overwrite changes of file x (git checkout HEAD x
) and git unadd x
when I accidentally added x, but don’t want to commit x (git reset HEAD x
).
Nice listing of history by tuning log
I have git lg
mapped to git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
which will show the history in a way that you can easily identify the different branches and how they were merged and you get a brief overview of your commits.
Undo in git
git undo
short for git !f() { git reset --hard $(git rev-parse --abbrev-ref HEAD)@{${1-1}}; }; f
I found this snippet some time ago (I think it was here: https://megakemp.com/2016/08/25/git-undo/) and it’s quite helpful if you want to undo/redo a commit. It basically will put the pointer to one commit before your most recent one, but if you run it again, then you are back where you started.
Push behavior
One more thing I like in my config is the default push behavior, so what happens when I run git ps
. I usually set this to default = current
which means that it should push to the remote branch which has the same name as the local one.
Credential store
One thing I’ve added recently is the credential store helper = store
which is needed as Github recently disabled the normal authentication, so you need to login with a cryptic password and the credential store caches this, so I don’t need to re-enter it.
comments powered by Disqus