Skip to content

This cheat sheet features the most important and commonly used Git commands for easy reference.

Notifications You must be signed in to change notification settings

siamshaeed/git_cheat_sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 

Repository files navigation

Git Cheat Sheet

A distributed version control system is a system that helps you keep track of changes you've made to files in your project.

This change history lives on your local machine and lets you revert to a previous version of your project with ease in case something goes wrong.

Git makes collaboration easy. Everyone on the team can keep a full backup of the repositories they're working on on their local machine. Then, thanks to an external server like BitBucket, GitHub or GitLab, they can safely store the repository in a single place.

This way, different members of the team can copy it locally and everyone has a clear overview of all changes made by the whole team.

How to check your Git version:

  git --version

How to check your Git configuration:

The command below returns a list of information about your git configuration including user name and email:

  git config -l

How to setup your Git username:

With the command below you can configure your username: and email

  git config --global user.name "username"
  git config --global user.email "email"

How to cache your login credentials in Git:

You can store login credentials in the cache so you don't have to type them in each time. Just use this command:

  git config --global credential.helper cache

How to initialize a Git repo:

Everything starts from here. The first step is to initialize a new Git repo locally in your project root. You can do so with the command below:

  git init

How to add a file to the staging area in Git:

The command below will add a file to the staging area. Just replace filename_here with the name of the file you want to add to the staging area.

  git add filename_here

How to add all files in the staging area in Git

If you want to add all files in your project to the staging area, you can use a wildcard . and every file will be added for you.

  git add .

How to check a repository's status in Git:

This command will show the status of the current repository including staged, unstaged, and untracked files.

  git status

How to commit changes with a message in Git:

You can add a commit message without opening the editor. This command lets you only specify a short summary for your commit message.

  git commit -m "your commit message here"

How to commit changes (and skip the staging area) in Git:

You can add and commit tracked files with a single command by using the -a and -m options.

  git commit -a -m"your commit message here"

How to see changes made before committing them using "diff" in Git:

You can pass a file as a parameter to only see changes on a specific file. git diff shows only unstaged changes by default. We can call diff with the --staged flag to see any staged changes.

  git diff
  git diff all_checks.py
  git diff --staged

How to see your commit history in Git:

This command shows the commit history for the current repository:

  git log

How to see your commit history including changes in Git:

This command shows the commit's history including all files and their changes:

  git log -p

How to see a specific commit in Git:

This command shows a specific commit. Replace commit-id with the id of the commit that you find in the commit log after the word commit.

  git show commit-id

How to remove tracked files from the current working tree in Git:

This command expects a commit message to explain why the file was deleted.

  git rm filename

How to rename files in Git:

  git mv oldfile newfile

How to revert staged changes in Git:

You can use the -p option flag to specify the changes you want to reset.

  git reset HEAD filename
  git reset HEAD -p

How to amend the most recent commit in Git:

git commit --amend allows you to modify and add changes to the most recent commit.

  git commit --amend

How to rollback the last commit in Git:

git revert will create a new commit that is the opposite of everything in the given commit. We can revert the latest commit by using the head alias like this:

  git revert HEAD

How to rollback an old commit in Git:

You can revert an old commit using its commit id. This opens the editor so you can add a commit message.

  git revert comit_id_here

Branch

How to create a new branch in Git:

By default, you have one branch, the main branch. With this command, you can create a new branch. Git won't switch to it automatically – you will need to do it manually with the next command.

  git branch branch_name

How to switch to a newly created branch in Git:

When you want to use a different or a newly created branch you can use this command:

  git checkout branch_name

How to list branches in Git:

You can view all created branches using the git branch command. It will show a list of all branches and mark the current branch with an asterisk and highlight it in green.

  git branch

How to create a branch in Git and switch to it immediately:

In a single command, you can create and switch to a new branch right away.

  git checkout -b branch_name

How to delete a branch in Git:

When you are done working with a branch and have merged it, you can delete it using the command below:

  git branch -d branch_name

How to merge two branches in Git:

To merge the history of the branch you are currently in with the branch_name, you will need to use the command below:

  git merge branch_name

How to abort a conflicting merge in Git:

If you want to throw a merge away and start over, you can run the following command:

  git merge --abort

How to push changes to a remote repo in Git:

When all your work is ready to be saved on a remote repository, you can push all changes using the command below:

  git push

How to pull changes from a remote repo in Git:

If other team members are working on your repository, you can retrieve the latest changes made to the remote repository with the command below:

  git pull

How to pull another branch:

If other team members are working on your repository, you can get the latest changes from another branch with the command below:

  git pull origin branch_name

About

This cheat sheet features the most important and commonly used Git commands for easy reference.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published