Using Git may be intimidating at times. There are so many commands and details to learn. The documentation, however, while being immense, is still greatly accessible. Once you overcome the initial feeling of being overwhelmed, the things will start to fall into place.
Here is a list of 15 Git commands that you may not know yet, but hopefully they will help you out on a journey to master this tool.
1. Modify The Most Recent Commit
git commit --amend
—-amend
allows to append staged changes (e.g. to add a forgotten file) to the previous commit. Adding —-no-edit
on top of that will amend the last commit without changing its commit message. If there are no changes, -—amend
will allow you to reword the last commit message.
For more: git help commit
2. Interactively Add Selected Parts of Files
git add -p
-p
(or —patch
) allows to interactively select parts of each tracked file to commit. This way each commit contains only related changes.
For more: git help add
3. Interactively Stash Selected Parts of Files
git stash -p
Similar to git-add
, you can use --patch
option to interactively select parts of each tracked file to stash.
For more: git help stash
4. Stash with untracked
git stash -u
By default, when stashing, the untracked files are not included. In order to change that bevahiour and include those files as well you need to use -u
parameter. There is also -a
(—all
) which stashes both untracked and ignored files altogether, which is probably something you usually won’t need.
5. Interactively Revert Selected Parts of Files
git checkout -p
--patch
can be also used to selectively discard parts of each tracked file. I aliased this command as git discard
For more: git help checkout
6. Switch to Previous Branch
git checkout -
This command allows you to quickly switch to the previously checked out branch. On a general note -
is an alias for the previous branch. It can be used with other commands as well. I aliased checkout
to co
so, it becomes just git co -
7. Revert All Local Changes
git checkout .
If you are sure that all of your local changes can be discarded, you can use .
to do it at once. It is, however, a good practice to always use checkout --patch
.
8. Show changes
git diff --staged
This command shows all staged changes (those added to the index) in contrast to just git diff
which only shows changes in the working directory (without those in the index).
For more: git help diff
9. Rename Branches Locally
git branch -m old-name new-name
If you want to rename the currently checked out branch, you can shorten this command to the following form:
git branch -m new-name
For more: git help branch
10. Rename Branches Remotely
In order to rename a branch remotely, once you renamed your branch locally, you need to first remove that branch remotely and then push the renamed branch again.
git push origin :old-name
git push origin new-name
11. Open All Files with Conflicts at Once
Rebasing may lead to conflicts, the following command will open all files which need your help to resolve these conflicts.
git diff --name-only --diff-filter=U | uniq | xargs $EDITOR
12. What changed?
git whatchanged —-since=‘2 weeks ago’
This command will show a log with changes introduced by each commit from the last two weeks.
13. Remove file from last commit
Let's say you committed a file by mistake. You can quickly remove that file from the last commit by combining rm
and commit --amend
commands:
git rm —-cached <file-to-remove>
git commit —-amend
14. Find Branches
git branch --contains <commit>
This command will show all branches that contain a particular commit.
15. Optimize the repository locally
git gc --prune=now --aggressive
For more: git help gc
Bonus
Although I like CLI a lot, I highly recommend checking Magit to further step up your Git game. It is one of best pieces of software I used.
There is, also, a fantastic overview of recommended Git workflows available via help
command. Be sure to read it thoroughly!
git help workflows
Oldest comments (39)
Great article! Sometimes we forget how powerful git really is.
Another command I really like and most people don't use is
git bisect
.It's a great way to find a buggy commit.
This is one of those commands you don’t use very often but is priceless when you do need it.
Oh nice! Didn't know about git co -, and renaming branch! Thanks!
Nice 👌 post
Great article, didn't know about some of these, especially 9, 11 and 12.
Adding to 15, another cleanup argument I find very useful is
git remote prune origin
to cleanup deleted branches on
origin
Very glad you wrote these important tips down. I was not aware of all of them but number 6 and 7 are shortcuts I often use.
Be aware that if you use too much shorthand git command you get the reputation as git wizard which leads to the hard to deal with problems. :-D
Very well written, thank you Zaiste.
Thanks Claudio for kind words. I'm happy you liked this article!
Oh, I didn't know about the
--patch
to stash and checkout, that's nice. I usually usegit add -i
when I want to fiddle, since you can then see what's going on a little easier.Nice! Can I translate this post to Chinese? The translation will be published at nextfe.com (sponsored by LeanCloud, a BaaS provider) and related Chinese social network accounts. The translated text will backlink to this original post.
I learned
git cherrypick
a few weeks ago and found that really useful - we needed a couple of commits from one branch to be brought into a new branch of fixes.I didn't know about a lot of these commands! Thanks a lot. It's always good to improve our git abilities. (:
Absolutely! It's also good to take baby steps, especially where there is a lot to learn.
Not to be weird, but you should read the official docs on all technologies you use, you'll be surprise how much time you will save on the long run. Git, the IDEs, programming languages and especially libs/frameworks have good documented functionalities, eg: git-scm.com/docs
Thanks for this interesting article.
I'm glad you liked it!
Some comments may only be visible to logged-in visitors. Sign in to view all comments.