If you find git confusing, I created this little cheat sheet! Please, note that I voluntary skipped the basic commands like git commit
, git pull/push
... This cheat sheet is intended for an "advanced" usage of git.
π§ Navigation - Go to the previous branch
git checkout -
π Get the history
# Log in one line
git log --oneline
# Retrieve all commits by message
# Here all commit that contain 'homepage'
git log --all --grep='homepage'
# Retrieve all commit by author
git log --author="Maxence"
πOoops #1: I reseted an unwanted commit. How to rollback?
# Get everything you did
git reflog
# then reset to the desired commit (i.e. HEAD@{4})
git reset HEAD@{4}
# ...or...
git reset --hard <commit-sha1>
For more detail about this command, I wrote this post.
What's happens when you `git commit`
Maxence Poutord γ» Oct 16 '19 γ» 5 min read
π€¦ββοΈOoops #2: I mixed-up with my local repo. How to clean it?
git fetch origin
git checkout master
git reset --hard origin/master
# You're now up-to-date with master!
π΅π»ββοΈDifference between my branch and master
git diff master..my-branch
β Custom commits
# Edit last commit
git commit --amend -m "A better message"
# Add something to the last commit without writing message again
git add . && git commit --amend --no-edit
# empty commit - can be useful to re-trigger CI build...
git commit --allow-empty -m "chore: re-trigger build"
If you don't know what to put in your commit messages, I wrote a post about conventional commits.
β»οΈ Squash commits
Let say I want to rebase the last 3 commits:
git rebase -i HEAD~3
- Leave the first "pick" and replace the rest by "
squash
" (or "s
") - Tidy up the commit message and save (
:wq
in vi).
π―Fixup
Let say I want to add something in the commit fed14a4c
git add .
git commit --fixup HEAD~1
# or replace HEAD~1 by the commit hash (fed14a4c)
git rebase -i HEAD~3 --autosquash
# save&quit the file (:wq in VI)
πΉExecute command on each commit when rebasing
For massives features, you might end up with a branch with a few commits inside. And then tests are failing and you want to identify the "guilty commit". You can use rebase --exec
to execute a command on each commit of the history.
# Will run "npm test" command on the last 3 commit β€οΈ
git rebase HEAD~3 --exec "npm run test"
π¦Stash
Because it's not all about git stash
and git stash pop
;)
# save all tracked files
git stash save "your message"
# list your stashes
git stash list
# retrieve stash and delete
git stash apply stash@{1}
git stash drop stash@{1}
# ... or in 1 command
git stash pop stash@{1}
π Clean
# remove branches that no longer exist on remote
git fetch -p
# remove all branch that contains "greenkeeper"
git fetch -p && git branch --remote | fgrep greenkeeper | sed 's/^.\{9\}//' | xargs git push origin --delete
π GitHub = Git
+ Hub
I use Hub as a wrapper for git. To enable it you've to set hub as an alias for git (alias git='hub'
).
# Open browser and go to the repository url (GitHub only)
git browse
Other commands are available here.
π¦ Bonus: my favourite git aliases
alias g='git'
alias glog='git log --oneline --decorate --graph'
alias gst='git status'
alias gp='git push'
alias ga='git add'
alias gc='git commit -v'
# π€
alias yolo='git push --force'
# useful for daily stand-up
git-standup() {
AUTHOR=${AUTHOR:="`git config user.name`"}
since=yesterday
if [[ $(date +%u) == 1 ]] ; then
since="2 days ago"
fi
git log --all --since "$since" --oneline --author="$AUTHOR"
}
And you, what's your favourite git command?
Thank you for taking the time to read this post. I hope you found it useful! If you liked it, please give it a β€οΈ or a π¦! Also, feel free to comment or ask questions in the section below or on Twitter @_maxpou :)
Originally published on maxpou.fr.
Latest comments (58)
This is brilliant! I learned a few new things, especially
hub
. I am gonna try it soon.Thanks @maxpou!
Thx for this! This is really what I wanted. Helped A LOT.
Can I translate in Korean this post? If you don't mind, I wanna share this awesome post in Korean. Surely, There will be a linke directing to this original post.
Hey, sure you can translate it in Korean. If you do so, could you please
I hope it's not too late :)
Thanks for sharing this!
When there a lot of commits it might take too much time. In such cases, git bisect comes to the rescue -- instead of executing a command on all commits it uses binary search to identify the first commit that breaks everything.
Perhaps, you might find this project useful: github.com/kamranahmedse/git-standup
Agree, bisect is the key when we have too many commits!
About git-standup, I don't really need it. I'm happy with my simple alias. I also grep a lot my git log :)
But, thanks for sharing! Someone else might found it handy!!!
Question: Where did you store the "alias-code" (from the last section "Bonus: my favourite git aliases") for it to take effect?
hey, to be transparent with you everything is on my dotfile repository!
π github.com/maxpou/dotfiles
(in a file called alias and I
source $HOME/.aliases
on my zshrc/bashrc)Very useful post! Thanks!
alias yolo='git push --force' π
nice isn't it? π
Nice summary! I'd like to translate it to Chinese (the translated text will be published at nextfe.com). Can you give me the permission?
Hey Jang, sorry for the late reply.
Sure! You can translate it in Chinese! :) (I just ask you to mention the original post + author)
Let me know when it's published!
Just published Chinese translation at: nextfe.com/git-cheatsheet-advanced/
Credit is given at the beginning of the translated text (also backlink to the original post).
BTW, in section "πΉExecute command on each commit when rebasing":
I think you meant to write
npm test
here (as shown in the figure below), despite thatnpm run test
andnpm test
are equivalent.Sorry for the very late reply but thank you!
alias yolo made me laugh hard :D
Haha I'm glad if you liked it!
I use this command everyday π
Great article! Thank you.
well, you don't need to create a bash alias, you can create git alias for
alias glog='git log --oneline --decorate --graph'
such asgit config alias.glog ...
Hmmm for some reasons, I am not a big fan of git aliases :) (but I don't remember why haha)
Found here: github.com/nicksp/dotfiles/blob/ma...
this config is... massive!!! π―
Very nice article
Nice post! Thank you!
My aliases:
Excellent guide! I like the idea of git-standup. β
For alias, to work even with mistyping, I also use:
Also, one thing that was really useful for me is the meta repository of Matt Walters, so I can avoid git submodule and subtree to work with multiple repositories.
Thanks Sean :)