Git is an independent version control system that helps users while tracking changes in the project source code over time. On Git, tasks can be performed through a bunch of commands which are available for multiple purposes like Git configurations, initializing repository, creating, deleting, updating, removing codes files, adding, switching, removing branches, for building connections between the Git remote repository and Git local repository, and many more operations.
In this manual, we will explain the Git commands cheat sheet. So, let’s start!
Git Commands Cheat Sheet
When users are working on Git, they utilize different Git commands that can help to perform the required tasks with ease. We have listed Git commands in a sequence that are started from basic commands in the below-provided table.
Let’s have a look at Commands with descriptions one by one!
Git Basic Commands
Commands |
Description |
$ git init <repository-name> |
This command is utilized for initializing the Git current directory as a git repository |
$ git config user.name <username> |
To configure the username that will be utilized for all commits in the current repository |
$ git add <directory> or <file> |
To add all changes in directory or files for the next commit |
$ git commit -m “<message>” |
To commit the staged changes with commit message to repository |
$ git status |
To show the staged and untracked files |
$ git log |
To list the entire commit history utilizing the default format |
$ git diff |
To view the unstaged changes between the working directory and index |
$ git clone <repository> |
To clone the repository which can be located on the filesystem or on a remote machine |
Git Branches
Command |
Command |
$ git branch |
To list of all branches of repository |
$ git checkout -b <branch> |
To create and switch a new branch immediately |
$ git merge <branch> |
To merge the specified branch with current branch |
Git Undoing Changes
Command |
Description |
$ git revert <commit> |
To create the new commit that revert all the changes made in <commit> |
$ git reset <file-name> |
To delete the file from staging area and leave the working area unchanged |
$ git clean -n |
To display which file would be removed from working directory |
Rewriting Git History
Command |
Description |
$ git commit –amend -m “<message>” |
To replace or change the last commit message with the new one with unstaged changes |
$ git rebase <base> |
The current branch will be rebased onto <base>. Here, <base> can be a commit ID, a tag, branch name, or relative references to HEAD |
$ git reflog |
To view the log of changes to the Git local repository’s HEAD |
Remote Repositories
Command |
Description |
$ git remote add <repo-url> |
To create a new connection of the local repository with the remote repository, this command is useful. Here, <repo-url> will be replaced with the remote repository URL. |
$ git fetch <remote> <<branch> |
It fetches the specified remote branch from the remote repository. |
$ git pull <remote> |
To fetch the copy of specified remote branches and merge it immediately into the local copy |
$ git push <remote> <branch> |
It is utilized to push the specific branch to the remote repository, along with commit history and create a branch in the remote repository if it doesn’t exist |
Git Config
Command |
Description |
$ git config –global user.name <user name> |
Used to configure username for all commits by specific user |
$ git config –global user.email <user-email> |
To config the author email that will be utilized for all commits by specific user |
$ git config –global alias. <alias-name> |
To create shortcut for a Git command |
$ git config –system core.editor <editor> |
To set the text editor as default editor which is used by commands for all users |
$ git config –global –edit |
To open the global file in the set text editor for editing manually |
Git Pull
Command |
Description |
$ git pull –rebase <remote> |
To fetch the rebases and remote’s copy of current branch into the local copy |
Git Push
Command |
Description |
$ git push <remote> –force |
Utilized for force push as if results in non-fast-forward merge |
$ git push <remote> –all |
It will push all Git local branches to the specific Git remote repository |
$ git push <remote> –tags |
To push the tags because tags are not automatically pushed when we push a branch |
Git Log
Command |
Description |
$ git log -<limit> |
Used to show limited number of commits |
$ git log –oneline |
List each commit in single line |
$ git log -p |
It shows the difference of each commit |
$ git log –author=“<pattern>” |
Used to search the commit by a particular username |
$ git log <since>..<until> |
Used to display commits that appear between <since> and <until> |
$ git log — <file-name> |
Only lists the commit of specified file |
$ git log –grep=“<pattern>” |
Used to search for commits with a commit message that matches <pattern> |
Git Reset
Command |
Description |
$ git reset |
Used to reset the staging area to match the recent commit but the working directory will remain unchanged |
$ git reset –hard |
To reset the staging area as well as a working directory to match recent commit and all made changes in the working directory |
$ git reset <commit> |
Utilized for moving the current branch backward to commit, and reset the staging. However, the working directory will be alone |
$ git reset –hard <commit> |
The working of this command is like the previous command, however, it will reset the staging area as well as the working directory. Removes the uncommitted changes and all commits after provided |
Git Diff
Command |
Description |
$ git diff HEAD |
Used to display the difference between the last commit and the working directory |
$ git diff –cached |
Utilized to show the difference between the last commit and staged changes |
Git Rebase
Command |
Description |
$ git rebase -i <base> |
Used to rebase current branch onto <base> and launch an editor to specify commands for how each commit will be moved to the base |
That’s it! We have elaborated on the detailed Git commands cheat sheet with a description.
Conclusion
Git has numerous commands to perform multiple operations such as Git configurations, initializing repository, creating, deleting, updating, removing codes files, adding, switching, removing branches, building connections between the Git remote repository and Git local repository, pull and push repositories with branches and many more. This manual demonstrated the Git commands cheat sheet with description.