Git Cheat Sheet: Commands To Track And Manage Your Project

This blog will help you to learn the basic commands of Git & GitHub that you must know which will help you to maintain the versions of projects and control the progress of projects in an efficient manner.

When you are building a small or big project in a team, Git and GitHub are essential tools for the progressive development of projects. Git is a distributed version control system used to track and manage changes in source code (SCM-source code management), files, documents, and other information when our team members are in the same workspace to collaborate efficiently. On the other hand, GitHub is a hosting platform to host the git repositories online.

Discover the essential Git & Github commands that every developer should know. This hands-on lab provides the most essential and used git commands, tips, and best practices for working.

Difference between Git vs GitHub

GitGitHub
Git is a free and open-source distributed version control tool.GitHub is an internet hosting service for software development and version control. It also has free as well as paid services.
It is a command-line toolIt provides a graphical user interface
Git mainly focuses on code sharing and version control.GitHub mainly focuses on Centralized source code hosting.
Git is maintained by LinuxGitHub is maintained by Microsoft 
Git provides a Git GUI interfaceGitHub provides Github Desktop interface 
Git can be compared with CSV, Azure DevOps Server, Subversion, etc.GitHub can be compared with GitLab, Bit Bucket, etc.

Git Workflow (With Diagram)

Working Directory

Any directory on your local system is known as a working directory. Where you modify your existing files. When you execute the git init command in the present working directory then that directory is tracked by git. Also, The git init command creates a hidden directory called the .git directory. The .git directory contains all tracking information.

Staging area (or indexing)

It is an intermediate step between the working directory and the repository. The staging area allows you to carefully review and modify your changes before committing them. Once all the required changes have been done, to track those files from the working directory execute the git add command. You can stage specific files or specific changes within a file, and Git will only consider the staged changes when you make a commit. Also, use options like git add -p to selectively stage changes within a file.

Local repository

Once the staging is done you can commit staged files to the git repository by using the git commit command. Using the git commit command you have to specify the message (-m) which tells about that commit. Adding commits keeps track of changes as we work. Git considers each commit as a save point and adds a snapshot of the directory that the user can use later to go back if they find any bug or want to go to the previous version of the repository.

Remote Repository

To work as a team multiple users have to work on the project and to perform progressively we upload successful commits to a remote server known as Remote Repositorygit push command is used to upload all commits on the server. We can use remote servers like GitHub, GitLab, Bitbucket, Azure DevOps, etc.

Git Installation and Configuration –

You can install Git on any operating system Windows/Linux/Mac OS X.

To Install Git on Linux:

  1. You can install git using the command apt-get

           $ sudo apt-get update

            $ sudo apt-get install git

Install Git on Windows & on Mac OS:

  1. Download the latest version of Git from here- https://git-scm.com/download/win
  2. Download the latest version of Git from here- https://git-scm.com/download/mac

After the installation open the command prompt and run the following command to configure git and use your username and email.

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

$ git config --global user.email "user_emails@gmail.com"

Git Dictionary

It is difficult for new users to understand git terminology. Let’s have a look at the commonly used terms.

  • Repository – A repository is a storage space where your project and its version history are stored.
  • Commit – It is a snapshot of your project at a specific point in time. It captures and adds the latest changes to the repository.
  • Branch – It is a second or alternative copy of the project which enables the user to work in different separate environments.
  • Main – It shows the default branch in the repository 
  • Origin – It is a shorthand name for remote repository from which it is cloned from
  • Merge – Merging combines changes from different branches into a single branch
  • Remote – The repository is hosted on a different server, on a remote location.And this remote repository is basically used for collaboration or backup purposes.
  • Clone – Copying remote repository to the local machine.
  • Fetch – To retrieves the latest changes from a remote repository without merging them into your local repository.
  • Pull – Pulling refers to fetching and merging changes from a remote repository into your local repository.
  • Push – To upload local repository content to a remote repository 
  • HEAD – It shows the last commit in the current branch. When you switch branches with the git checkout command, the HEAD is pointing to the new switched branch.
  • HEAD^ – It shows the parent of the head.The caret ”^” symbol is used on merge commits. Merge commits are special commits that happen when combining changes from multiple branches. They can have more than one parent, representing the branches being merged.
  • HEAD~4 – It shows the great-great-grandparent of the HEAD, the tilde symbol “~” is used to reference commits relative to a given commit. It allows you to navigate and reference previous commits based on their position in the commit history. The number following the tilde “~” indicates the number of commits you want to go back in history.

Git cheat sheet

For quick reference, review the below table.

IndexCommandUse
1git configTo configure git on your system This command is used. also, you can use the –global option to configure git locally or globally.
2git initTo initialize Git in your repository.
3git addTo add files in the staging area (git add . = to add all files).
4git statusTo check the status of the current repository and also to display the files that you have changed.
5git commitTo commit all staged files to git.
6git logTo see a list of all commits made on the branch or repository.
7git cloneTo make a copy of the Git repository from a remote source.
8git branch To create or list all branches (local), also you can use the -r flag to see remote branches, and -a to see all branches.
9git checkoutTo create and switch to a new branch.
10git fetch To download the latest changes into the local repository.
11git pullTo update the local repository to the newest commit.
12git pushTo push/upload local repository content to a remote repository.
13git diffTo see the changes made in the files before committing them.
14git mergeTo merge the branch into an active branch.
15git rebaseTo transfer your complete work from one branch to another branch.
16git rmTo remove or to delete tracked files from the working directory and git index.
17git revert To create a new commit using the previous commit means reverting the changes from the specified commit.
18git resetTo move or to undo and to move the repository to the previous commit.
19git stashTo store untracked files on a temporary basis.
20git remoteUsed to perform different operations on git repo.

The commands listed below are some of the most useful and most frequently used Git commands.

git config

git config command is used to configure your name and email address in Git.

global flag: The global flag is used to set the same username and email for every repository on your computer. When you don’t specify the –global flag the username & email is set only for that current repository. 

The git config command configures Git on your PC using your username.

Syntax

git config --global user.name "user_name"

Example

Replace “Your Name” with the actual User name

git config --global user.name "Your Name"

To configure Git on your PC using your email.

Syntax

git config --global user.name "valid_email"

Example

Replace “youremail@example.com” with your actual email address.

git config --global user.email "youremail@example.com"

If you want to set the configurations for a specific repository only, you can omit the –global flag and run the command within the repository’s directory.

You can verify the configurations you set by running the following command:

git config --global --list

This will list all the global Git configurations, including your name and email address.

To get more information you can use the git config –help command.

git init

Syntax

git init

To initialize the existing directory as Git Repository you can use the git init command. The git init command creates a hidden directory called the .git directory. The .git directory contains all tracking information such as commit history, branch information, and configuration settings. It’s important to note that this directory should not be modified or deleted manually, as it holds the integrity of the repository.

steps involved in initializing a Git repository:

  1. Open your terminal or command prompt and navigate to the desired directory where you want to initialize the Git repository.
  2. Run the following command to initialize the repository:
    git init
  3. Once the repository is initialized, you can start tracking files and making commits. Before doing so, it is essential to configure your identity using the following commands:
    git config –global user. name “Your Name”
    git config –global user.email “youremail@example.com”

To track files within the repository, you need to add them to the staging area. This can be done using the “git add” command as explained below.

To get more information you can use the git init –help command.

git add

Syntax

git add [file]

git add command is used to add files to the staging area. As we add files to the staging area, then git tracks those files/folders. By using the git add command you can add single or multiple files at a time.

To add all files from the repository, you can use the -A option or the dot “.” option.

For example

git add -A

Or

git add .

To get more information you can use the git add –help command.

git status

Syntax

git status [<option(s)>[--][<pathspec>]

The git status command is used to see the status of the repository and staging area. This command shows the status of tracked files, untracked files, changed files, and files that are ready to commit.

For example ,To see the status of before git add and after git add command from the working directory 

git status

To get more information you can use the git status –help command.

git commit

Syntax-

git commit <option(s)> "[descriptive message]"

git commit command fetches updates from the staging area to the repository. The git commit command takes a snapshot of the project to track the project’s progress from the last commit. we can recall or revert the commits to their older version as well. For each commit, the git commit command creates commit-id which helps us to track each commit. You can use the -m option along with the git commit command to give the message at commit time.

Example-  

git commit -m "added message in test.txt file"

To get more information you can use the git commit –help command.

git log

Syntax-

git log [-n count]

To view the recent commit history of the repository git log command is used.

The git log command displays the following metadata.

Commit– it shows a unique commit-id generated after each commit.

Author– It shows information about the author such as email & name

Date– It shows the date & time of the commit.

Message– In the end section it shows the commit message

Example-

git log

To get more information you can use the git log –help command.

git clone

Syntax- 

git clone <repo URL> 

git clone is a command line utility used to clone/copy the remote repositories to the local system also you can sync between the two locations. You can clone local to remote or vice versa. The git clone command assesses the repository through a remote URL(i.e. origin).

The git clone command also makes a copy of a specific branch from the repository. To make a clone/copy of a specific branch you have to use the -b option.

Syntax To clone a specific Branch-

git clone -b <Branch name> <Repository URL>  

Example- To clone the repository from remote to local.Repository link : https://github.com/cloudyuga/git-gh-course

git clone https://github.com/<User Name>/Git_cloudyuga_course.git

To get more information you can use the git clone –help command.

git branch

Syntax-

git branch <option(s)> [branch name]

To create a new branch i.e. separate or new version of the main repository the git branch command is used. The “*” symbol indicates the current branch you are in. Creating a new branch of the project allows us to work in your own workspace or working directory without impacting the main branch(main repository). As soon as you complete your work on the new branch you can merge it into the main branch. we can perform multiple operations such as renaming branches, switching, and comparing branches, etc.

Example- 

git branch branch1 

To delete the merged branch you can use the -d option with the git branch command or to delete the merged or unmerged branch you can use the -D option with the git branch.

Example- 

git branch -d branch1

(Note– you can use the git branch command or git branch – list command to see available branches in the repository.) 

To get more information you can use the git branch –help command.

git checkout

Syntax-

git checkout <option(s)> [branch_name]

To switch from one branch to another git checkout command is used. git checkout command is used to create and switch, and to restore files as well. The git checkout command basically works on 3 main entities such as files, branches, and commits.

The git checkout command is also used to create a new branch and simultaneously switch to that branch. we can use the -b option to create a new branch and switch on to that.

Syntax- 

git checkout -b [branch name]

Example- 

git checkout -b branch1

To get more information you can use the git checkout –help command.

git fetch

Syntax-

git fetch 

To download only the latest changes from the main repository (remote) that other users pushed to the remote repository to the local repository for that purpose git fetch command is used. But the git fetch command will not perform a merge on your local branch. 

We can use the git fetch command for different scenarios such as 

1)If you want to fetch the remote repository you can use

git fetch< repository Url> 

2)If you want to fetch the specific branch from the repository you can use

git fetch <branch URL><branch name>

3)If you want to fetch all branches simultaneously from the remote repository you can use

git fetch -all

4)If you want to fetch updated data from the remote repository to your local repository you can use

git fetch origin

For example- To fetch from a specific URL

git fetch https://github.com/cloudyuga/git-gh-course.git

To get more information you can use the git fetch –help command.

git pull

Syntax-

git pull <option(s)> [<repository URL><refspec>...]  

This command is equivalent to

git fetch origin head

To pull/fetch the latest changes from the remote repository and immediately merge them into the local repository git pull command is used. The git pull command is a combination of git fetch+git merge commands.

Example- To pull the new file and update it into the local repository.
Replace the User Name with your name.

git pull https://github.com/<User Name>/git-gh-course.git

To check any changes you can use the following command

git pull <RemoteName> <BranchName>

To get more information you can use the git pull –help command.

git push

Syntax-

git push <option> [<Remote URL><branch name><refspec>...]  

To push/upload local repository content to a remote repository you can use the git push command. It basically transmits local repository commits to the remote repository. While using the git push command you have to specify the location of the repository else git considers the origin master as a default location.

Example- 

To get more information you can use the git push –help command.

git diff

Syntax-

git diff [<options>] [<commit>] [--] [<path>...​]

To see the changes made in the files before committing them using the git diff command. git diff command executes the diff function on git data. By default, the git diff command shows only unstaged changes. But if you want to see the staged changes you have to pass –the staged flag to the git diff command. You can use the git diff command to see changes between files, branches, commits, working tree, etc.

For example- Suppose If you want to track changes that have staged but not committed you can use the git diff –staged command.

git diff --staged

To get more information you can use the git diff –help command.

git merge

Syntax-

git merge <query>

Most of the time to merge one branch i.e. current branch into another i.e. target branch, the git merge command is used. The commit history of merged branches will be combined and organized into a single history. The git merge command can be used in different scenarios such as – 

1) To merge a specific commit in the active branch we can use

git merge <commit> 

2) To merge commits in the master branch

git checkout master

3) To merge the whole branch into another

git merge <branch_name>

For example- if we want to merge a branch 

git merge branch1

To get more information you can use the git merge –help command.

git rebase

Syntax-

git rebase <branch name>

To transfer your complete work from one branch to another branch git rebase command is used. Eventually, rebase combines a sequence of commits to a new base commit. It is an alternative to the git merge command. It is good to rebase your branch before merging it.

The git rebase is mainly used for the following reasons such as-

1)With rebase you can maintain a linear project history.

2)In rebase you basically disconnect a branch and reconnect it with the tip of another branch(main repository).

3)To get commit from all feature branch and add them to the main branch. 

For example- To rebase a branch you can use the following command

git rebase main

To get more information you can use the git rebase –help command.

git rm

git rm <option(s)> <file name>

To remove or to delete tracked files from the working directory and git index git rm command is used. To delete files forcefully you can use the -f option as well.

For example- To remove files from the working directory and not track them by git you can use the following command.

git rm a.txt

For example, supposed You have removed the file by the normal command “rm” You can see the file is still in a staging area and is still available in the repository. we can get those deleted files by first adding them and then committing them.

rm pull.txt

Also if you want to remove files from the remote repository but keep the files in your local repository you can use the cached option such as git rm -cached <file name>

To get more information you can use the git rebase –help command.

git revert

Syntax-

git revert

It is a undo type of command. When you want the previous commit to add it as a new commit you can use the git revert command. It creates a new commit without deleting that previous commit data. git revert allows operations such as editing, cleanup, etc. 

git revert --no-edit commit-id

For example- If you want to revert the commit you have to know the commit id. To know the commit id you can use the git log –oneline command (oneline option is used to give output in one line)

git revert --no-edit baec440

Here, in the above example –no-edit option is used to perform the revert operation without opening the editor.

To get more information you can use the git revert –help command.

git reset

Syntax-

git reset

To undo changes means When you want to move the repository back to the previous commit and remove the changes made after that commit you can use the git reset command. The git reset has 3 modes such as –

  1. Soft 
  2. Mixed
  3. Hard

Basically, the git reset command resets the current state of HEAD to the specified state.

1) git soft reset-

Syntax-

git reset -soft <commit-sha> 

The –soft flag is used with the git reset command to change the position of HEAD(updating reference pointer)  to the particular commit and all changes made after the HEAD commit are stored in the staging area. With the –soft flag user can decide what needs to be saved/remove from other commits.

2) git Mixed reset-

Syntax-

git reset <commitID> # or git reset --mixed <commitID> 

The –mixed flag is a default option if we did not specify any option to reset the command. Used to change the position of HEAD(updating reference pointer) to the particular commit and all changes made after the HEAD commit are stored in the unstaged area. The undone changes are transferred to the working directory

3) git Hard reset-

Syntax-

git reset --hard <commitID>

The –hard flag is used with the git reset command to change the position of HEAD(update reference pointer) to the particular commit and all changes made after the HEAD commit are removed i.e. it removes commit history. It is one of the most direct, unsafe, and rarely used option.

For example- if you wanted to reset by using –the hard flag

git reset --hard ba5d295

To get more information you can use the git reset –help command.

git stash

Syntax- 

git stash

When you want to save the unfinished, uncommitted work temporarily you can use the git stash command so you can record the current state of the working directory and index so you can work on that later. stash helps you to save uncommitted files in the branch. The git stash command enables you to switch branches without committing the current work. 

When you use the git stash command to store changes temporarily so those changes are stored in a stash list on your local machine.the stash list work in LIFO (Last In First Out) manner which means the most recent changes you stashed will be applied first. We can use the git stash pop command to work on stashed files.

For example – if you want to stash some untracked files (files that are not in the staged area) we have to use the -u flag with the stash command.

git stash save -u

If you want to delete all saved stashes use

git stash clear

To get more information you can use the git stash –help command.

git remote

Syntax –

git remote <option(s)>

It is a remote repository that is a shared repository which is stored/hosted on a different cloud server, often used for collaboration or backup purposes. The user can perform many operations on a remote server such as clone, push, pull, fetch, and many more. To check the configuration of the remote server you can use the git remote command. You can perform different operations using git remote such as creating, viewing, and deleting connections to other repositories.

If you want to see the name of the remote server you can use or want to see the repository which is cloned use the following command.

git remote

The above command gives you the remote name as an origin. Origin is the default name for the remote server given by git.

To see available git remote connections with their URL

git remote -v

Here -v stands for verbose

git remote add –

To add another user repository as remote with a short name you can use

Syntax –

git remote add <short_name><remote_URL>
git remote add cloudyuga https://github.com/cloudyuga/git-gh-course.git

You can see in the above example I have added remote repository short name as “cloudyuga”.Now you can use this short name on the command line by replacing it with the whole URL.

For example, If you want to pull a repository 

git pull <short name>
git pull cloudyuga

git remote remove –

Same as add to remove remote repository you can use the rm/remove option with git remote.

Syntax-

git remote rm <destination>  

or

git remote remove <destination>  
git remote rm cloudyuga

Git remote upstream – 

If you wanted to synchronize the central repository with the local repository i.e. If you wanted to directly update our local repository when any changes are done in central/remote repository you have to set up the central repository as an upstream remote repository.

Syntax –

git remote add upstream <repo-url> 
git remote add upstream https://github.com/<User Name>/Git_cloudyuga_course.git

As you can see you can collect the latest changes of the upstream repository with fetch. Repeat this every time when you want to get the latest updates.

To get more information you can use the git remote –help command.

Conclusion

Understanding git is essential for any developer who wants to work on collaborative projects, manage versions of their codebase, and keep track of changes made by developers which is resulting in the fast development of projects without any kind of error or conflict. This git sheet lab provides a quick view of git basic commands which saves your time with a basic understanding of commands such as git init, add, commit, clone, etc.

Join Our Newsletter

Share this article:

Table of Contents