<< All Posts
17 useful Git commands

Here is a great list of 17 git commands I use pretty often and might be handy to you.

I was curious about what are my top commands that I’ve been using on my terminal on daily basis. With a quick Google search I ended on this really nice snippet:

history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' \
 | grep -v "./" | column -c3 -s " " -t | sort -nr | nl |  head -n10

And here is the output I got:

 1  258  25.8%  git
 2  215  21.5%  ls
 3  163  16.3%  cd
 4  83   8.3%   vim
 5  51   5.1%   exit
 6  34   3.4%   go
 7  19   1.9%   ssh
 8  15   1.5%   clear
 9  14   1.4%   tmux
10  11   1.1%   sudo

You might find actually low numbers which make sense. This laptop I’m writing right now is my personal computer so if I run that on my work computer, those numbers will be a bit higher indeed. Anyways, seeing “git” in the first position stood out from the other commands so I decided to share some of my top git commands on this post. It is not necessarily the most used command for me, but definitely useful and you might need it one day. Here they are:

1. How to list all git branches ordered by creation or last commit date?

git for-each-ref \
--sort=committerdate refs/heads/ --format='%(HEAD) \
 %(color:yellow)%(refname:short)%(color:reset) - \
%(color:red)%(objectname:short)%(color:reset) - \
%(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'

The most recent branches will be listed in the bottom. Just run it, you’re gonna be impressed. By the way, I’ve been using FzF, which is a fuzzy finder that you can hook up into your terminal and it gives you the ability to just type: Control + R, and start typing “git for-ea”, and boom, it will pull the last commands with that keyword from your history and all you gotta do is just hit Enter key. If you’re interested in learning more about FzF, check out the FzF project page

2. How to squash multiple commits into just one commit.

git checkout master
git pull
git checkout -
git rebase -i master

If you have VIM or any other editor setup in your bash, this will open it and ask which action you should use. Replace the word pick to s (squash). When you save your vim :x it will ask you to put the commit merge message. If you just want to merge it without worrying about commit messages, you can use f (force) which will skip the next step. After that, if you type: git log -n 5, you’re going to see the final result.

3. How to revert the last commit. (Even after pushed it to the remote branch)

git log -n 4 --oneline (Get the hash)
git revert <commit-hash>
git push

4. How to create an apply a git patch

Do you want to export the work you’ve done so far but don’t want to commit anything? Perhaps you want to export a patch and send it via email, slack? Maybe transferring a patch a different machine? Here is how you can do this:

git diff > my-patch-file.patch
patch -p1 < my-patch-file.patch

5. List all filenames added or changed from last commit:

git show --pretty=oneline --name-only

6. Get the diff between two git branches:

git diff branch1..branch2

7. Remove last commit and put all files from the previous commit on stash mode:

git reset --hard HEAD~
git reset HEAD^

8. Put all files from last commit on “unstash” mode:

git reset HEAD^

9. Delete all local branches that were already merged on the remote branch:

git branch -r --merged | grep origin | egrep -v '>|master' | cut -d/ -f2- | xargs git push origin --delete

10. How to remove a commit that you just pushed:

git reset --hard HEAD~ (Move your local branch to previous commit. (Before the last commit)
git push --force-with-lease (Push your changes to remote forcing. This will destroy your last commit)

11. How to destroy all old commit history and start from scratch:

Do you want to do a complete git reset? Keep in mind that this will destroy all your git history:

rm -rf .git
git init
git add .
git commit -m ‘Initial commit’
git remote add origin <your git repo url. git://…>
git push --force --set-upstream origin master

12. How to make VIM your default git editor:

git config --global core.editor "vim"

13. How to show all files for a specific commit:

git diff-tree --no-commit-id --name-only -r YOUR_COMMIT_HASH

14. Search over all git commits, for a specific keyword:

git log -S 'your key word' --source --all

15. How to edit a commit message for your last commit:

git commit --amend

16. How to change the author of a commit:

git commit --amend --author="Author Name <[email protected]>"

17. How to get a specific commit from another branch:

First of all, you’re gonna need the commit hash you want to pull into another branch. This is particularly useful when you messed up something in your branch and wants to start a new branch but want to pull together some specific commits.

git cherry-pick -n COMMIT_HASH_YOU_WANT_TO_PULL

Simple like that. Typing git status will show all files from that specific commit unstashed. The -n will tell git to not commit the files into this new branch.

That’s pretty much what I have for today. I have 125 git notes here. I’ll prepare another version of this post with more “advanced” git commands. Stay tuned.

<< All Posts


🇧🇷 🇨🇦
Runs on OpenBSD 🐡