There are developers who use only three Git commands: git add, git commit and git push - and that's enough for them in their everyday work. But Git is so much more than these 3 commands! As we can read in the documentation:
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
To take advantage of this speed and efficiency you should learn some useful commands. In this blogpost, I'd like to share with you some of the most interesting Git commands - and my personal favorites. Let's explore them!
1. git stash -u
You may have used git stash command (this command takes modified files in your working directory and saves them in a separate place) and you may have been annoyed that the new files were not stashed. By using -u option you can move your new files to the stash.
2. git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch Filename' \
--prune-empty --tag-name-filter cat -- --all
Have you ever discovered that there's a big unnecessary file in your repository? You could just remove it from your local directory and push that change to the remote. However, there is one problem with this approach: the file still exists in the repository history. Good news - it's possible to remove such file permanently! See here for details.
3. git add --renormalize
Have you ever heard of the wall of pink? It is a situation when someone commited a small change to your repository, but all the files in this change have all the lines changed. In GUI tool, you may see all the lines pink (removed) and right below all the lines green (added). But how did it happen? The answer to this problem may be different line endings (CR LF vs LF). Read more about it here and here.
4. git log --graph --decorate --abbrev-commit --all --format=format:"%C(magenta)%h%n%C(green)%t%n%C(yellow)%an%n%C(red)%ae%n%C(blue)%ad%n%C(cyan)%s"
Did you know that you can customize git log appearance? Read more about possible formatting options here.
5. git shortlog -s -n --all --no-merges
This command will show how many commits you and your teammates pushed to the repository.
6. git push --force-with-lease
More polite version of git push --force. Remote branch won't be overwritten if someone pushed some changes to it.
7. git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
Delete branches that were merged to current branch, except for branches excluded in the filter (master and dev in this example).
8. git reflog
Using this command, you can find a log of what you did in the repository. You can use it for example to restore a commit from a branch that was deleted.
9. git commit --amend --no-edit
If you forgot to commit one extra change, you can use this command to edit your commit. Use it only if you did not push your commit!
Imagine that you find a bug in the current version, and you know that this feature worked in some old commit. There are probably dozens of commits between the current version with the bug and the working one. If you have trouble with identifying what part of code and what commit is responsible for that bug, you can use git-bisect to identify the root cause of the issue. You start by marking two commits as bad and good, and use git-bisect to perform binary search in the repository history. More details can be found in the documentation.
11. git grep
You probably know that you can search your working directory with grep function. Did you know you can do the same in the repository history? If you remember some part of code that was deleted and you need it now, use git grep. Usage is similar to grep function. Find out more here.