In the world of software development, mastering version control is also become important skill. And being a developer, having a good knowledge of git is also important. Whether you’re a full time developer, part time developer or just starting on your coding journey, understanding Git is crucial for efficient collaboration, seamless project management, and version history maintenance.
To make your Git journey smoother and get the git overview in short time, we’ve curated a comprehensive Git Command Cheat Sheet that contains the fundamental commands. This cheat sheet is not just a reference guide, it’s a roadmap designed to empower developers at every level to navigate the Git landscape with confidence.
Lets start learning git.
1. Git Core
1.1 git init
Git init is a fundamental command to initializes a new Git repository. When this command is executed in directory, it sets up the necessary data structures and files for start tracking the changes. Also it start managing the version control for the project in that directory.
git init
1.2 git clone
Clones a repository into a new directory. git clone command fetches an entire Git repository from a specified URL and creates a local copy on your machine.
git clone <repository_url>
#For example
git clone https://github.com/example/repo.git
1.3 git add
git add command adds the changes or we can say modified file from working directory to the staging area. And this added file will be now available in the next commit.
git add <file>
#to add all the modified file at once
git add .
1.4 git commit
git commit is used to records the staged changes to the repository.
git commit -m "Commit message"
1.5 git status
git status command shows the status of changes as untracked, modified, or staged.
git status
1.6 git diff
Git diff command shows the changes between commits, commit and working tree, etc.
git diff
1.7 git checkout
Using git checkout you can switch between the branchs or restores working tree files.
git checkout <branch_name>
1.8 git reset
git reset command basically reset the current HEAD to the specified state. In simple words we can explain it as it is used to undo the changes in your working directory and get back to a specific commit while discarding all the commits made after that one.
git reset <commit_hash>
1.9 git log
Git log command print the all commit logs.
git log
1.10 git show
Git show command display the information about a given commit, tag, or object.
git show <commit_hash>
1.11 git tag
Creates, lists, or deletes tags.
git tag -a v1.0 -m "Version 1.0"
1.12 git push
git push command helps to push the changes to a remote repository. It can be done after git add and git commit.
git push origin <branch_name>
1.13 git pull
git pull command basically pull the changes from remote repository to your local branch. It is a combination of git fetch and git merge.
git pull origin <branch_name>
2. Branching
2.1 git branch
git branch command is use to lists, creates, or deletes branches.
#to list all the branch
git branch
#to create new branch
git branch <branch_name>
#switch branch
git checkout <branch_name>
#create and switch to branch
git checkout -b <new_branch_name>
#delete local branch
git branch -d <branch_name>
#force delete local branch
git branch -D <branch_name>
2.2 git checkout -b
Creates a new branch and switches to it.
git checkout -b <new_branch>
2.3 git merge
git merge command merge the two branches, it means integrate the changes from one branch into another.
git merge <branch_name>
2.4 git rebase
git rebase
command in Git is a powerful tool for rewriting commit history by moving or combining commits. Unlike git merge
, which integrates changes from one branch into another, git rebase
reorganizes the commit history by applying commits from one branch onto another.
git rebase <branch_name>
2.5 git branch –set-upstream-to
Sets up a tracking relationship with a remote branch.
git branch --set-upstream-to=origin/<remote_branch> <local_branch>
2.6 git branch –unset-upstream
Unsets the upstream information.
git branch --unset-upstream
2.7 git cherry-pick
git cherry-pick command is used to applies changes from some existing commits to current branch. It allows you to pick and choose individual commits by their commit hash from one branch and apply them to another.
git cherry-pick <commit_hash>
3. Merging
3.1 git merge
git merge
command used to integrate changes from one branch to another branch. When we perform merge operation, it creates a new commit that reflects the combined history.
git merge <branch_name>
3.2 git rebase
git rebase
command in Git is a powerful tool for rewriting commit history by moving or combining commits. Unlike git merge
, which integrates changes from one branch into another, git rebase
reorganizes the commit history by applying commits from one branch onto another.
git rebase <branch_name>
4. Stashing
4.1 git stash
git stash command basically removes the all uncommit changes till to the last commit and creates a new stash and reverts your working directory there. Stashing is useful when you want to store your modifications temporarily from current working directory that is unsaved and apply them later.
git stash
4.2 git stash pop
git stash pop command is used to restores the most recently stashed changes and remove it from the stash list. It’s a combination of git stash apply
and git stash drop
.
git stash pop
4.3 git stash list
Lists all stashes.
git stash list
4.4 git stash apply
git stash apply
command in Git is used to apply changes from a stash to the current working directory. Unlike git stash pop
, it doesn’t automatically remove the applied stash from the stash list.
git stash apply <stash_name>
4.5 git stash drop
git stash drop
command is used to remove a specific stash from the stash list.
git stash drop <stash_name>
5. Remotes
5.1 git remote
Shows remote repositories.
git remote
5.2 git remote add
git remote add
command in Git is used to add a new remote repository.
git remote add origin <repository_url>
5.3 git remote remove
Removes a remote repository.
git remote remove origin
5.4 git fetch
git fetch
command retrieve the changes from a remote repository without merging them into your local branch.
git fetch origin
5.5Â git pull
git pull command basically pull the changes from remote repository to your local branch. It is a combination of git fetch and git merge.
git pull origin <branch_name>
5.6 git push
git push
command is used to push the local changes to a remote repository.
git push origin <branch_name>
5.7 git clone –mirror
git clone --mirror
command is used to create a bare, mirrored clone of a Git repository. A mirrored clone includes all remote branches, tags, and refs, and it’s an exact copy of the original repository, suitable for creating a backup or setting up a remote repository that stays in sync with the source repository.
git clone --mirror <repository_url>
6. Configuration
6.1 git config
git config command is used for gets and sets repository or global options. It allows you to customize various aspects of Git behavior, such as user information, default settings, and more.
git config --global user.name "Your Name"
6.2 git global config
git global config is used to sets global configuration options global configuration that is user-specific settings. It apply across all repositories on a particular machine for a given user.
git config --global user.email "your.email@example.com"
6.3 git reset config
Resets configuration to default settings.
git config --unset --global user.email
7. Plumbing
The commands listed under “Plumbing” in Git are lower-level commands that provide direct access to Git’s internal object model and mechanisms. They are generally not used directly by most users but are valuable for scripting and understanding Git’s internals.
7.1 git cat-file
Displays the content or type of an object.
git cat-file -p <object_id>
7.2 git checkout-index
Copies files from the index to the working directory.
git checkout-index --all
7.3 git commit-tree
Creates a new commit object with specified parameters.
git commit-tree -m "Commit message" <tree_id>
7.4 git diff-tree
Compares the content and mode of blobs found via two tree objects.
git diff-tree <commit_id1> <commit_id2>
7.5 git for-each-ref
Outputs information on each ref.
git for-each-ref --format '%(refname) %(objectname)'
7.6 git hash-object
Computes the object ID value for a given file.
git hash-object -w <file>
7.7 git ls-files
Lists files in the index and the working directory.
git ls-files
7.8 git ls-remote
Shows references in a remote repository.
git ls-remote origin
7.9 git merge-tree
Show three-way merge without touching index.
git merge-tree <base> <branch1> <branch2>
7.10 git read-tree
Reads tree information into the index.
git read-tree <tree_id>
7.11 git rev-parse
Parses and outputs Git revisions.
git rev-parse HEAD
7.12 git show-branch
Shows the branches and their commits.
git show-branch
7.13 git show-ref
Displays heads and tags along with the object ID.
git show-ref
7.14 git symbolic-ref
Read or modify symbolic refs.
git symbolic-ref HEAD refs/heads/branch_name
7.15 git tag –list
List tags matching given pattern.
git tag --list "v1.*"
7.16 git update-ref
Updates the object name stored in a ref safely.
git update-ref refs/heads/branch_name <commit_id>
8. Porcelain
In git, Porcelain is used to refer to high-level commands that are user-friendly. It provides more abstract and convenient way to interact with the version control system. The counterpart to porcelain is “plumbing,” which refers to low-level commands that provide more fine-grained control and are often used by scripts or other Git commands.
8.1 git blame
Shows what revision and author last modified each line of a file.
git blame <file>
8.2 git bisect
Helps find the commit that introduced a bug using binary search.
git bisect start
git bisect good <commit>
git bisect bad <commit>
8.3 git checkout
Switches branches or restores working tree files.
git checkout <branch_name>
8.4 git commit
Records changes to the repository with a custom message.
git commit -m "Commit message"
8.5 git diff
Shows changes between commits, commit and working tree, etc.
git diff
8.6 git fetch
Fetches changes from a remote repository.
git fetch origin
8.7 git grep
Searches the working directory for a regex pattern.
git grep "pattern"
8.8 git log
Displays commit logs.
git log
8.9 git merge
Merges branches.
git merge <branch_name>
8.10 git push
Pushes changes to a remote repository.
git push origin <branch_name>
8.11 git rebase
Reapplies commits on top of another base tip.
git rebase <branch_name>
8.12 git reset
Resets the current HEAD to the specified state.
git reset <commit_hash>
8.13 git show
Shows information about a commit, tag, or object.
git show <commit_hash>
8.14 git tag
Creates, lists, or deletes tags.
git tag -a v1.0 -m "Version 1.0"
These commands provide a more user-friendly interface compared to the lower-level “Plumbing” commands. They are designed for day-to-day use and cover a wide range of version control tasks, making them essential tools for Git users.
9. Alias
In Git, Alias allow developers to create shortcuts or abbreviations for commonly used Git commands. Git aliases can improve productivity by reducing the amount of typing needed for frequently performed operations. You can define aliases for both existing Git commands and custom command sequences.
9.1 git config –global alias
Creates a shortcut or alias for a Git command.
git config --global alias.co checkout
10. Hook
10.1 git config –local core.hooksPath
Git hooks are scripts that Git executes before or after specific events occur in the Git workflow, such as committing, merging, or pushing. These hooks allow developers to automate or customize various aspects of their Git workflow. Git provides both client-side and server-side hooks.
git config --local core.hooksPath /path/to/hooks
Remember to replace placeholders like <branch_name>, <commit_hash>, <repository_url>, etc., with actual values in the examples.