DEV Community

Srijan Karki
Srijan Karki

Posted on

Git Cheat Sheet: Essential Commands for Version Control

Git is a powerful version control system that allows developers to track changes, collaborate on projects, and manage their code efficiently. This cheat sheet provides a quick reference to the most commonly used Git commands, helping you navigate through your version control tasks with ease.

Getting Started

Create a New Repository

To start a new project with Git, you can initialize a new repository:

$ git init [project name]
Enter fullscreen mode Exit fullscreen mode

If you want to clone an existing repository, use:

$ git clone git_url
Enter fullscreen mode Exit fullscreen mode

To clone a repository into a specified directory:

$ git clone git_url my_directory
Enter fullscreen mode Exit fullscreen mode

Making Changes

Check Status

To see which files have been modified and staged for your next commit:

$ git status
Enter fullscreen mode Exit fullscreen mode

Stage Changes

To stage a specific file:

$ git add [file]
Enter fullscreen mode Exit fullscreen mode

To stage all changed files:

$ git add .
Enter fullscreen mode Exit fullscreen mode

Commit Changes

To commit all staged files with a message:

$ git commit -m "commit message"
Enter fullscreen mode Exit fullscreen mode

To commit all tracked files (including those that are not staged) with a message:

$ git commit -am "commit message"
Enter fullscreen mode Exit fullscreen mode

Discard Changes

If you want to discard changes in your working directory that are not staged:

$ git restore [file]
Enter fullscreen mode Exit fullscreen mode

To unstage a staged file:

$ git restore --staged [file]
Enter fullscreen mode Exit fullscreen mode

To unstage a file but keep the changes:

$ git reset [file]
Enter fullscreen mode Exit fullscreen mode

Revert Changes

To revert everything to the last commit:

$ git reset --hard
Enter fullscreen mode Exit fullscreen mode

View Differences

To see the differences between your working directory and the last commit:

$ git diff
Enter fullscreen mode Exit fullscreen mode

To see the differences between staged changes and what is yet to be committed:

$ git diff --staged
Enter fullscreen mode Exit fullscreen mode

Rebase

To apply any commits of the current branch ahead of a specified one:

$ git rebase [branch]
Enter fullscreen mode Exit fullscreen mode

Configuration

Set User Information

To set the name that will be attached to your commits and tags:

$ git config --global user.name "name"
Enter fullscreen mode Exit fullscreen mode

To set an email address for your commits:

$ git config --global user.email "email"
Enter fullscreen mode Exit fullscreen mode

Customize Git Output

To enable colorization of Git output:

$ git config --global color.ui auto
Enter fullscreen mode Exit fullscreen mode

To edit the global configuration file in a text editor:

$ git config --global --edit
Enter fullscreen mode Exit fullscreen mode

Working with Branches

List Branches

To list all local branches:

$ git branch
Enter fullscreen mode Exit fullscreen mode

To list all branches, both local and remote:

$ git branch -av
Enter fullscreen mode Exit fullscreen mode

Switch Branches

To switch to a specific branch:

$ git checkout my_branch
Enter fullscreen mode Exit fullscreen mode

To create and switch to a new branch:

$ git checkout -b new_branch
Enter fullscreen mode Exit fullscreen mode

Delete a Branch

To delete a branch:

$ git branch -d my_branch
Enter fullscreen mode Exit fullscreen mode

Merge Branches

To merge branchA into branchB:

$ git checkout branchB
$ git merge branchA
Enter fullscreen mode Exit fullscreen mode

Tagging

To tag the current commit:

$ git tag my_tag
Enter fullscreen mode Exit fullscreen mode

Observing Your Repository

View Commit History

To show the commit history for the currently active branch:

$ git log
Enter fullscreen mode Exit fullscreen mode

To see the commits on branchA that are not on branchB:

$ git log branchB..branchA
Enter fullscreen mode Exit fullscreen mode

File-Specific Logs

To show the commits that changed a specific file, even across renames:

$ git log --follow [file]
Enter fullscreen mode Exit fullscreen mode

Compare Branches

To see the differences between branchA and branchB:

$ git diff branchB...branchA
Enter fullscreen mode Exit fullscreen mode

Show an Object in Human-Readable Format

To display any object in Git in a human-readable format:

$ git show [SHA]
Enter fullscreen mode Exit fullscreen mode

Synchronize

Fetch Changes

To fetch all branches from a remote repository:

$ git fetch [alias]
Enter fullscreen mode Exit fullscreen mode

Merge Changes

To merge a remote branch into your current branch:

$ git merge [alias]/[branch]
Enter fullscreen mode Exit fullscreen mode

No Fast-Forward

To prevent fast-forwarding during the merge:

$ git merge --no-ff [alias]/[branch]
Enter fullscreen mode Exit fullscreen mode

Only Fast-Forward

To allow only fast-forward merges:

$ git merge --ff-only [alias]/[branch]
Enter fullscreen mode Exit fullscreen mode

Push Changes

To push local branch commits to a remote repository:

$ git push [alias] [branch]
Enter fullscreen mode Exit fullscreen mode

Pull Changes

To fetch and merge any commits from the remote branch you're tracking:

$ git pull
Enter fullscreen mode Exit fullscreen mode

Cherry-Pick Commits

To merge just one specific commit from another branch:

$ git cherry-pick [commit_id]
Enter fullscreen mode Exit fullscreen mode

Working with Remotes

Manage Remotes

To add a remote repository URL as an alias:

$ git remote add [alias] [url]
Enter fullscreen mode Exit fullscreen mode

To show the names of the remote repositories you've set up:

$ git remote
Enter fullscreen mode Exit fullscreen mode

To show the names and URLs of the remote repositories:

$ git remote -v
Enter fullscreen mode Exit fullscreen mode

To remove a remote repository:

$ git remote rm [remote repo name]
Enter fullscreen mode Exit fullscreen mode

Change Remote URL

To change the URL of the remote repository:

$ git remote set-url origin [git_url]
Enter fullscreen mode Exit fullscreen mode

Temporary Commits

Stash Changes

To save modified and staged changes temporarily:

$ git stash
Enter fullscreen mode Exit fullscreen mode

To list stashed changes:

$ git stash list
Enter fullscreen mode Exit fullscreen mode

To apply changes from the top of the stash stack:

$ git stash pop
Enter fullscreen mode Exit fullscreen mode

To discard changes from the top of the stash stack:

$ git stash drop
Enter fullscreen mode Exit fullscreen mode

Tracking Path Changes

Remove Files

To delete a file from the project and stage the removal:

$ git rm [file]
Enter fullscreen mode Exit fullscreen mode

Move Files

To change an existing file path and stage the move:

$ git mv [existing-path] [new-path]
Enter fullscreen mode Exit fullscreen mode

View File History

To show all commit logs with an indication of any paths that moved:

$ git log --stat -M
Enter fullscreen mode Exit fullscreen mode

Ignoring Files

A .gitignore file specifies intentionally untracked files that Git should ignore. Here are some examples:

/logs/*
!logs/.gitkeep
.DS_store
node_modules
.sass-cache
Enter fullscreen mode Exit fullscreen mode

Git Tricks

Rename Branch

To rename the current branch:

$ git branch -m <new_name>
Enter fullscreen mode Exit fullscreen mode

Push and Reset

To push and set the upstream branch:

$ git push origin -u <new_name>
Enter fullscreen mode Exit fullscreen mode

Delete Remote Branch

To delete a remote branch:

$ git push origin --delete <old>
Enter fullscreen mode Exit fullscreen mode

View Logs

To search for a change by content:

$ git log -S'<a term in the source>'
Enter fullscreen mode Exit fullscreen mode

To show changes over time for a specific file:

$ git log -p <file_name>
Enter fullscreen mode Exit fullscreen mode

To print out a visualization of your log:

$ git log --pretty=oneline --graph --decorate --all
Enter fullscreen mode Exit fullscreen mode

Branch Management

To list all branches and their upstreams:

$ git branch -vv
Enter fullscreen mode Exit fullscreen mode

To quickly switch to the previous branch:

$ git checkout -
Enter fullscreen mode Exit fullscreen mode

To get only remote branches:

$ git branch -r
Enter fullscreen mode Exit fullscreen mode

To checkout a single file from another branch:

$ git checkout <branch> -- <file>
Enter fullscreen mode Exit fullscreen mode

Rewriting History

Rewrite Last Commit Message

To rewrite the last commit message:

$ git commit --amend -m "new message"
Enter fullscreen mode Exit fullscreen mode

Git Aliases

To create shortcuts for commonly used Git commands:

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
Enter fullscreen mode Exit fullscreen mode

Conclusion

Git is an incredibly versatile tool for version control, offering numerous commands to manage your project's history, collaborate with others, and ensure your code is always in a good state. Keep this cheat sheet handy as a quick reference to streamline your Git workflow.

Top comments (0)