DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 964,423 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Sarath P M
Sarath P M

Posted on • Updated on

Git Cheatsheet

Hello everyone, here is a quick reference guide of the git commands:

git config

Setup author name and mail to be used for all commits in current repository, Devs commonly use --global flag to set config options for current user.
git config --global user.name "username"
git config --global user.email "usermail@example.com"

git clone

Clone the remote repository with HTTPS or with SSH. This should create a new child directory for the project sources.
git clone <REPO CLONE URL>

git status

Shows the status of your current branch. It will tell you which files you have modified, which files are stages for commit and which files are not. It will also let you know if your local branch is behind, or not up to date with, its remote tracking branch. This command is particularly useful in combination with git add when you only want to commit certain files.

git branch

Create a branch, or show a list of local or remote branches. With git branch, you can add options to do branch-related things:

  • git branch new-branch-name: Create a branch off of the current branch named new-branch-name
  • -d or --delete: Delete a branch. e.g. git branch -d feature-branch-name
  • -D: Force delete a branch. This is used when you want to delete a branch that has changes that weren't pushed to the server or merged. e.g. git branch -D feature-branch-name
  • -a or --all: Display all local and remote-tracking branches.

git fetch

Fetches branches from the server, along with their history and information. You can also use git fetch origin to update your remote-tracking branches.

git pull

Update your working directory by performing a git fetch, followed by a git merge. This is useful if you want to bring your branch up to date with the remote-tracking branch.
You can also use the --rebase option to perform a rebase rather than a merge.

git checkout

This command has a few functions. It can be used to create a new branch off of the current branch, it can be used to checkout a branch from the server, or switch to another branch.

  • git checkout develop: you will use this a lot. It will switch you from whatever branch you are currently in to the develop branch. If you have changes in your current branch, you will need to either stash them or undo your changes before you switch branches.
  • git checkout branch-on-server: If you want to work on another persons branch (assuming they already pushed their branch to the remote repository), you can use this command to create a copy of the branch on your local machine. You will need to do git fetch first to grab the information about the branch.
  • git checkout -b new-branch-name: create a new branch off of the current branch and switch to that branch. If you are in develop, this will create a new branch from develop. This command is synonymous to git branch new-branch-name && git checkout new-branch-name

git add

You made changes to your branch, and you want to stage your changes for committing. You will need to add them to what is known as the index. Here are two ways to do this:

  • git add /filepath/file.f: this will add the single file.
  • git add .: this will add all the changed files. Be careful here; make sure you aren't adding unwanted changes by reviewing your changes using git status. If you accidentally staged unwanted files, take a look at git reset described below.

git commit

You want to make a commit! Awesome! Here's how you do it:

  • git commit: This will make a commit of the files you have staged using git add, and will open a VIM editor (or whichever editor you have configured in your .gitconfig) to enter your commit message.
  • git commit --amend: This is used to modify the most recent commit. It combines changes in the staging environment with the latest commit, and creates a new commit. This new commit replaces the latest commit entirely.
  • git commit -a: This will make a commit and add all changed files, and will open an editor to enter your commit message. This is the same as doing git add . && git commit.
  • git commit -m "commit message": Skip the editor and put your entire message in the command line!
  • git commit -a -m "commit message": Combine them all! Fab.

git push

Once you make a commit, you will need to push your changes to the server to make it visible to the world. Once you push, your branch and commit(s) will be visible to others.

  • git push branch-name or git push origin HEAD: Push your branch upstream. You can use either version; using origin HEAD is simply a more verbose way of pushing. It signifies you are pushing the tip (HEAD) your branch to the same name on the remote origin.
  • git push origin -f branch-name or git push origin +branch-name: ❗ ❗ ❗ BE CAREFUL: This is a potentially scary command, and you should only use it if you really know what you are doing. It will overwrite the branch you specified on the remote repository with your local copy of the branch. ❗ ❗ ❗

git log

Show the history of all branches and their commits. Useful for seeing the most recent commit and getting commit hashes.

  • git log: Show all commits on this branch, along with the commit hash, author, date and message.
  • git log --oneline: Show a simplified version of the above command, showing only the commit hash and commit message.
  • git log --graph: Show a graph of the commit history for your branch. (AWESOME)
  • git log --max-count <n>: Limit the number of commits shown.
  • git log --oneline --graph --max-count 50: Combined them all! Woo!

git rebase


Rebasing is a bit more advanced, but incredibly useful when you need it. When you perform a rebase, you are changing the base of your branch. In essence, a rebase will look at each commit on your branch and update the code to make it seem like you've been working off the new base all along. Sometimes, a rebase will barf if it encounters a situation in which it tries to update a piece of code that you have already modified. In this case, it doesn't know which version of the code to use, so it leaves it to you to resolve them manually.
Although they have similar uses, a rebase differs from a merge in that a rebase updates your branch by tweaking each commit, and merge will update your branch by creating a new commit at the tip of your branch. Usually, there are standards and practices employed in a project or team around which method is preferred. Have a discussion with your team about which workflow they prefer.

git stash

Stashing allows you to save your current unstaged changes and bring your branch back to an unmodified state. When you stash, your changes are pushed onto a stack. This is especially useful if you need to quickly switch to another branch without having to commit your incomplete changes.

  • git stash: stash your unstaged changes.
  • git stash pop: unstash your changes.
  • git stash drop: drop your stashed changes. Careful, you will lose your changes!
  • git stash push <path>: stash a single file
  • git stash -m <message>: add a message to your stash

git reset

Git reset is used to unstage files or remove commits. It does so by changing what the tip of the branch (HEAD) points to.
Three trees are affected by a reset:

  • HEAD: the tip of your branch
  • Index: the staging area, which is like a proposed commit
  • Working Directory: the files currently on your disk There are three reset modes: --soft, --mixed (default), and --hard:
  • --soft: This is the very first step in any reset mode. For a soft reset,
  • --mixed:
  • --hard:

  • Undo git add:

    • git add .
    • git reset -- file.html (unstage single file, or)
    • git reset (to unstage all)
  • Undo most recent commit:

    • git commit -a -m "commit message"
    • git reset --soft HEAD^ (undo commit that is at the tip of the branch)
    • git commit -a -m "new commit message"
  • Undo a commit permanently:

    • git reset --hard HEAD~2 (destroy the last two commits, i.e. HEAD and HEAD^)

🌐 Connect with me

 🔗LinkedIn
 🔗GitHub

Top comments (16)

Collapse
 
thonio profile image
Thonio • Edited on

Very nice to see it! But, from experience, I noticed git add -A is not the same has git commit -a or git add .. In the case who you add "new" file on project. New file need to add with git add -A otherwise it will be ignored.

Collapse
 
vinceamstoutz profile image
Vincent Amstoutz

Very good explantions and work. Just a suggestion to complete your cheatsheet : it's recommend to use git push --force-with-lease instead of only git push -f or git push --force in most cases. A full example of this topic here.

Collapse
 
tiag00liveira profile image
Tiago Oliveira de São José

Very good, thanks for sharing.
You can use git switch to create, change, etc. branchs
I like this command, because for me it's more semantic than checkout in these cases.

See more in git doc: git-scm.com/docs/git-switch

Collapse
 
kasukur profile image
Sri • Edited on

very nice list of commands, you might want to add Rewriting the most recent commit message - git commit --amend. This is useful when we have rules for the hook when we push the commits.

Collapse
 
sarath_pm profile image
Sarath P M Author

Thanks for the feedback, Sri. I will add it. 😊

Collapse
 
anderspersson profile image
Anders Persson

Great this will be in my "save" list. thanks.

Collapse
 
lakuapik profile image
David Adi Nugroho

$ man giteveryday

Collapse
 
raddevus profile image
raddevus

Very nice & helpful summary of numerous git features. Thanks for sharing. I bookmarked this for later use. really great

Collapse
 
sarath_pm profile image
Sarath P M Author

I'm really glad to hear that 😊

Collapse
 
imranshaik012 profile image
Imran Shaik

Very helpful, thanks for sharing

Collapse
 
xilam675 profile image
xilam675

Good job

Collapse
 
romankurnovskii profile image
Roman

helpful, thank you
what about "revert" usage for some commits ?

Collapse
 
krighaar profile image
Peter Krighaar

Nice walkthrough, what about git prune in order to clean up for repos in origin, not existing any more

Collapse
 
hassan_dev91 profile image
Hassan

Thanks, that's so sweet ❤️

Collapse
 
kevincp17 profile image
kevincp17

This is pretty neat, thanks

Collapse
 
macantovitz profile image
Matheus Cantovitz

Very good, thanks for sharing

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠