DEV Community

Cover image for Git Commands Cheat Sheet
Madhav Ganesan
Madhav Ganesan

Posted on • Edited on

Git Commands Cheat Sheet

Git is a distributed version control system (DVCS) that allows multiple developers to collaborate on a project, tracking changes to files and coordinating work seamlessly.

Initializing a Repository:

git init
Enter fullscreen mode Exit fullscreen mode

Cloning a Repository:

git clone -b <branch-name> <url> <folder-name>
Enter fullscreen mode Exit fullscreen mode

Working with Branches:

Create a new branch:

git checkout -b <new-branch>
Enter fullscreen mode Exit fullscreen mode

Switch branches:

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Create and Switch branches:

git switch -c <new-branch>
Enter fullscreen mode Exit fullscreen mode

List all branches:

git branch
Enter fullscreen mode Exit fullscreen mode

Staging and Committing Changes:

Add files to staging area:

git add .
Enter fullscreen mode Exit fullscreen mode

Commit changes:

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

View changes before staging:

git diff
Enter fullscreen mode Exit fullscreen mode

Remote Repositories:

Add a remote repository:

git remote add origin <remote-repository-URL>
Enter fullscreen mode Exit fullscreen mode

List all remotes:

git remote -v
Enter fullscreen mode Exit fullscreen mode

Push changes to a remote repository:

git push -u origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Pull changes from a remote repository:
It fetches changes from the remote repository and immediately tries to merge them into your current branch

git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Fetch changes from remote repository:
It retrieves new data from a remote repository but does not integrate it into your working files.

git fetch
Enter fullscreen mode Exit fullscreen mode

Stashing Changes:

Stashing in Git allows you to temporarily save changes that are not yet ready to be committed, so we can switch to another branch without losing your progress. It is used when you need to quickly switch contexts or update your working directory without committing incomplete work.

Stash changes:

git stash
Enter fullscreen mode Exit fullscreen mode

Apply stashed changes:

git stash apply
Enter fullscreen mode Exit fullscreen mode

Configuration:

Change user name:

git config --global user.name "name"
Enter fullscreen mode Exit fullscreen mode

Change user email:

git config --global user.email "email"
Enter fullscreen mode Exit fullscreen mode

Reset:

Soft reset:

git reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode

Moves to the previous commit and discards all changes.

Hard reset:

git reset --hard HEAD^
Enter fullscreen mode Exit fullscreen mode

Moves to the previous commit but keeps all changes.

( HEAD is a reference to the current commit &
notation HEAD^ refers to the parent commit of the current HEAD )

Clone large files

LFS

git lfs install
git clone <url>
Enter fullscreen mode Exit fullscreen mode

Increase buffer size:

git config --global http.postBuffer 1048576000
Enter fullscreen mode Exit fullscreen mode

Top comments (19)

Collapse
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza

for reset here is 2 commands

git reset --hard HEAD^

git reset --soft HEAD^

Collapse
 
madgan95 profile image
Madhav Ganesan

Can you explain the use of these commands?

Collapse
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza
  • git reset --hard HEAD^: Moves the current branch to the previous commit, discarding all changes in the working directory and index.
  • git reset --soft HEAD^: Moves the current branch to the previous commit, keeping changes in the index and working directory.
Thread Thread
 
madgan95 profile image
Madhav Ganesan

Thanks Syed๐Ÿ‘Œ

Thread Thread
 
madgan95 profile image
Madhav Ganesan

I have added them in the blog๐Ÿ‘

Thread Thread
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza

๐Ÿ‘

Collapse
 
pioche profile image
Ridwan Yunus

He gave you food you still want him to feed you.

How about you make your own research on their functions?

Collapse
 
gregjacobs profile image
Greg • Edited

Youโ€™ve got your Resets backwards.

Where you say โ€œSoft reset:โ€ you are displaying the git command for HARD.

Where you say โ€œHard reset:โ€ you are displaying the git command for SOFT.

Should beโ€ฆ

Soft reset:
git reset --soft HEAD^

Hard reset:
git reset --hard HEAD^
Enter fullscreen mode Exit fullscreen mode
Collapse
 
madgan95 profile image
Madhav Ganesan

Thanks for pointing out. Changed๐Ÿ‘

Collapse
 
andre75021 profile image
Andre Luiz

Great post, so much usefull

Collapse
 
madgan95 profile image
Madhav Ganesan

Thank you so much! I'm glad you found it useful๐Ÿ˜Š

Collapse
 
andre75021 profile image
Andre Luiz

You're welcome!!

Collapse
 
jgdevelops profile image
Julian Gaston

Classic post.

Collapse
 
madgan95 profile image
Madhav Ganesan

Thank you Julian๐Ÿซก

Collapse
 
litlyx profile image
Antonio | CEO at Litlyx.com

Pretty usefull. Thanks for sharing!

Collapse
 
madgan95 profile image
Madhav Ganesan

Thank you ๐Ÿ˜Š

Collapse
 
qui profile image
qui • Edited

You said origin but it may another thing.

Collapse
 
madgan95 profile image
Madhav Ganesan

Yes, but when we clone a repository, the remote name by default is origin unless we change them explicitly.๐Ÿ‘

Collapse
 
qui profile image
qui

Yes you're right but I think you should explain it.

Have a good day. :)