/

A Git Cheat Sheet

A Git Cheat Sheet

Welcome to the Git Cheat Sheet! Here, you will find a list of handy Git commands that are worth knowing but can be hard to remember. Let’s dive right in!

Table of Contents

Squash a Series of Commits and Rewrite the History

To squash multiple commits into one and rewrite the commit history, use the following command:

1
git rebase -i

This command will open up the interactive rebasing tool. In the tool, type s to apply squash to a commit with the previous one. Repeat the s command for as many commits as you need.

Apply Changes from a Separate Branch to the Current Branch

To apply changes from a separate branch to the current branch, use the following commands:

For a single commit:

1
git cherry-pick <commit>

For multiple commits:

1
git cherry-pick <commit1> <commit2> <commit3>

Restore the Status of a File to the Last Commit

To revert changes and restore the status of a file to the last commit, use the following command:

1
git checkout -- <filename>

Show a Pretty Commit History Graph

To display a visually appealing graph of the commit history, use the following command:

1
git log --pretty=format:"%h %s" --graph

Get a Prettier Log

To get a prettier log with additional information about authors and timestamps, use the following command:

1
git log --pretty=format:"%h - %an, %ar : %s"

Get a Shorter Status

To get a shorter and more concise status of your repository, use the following command:

1
git status -s

Checkout a Pull Request Locally

To checkout a pull request from a remote repository and create a corresponding local branch, use the following commands:

1
2
git fetch origin pull/<id>/head:<branch>
git checkout <branch>

List Commits Involving a Specific File

To list all commits that involve a specific file, use the following command:

1
git log --follow -- <filename>

List Commits Involving a Specific File Including the Commit Contents

To list all commits that involve a specific file, including the contents of the commits, use the following command:

1
git log --follow -p -- <filename>

List Repository Contributors by Number of Commits

To list the contributors of a repository ordered by the number of commits they made, use the following command:

1
git shortlog -s -n

Undo the Last Pushed Commit

To undo the last commit you pushed to the remote repository, use the following command:

1
git revert -n HEAD

Create a New Branch with Uncommitted Changes

To create a new branch and include all uncommitted changes, use the following command:

1
git checkout -b <branch>

Stop Tracking a File but Keep It in the File System

To stop tracking a file without removing it from the file system, use the following command:

1
git rm -r --cached

Get the Branch Name Where a Specific Commit Was Made

To get the name of the branch where a specific commit was made, use the following command:

1
git branch --contains <commit>

That wraps up our Git Cheat Sheet! We hope you find these commands helpful in your development journey.

Tags: Git, version control, source code management