Here is the list of my favorite git commands, I hope you will find them useful!
Set up your identity (per project)
This one could be used when for example you need to set your corporate credential instead of your default (
--global). You need to
cd into your local repository before executing these commands.
git config user.email "email@example.com" git config user.name "Kopiás Csaba"
Storing your passwords
git config --global credential.helper store
Warning: this stores your passwords in plain text. More on that here.
In memory, with longer than default timeout
git config --global credential.helper 'cache --timeout=9000'
I like to use this on servers, where I don’t want to store my passwords, but the default 15-minute timeout would annoy me too much. Learn more here.
Debugging (When did this break?!)
A very useful feature of git is the bisect command. It leverages the binary search algorithm to find the commit that introduced the break. This means that it will only jump as much as it is really needed between the commits.
At every jump, you need to specify if the app is still working or not. Based on this input it will quickly find where the bug was introduced. If my memory serves me well, it will check 150 commits in about 8 steps or so.
git bisect start git bisect good git bisect bad ... git bisect good # The commit that introduced the break is printed on the console
Probably we all have been there, we messed up our local repository so much, that we wanted a clean slate again. How to forget our past, and start over? Here is how.
This will reset your current state back to whatever branch you specify. You might want to stick to origin/*, as usually that is still good:)
Keep in mind, that this does not change your current branch. So you can be on your feature branch while resetting to origin/master, that is probably not what you want. Also, this does not remove untracked files.
But it will remove everything that you have been messing around locally with your rebase and every unpushed commits and so on. Learn more here.
git reset --hard origin/master
Change the last, unpushed commit
This will practically undo your last
git commit. All of the changes will be there for you that you’ve made in that commit as a local change.
git reset HEAD~
The commit message
This will bring up an editor, and you will be able to modify the commit message of your last commit.
git commit --amend
If your repository has any submodules, this will come in handy. When there is any change in a submodule (if it’s HEAD differs from the parent repo’s stored reference), you will see it in
git config status.submoduleSummary 1
Remove merged branches
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
Read more description of this at the source.
git log --graph --decorate --simplify-by-decoration --oneline --all
Add & commit in the same command
Create an alias with this command:
git config --global alias.add-commit '!git add -A && git commit'
And then you can use it just as you would the simple git commit. For example, you can add a message:
git add-commit -m "fixes #1 Foo bar"
Tell me about your favorite git commands & hacks in the comments section!