A Git Cheatsheet Of Commands Every Developer Should Use

ravimengar

Ravi Mengar

Posted on December 10, 2020

A Git Cheatsheet Of Commands Every Developer Should Use

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.

💖 💪 🙅 🚩
ravimengar
Ravi Mengar

Posted on December 10, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related