DEV Community

Cover image for 15 Git Commands You May Not Know
Zaiste
Zaiste

Posted on • Originally published at zaiste.net

15 Git Commands You May Not Know

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
Enter fullscreen mode Exit fullscreen mode

—-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
Enter fullscreen mode Exit fullscreen mode

-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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

--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 -
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If you want to rename the currently checked out branch, you can shorten this command to the following form:

git branch -m new-name
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

12. What changed?

git whatchanged —-since=‘2 weeks ago’
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

14. Find Branches

git branch --contains <commit>
Enter fullscreen mode Exit fullscreen mode

This command will show all branches that contain a particular commit.

15. Optimize the repository locally

git gc --prune=now --aggressive
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Latest comments (39)

Collapse
 
scratchcodeio profile image
Scratch Code

Great Article! I recommended this tutorial for beginners: scratchcode.io/basic-git-commands-...

Collapse
 
miteshkamat27 profile image
Mitesh Kamat

That's a good cheatsheet

Collapse
 
diogotome profile image
Diogo Tomé

In my workflow it is routine to delete branches on Github Pull Requests when they're merged. This leaves the local repo with local branches that have since lost its tracking branch. To clear this I have the following alias under .gitconfig

prune-branches = !git remote prune origin && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D

So whenever a bunch of my PR's have been merged I just run git prune-branches and it prunes the remote branch references but also deletes the corresponding local ones.

Collapse
 
tpompon profile image
Thomas Pompon

Nothing about git cherry-pick ? Still a nice post, could be useful !

Collapse
 
pshchelo profile image
Pavlo Shchelokovskyy

as alternative to 11. you can also use

git mergetool [-t <tool>]

when rebase fails with merge conflicts

The default merge tool to use is configurable in .gitconfig, mine is vimdiff :-)
For vimdiff it opens 4-pane view with two conflicting versions (REMOTE and LOCAL), their common ancestor (BASE) and the actual current file with merge conflicts. After resolving the conflict and exiting it will open the next file with conflicts, one by one.

Out of the box Git knows how to work with more diff/merge tools - emerge, gvimdiff, kdiff3, meld, vimdiff, and tortoisemerge, but you can configure other ones explicitly yourself.

And there's also a similar but simpler git difftool which is just a visual frontend to git diff

git-scm.com/docs/git-mergetool
git-scm.com/docs/git-difftool

Collapse
 
tomakesense profile image
Kevin Xiong

My small contribution

1.Count the number of commits on a branch
git rev-list --count master

2.Count number of branches in a git repository
git branch | wc -l

3.Delete remote branch cache in your local repository
git fetch -p

4.Delete remote tag cache in your local repository
git fetch -p -P

5.Delete a git alias
git config --global --unset alias.XXX

6.List git aliases
git config --list | grep alias

Collapse
 
sai2572910 profile image
Sai

You think Code processing spreed of Git of Magit faster?

Collapse
 
guyzmo profile image
zmo

you're telling about Magit, there's also the Vimagit plugin for the one true editor!

I cannot live without it. Before using that, I was already using GitXR on OSX, before it became discontinued. I also tried git-cola on linux.

Collapse
 
zerquix18 profile image
I'm Luis! \^-^/

Here's my small contribution: If you added a commit and already pushed it or merged it into another branch and you need to reverse those changes, you can always go:


git reverse {hash}

It will create a new commit with the changes reversed. Also, the new commit will have a reference to the reversed commit (i.e Github will tell you "this reverses {hash}")

Collapse
 
rajeshduggal profile image
Rajesh Duggal

Which version of git has "git reverse"? I can't find it mentioned in git-scm.com/docs

Collapse
 
mfilej profile image
Miha Filej

Probably meant git revert

Some comments may only be visible to logged-in visitors. Sign in to view all comments.