A Git Cheatsheet Of Commands Every Developer Should Use
Tags: git, github, codenewbie, beginners
Published: 2020-12-10 07:36:30

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](https://git-scm.com/downloads) 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

```git 
cd ~/projects/myproject
git init
git add .
``` 
From existing repo

* `git clone` is used to clone a repositroy from a remote server

```git 
git clone ~/existing/repo ~new/repo
git clone you@host:dir/project.git (default protocol is ssh)
``` 

Remote repository for existing local data
```git
mkdir repo.git && cd repo.git
git init --bare[--shared=group]
```

### UPDATE

Fetch latest changes from origin
```git
git fetch (this does not merge them)
```
Pull latest changes from origin

```git
git pull (does a fetch followed by a merge)
```
Apply a patch that someone sent you

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

Commit all local changes 
```git
git commit -a
```
Commit previously staged changes
```git
git commit -m "descriptive message"
```
Prepare a patch for other developers
```git
git format-patch origin
```
Push changes to origin
```git
git push [origin][branch]
```
Make a version or a milestone
```git
git tag <version_name>
```
### BRANCH

Switch to the BRANCH branch 
```git
git checkout <BRANCH>
```
Merge branch B1 into branch B2
```git
git checkout <B2>
git merge <B1>
```
Create branch based on HEAD
```git
git branch <BRANCH>
```
Create branch based on another
```git
git checkout <new><base>
```
Delete a branch
```git
git branch -d <branch>
```

### REVERT

Return to the last committed state
```git
git checkout -f | git reset --hard (you cannot undo a hard reset)
```
Revert the last commit
```git
git revert HEAD (Creates a new commit)
```
Revert specific commit
```git
git revert $id (Creates a new commit)
```
Fix the last commit
```git
git commit -a --amend (after editing the broken files)
```
Checkout the ID version of a file
```git
git checkout <ID><file>
```

### SHOW

Files changed in working directory
```git
git status
```
Changes to tracked files
```git
git diff
```
Changes between ID1 and ID2
```git
git diff <ID1><ID2>
```
History of changes
```git
git log
```
History of changes with files changed
```git
git whatchanged
```
Who changed what and when in a file
```git
git blame <file>
```
A commit identifies by ID
```git
git show <ID>
```
A specific file from a specific ID
```git
git diff <ID>:<file>
```
All local branches
```git
git branch (star "*" marks the current branch)
```
Search for patterns
```git
git grep<pattern>[path]
```

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.