DEV Community

loading...
Cover image for A Git Cheatsheet Of Commands Every Developer Should Use

A Git Cheatsheet Of Commands Every Developer Should Use

ravimengar profile image Ravi Mengar ・3 min read

GIT is the most widely used distributed open-source Version Control System that allows you to track and manage changes made to the files locally on your computer.

However, the tool is so powerful and extensive enough to get lost in all the possible commands it has.

Hence, based on my own experience, here’s a compilation of GIT cheat sheet
which is the most important and commonly used GIT commands for easy reference.

Here, you can download GIT for all platforms.


GIT CHEAT SHEET 📋


CREATE

From existing data,

  • git init creates new repository in current directory
  • git add . add all latest changes to the next commit
cd ~/projects/myproject
git init
git add .
Enter fullscreen mode Exit fullscreen mode

From existing repo

  • git clone is used to clone a repositroy from a remote server
git clone ~/existing/repo ~new/repo
git clone you@host:dir/project.git (default protocol is ssh)
Enter fullscreen mode Exit fullscreen mode

Remote repository for existing local data

mkdir repo.git && cd repo.git
git init --bare[--shared=group]
Enter fullscreen mode Exit fullscreen mode

UPDATE

Fetch latest changes from origin

git fetch (this does not merge them)
Enter fullscreen mode Exit fullscreen mode

Pull latest changes from origin

git pull (does a fetch followed by a merge)
Enter fullscreen mode Exit fullscreen mode

Apply a patch that someone sent you

git am -3 patch.mbox (In case of conflict, resolve the conflict and)
git am --resolve
Enter fullscreen mode Exit fullscreen mode

PUBLISH

Commit all local changes

git commit -a
Enter fullscreen mode Exit fullscreen mode

Commit previously staged changes

git commit -m "descriptive message"
Enter fullscreen mode Exit fullscreen mode

Prepare a patch for other developers

git format-patch origin
Enter fullscreen mode Exit fullscreen mode

Push changes to origin

git push [origin][branch]
Enter fullscreen mode Exit fullscreen mode

Make a version or a milestone

git tag <version_name>
Enter fullscreen mode Exit fullscreen mode

BRANCH

Switch to the BRANCH branch

git checkout <BRANCH>
Enter fullscreen mode Exit fullscreen mode

Merge branch B1 into branch B2

git checkout <B2>
git merge <B1>
Enter fullscreen mode Exit fullscreen mode

Create branch based on HEAD

git branch <BRANCH>
Enter fullscreen mode Exit fullscreen mode

Create branch based on another

git checkout <new><base>
Enter fullscreen mode Exit fullscreen mode

Delete a branch

git branch -d <branch>
Enter fullscreen mode Exit fullscreen mode

REVERT

Return to the last committed state

git checkout -f | git reset --hard (you cannot undo a hard reset)
Enter fullscreen mode Exit fullscreen mode

Revert the last commit

git revert HEAD (Creates a new commit)
Enter fullscreen mode Exit fullscreen mode

Revert specific commit

git revert $id (Creates a new commit)
Enter fullscreen mode Exit fullscreen mode

Fix the last commit

git commit -a --amend (after editing the broken files)
Enter fullscreen mode Exit fullscreen mode

Checkout the ID version of a file

git checkout <ID><file>
Enter fullscreen mode Exit fullscreen mode

SHOW

Files changed in working directory

git status
Enter fullscreen mode Exit fullscreen mode

Changes to tracked files

git diff
Enter fullscreen mode Exit fullscreen mode

Changes between ID1 and ID2

git diff <ID1><ID2>
Enter fullscreen mode Exit fullscreen mode

History of changes

git log
Enter fullscreen mode Exit fullscreen mode

History of changes with files changed

git whatchanged
Enter fullscreen mode Exit fullscreen mode

Who changed what and when in a file

git blame <file>
Enter fullscreen mode Exit fullscreen mode

A commit identifies by ID

git show <ID>
Enter fullscreen mode Exit fullscreen mode

A specific file from a specific ID

git diff <ID>:<file>
Enter fullscreen mode Exit fullscreen mode

All local branches

git branch (star "*" marks the current branch)
Enter fullscreen mode Exit fullscreen mode

Search for patterns

git grep<pattern>[path]
Enter fullscreen mode Exit fullscreen mode

Here,

  • master is the default development branch
  • origin is the default upstream repository
  • HEAD is the current branch

That's it from me today. I hope this cheat sheet helps you with some of the problems you may encounter along the way.

Certainly, it does not cover all the things, but it’s a good article to begin with.

Thanks for reading and let me know about your favorite Git commands in response to this article and share it with your friends and colleagues.

Discussion (16)

pic
Editor guide
Collapse
myrtletree33 profile image
jhtong

Another command I would add is Git Rebase:

git rebase origin/master
Enter fullscreen mode Exit fullscreen mode

Which would apply all the changes in master, below your branch, to make your commits grouped together and more organized.

Also, I use this quite a lot:

git rebase -i HEAD~2
Enter fullscreen mode Exit fullscreen mode

Which runs your rebase in interactive mode for the most recent 2 commits (which is really helpful for squashing and renaming your commits), making it more readable.

Collapse
eecolor profile image
EECOLOR

In practice I have noticed more problems arise when using rebase. These problems come up when you work as a team in a branch or have merged other branches (and solved conflicts).

We now have the policy that you are only allowed to rebase if the code has not been pushed to the remote. We also prevent push --force which is required for most rebases.

Collapse
ravimengar profile image
Ravi Mengar Author

Hey, jhtong

Thanks for letting me know about these commands. Will surely make use of it. Appreciate your help man👍

Collapse
liliang8858 profile image
Collapse
_arisharyanto profile image
Aris Haryanto

also you need to add

git stash
Enter fullscreen mode Exit fullscreen mode

when you want to keep your working progress without commit then you need to switch to other branch

and

git stash pop
Enter fullscreen mode Exit fullscreen mode

to put their back to your file

i think this is important if you work with some branch

Collapse
loebkes profile image
Ludwig Göbkes

I often use git stash save -m "name" instead as I can give the stash a name for easier identification later on

Collapse
i386net profile image
i386net

It's deprecated and it's recommended to use git stash push -m instead

Collapse
ravimengar profile image
Ravi Mengar Author

Hey, Aris

Thanks for highlighting the above helpful commands. Will surely make use of it 👍

Collapse
bew profile image
Benoit de Chezelles

Nice cheatsheet!

For git status you say Files changed in working directory, I think it's wrong, git status gives you the status of the current branch and the files of the whole repo, not just the working directory! For working directory status, you'd do git status .

Also checkout the subcommand git worktree (and its various options), I use it a lot to check something in another branch, review whole branches for a pr of someone else, or other cases. It allows me to have 2+ branches checked out at the same time, sharing the history graph!

Collapse
loebkes profile image
Ludwig Göbkes

I often use git branch --merged to identify branches that I can delete as I have already merged them. Same goes for --no-merged to find out if I've forgotten to merge some features.

Collapse
picwellwisher12pk profile image
Amir Hameed

git am -3 patch.mbox (In case of conflict, resolve the conflict and)
git am --resolve

can you elaborate these?

Collapse
ravimengar profile image
Ravi Mengar Author

Hey Amir,

Here, I've tried to elaborate your question,

  1. git am - Apply a series of patches from a mailbox.

  2. git am -3 patch : By default the command will try to detect the patch format automatically. This option allows the user to bypass the automatic detection and specify the patch format that the patch(es) should be interpreted as. Valid formats are mbox, mboxrd, stgit, stgit-series and hg.
    ( the -3 will do a three-way merge if there are conflicts )

  3. git am --resolve : When a patch failure occurs, will be printed to the screen before exiting. This overrides the standard message informing you to use --continue or --skip to handle the failure. This is solely for internal use between git rebase and git am.

I hope this will help you !!!

Collapse
hasii2011 profile image
Humberto A Sanchez II

This is very nice. I made a PDF out of this page

Collapse
ravimengar profile image
Ravi Mengar Author

I'm glad that you found this helpful.😃

Collapse
harshilparmar profile image
Harshil Parmar

Awesome !!! 👌🙌

Collapse
jazzthedog profile image
Jazz

I think 'git switch' is now more in favor than 'git checkout'? I use that to mainly switch between branches.