Cheat Sheet For All Things Git

Git is a vast VCS (Version Control System) that gives you the ability to work projects with teams in a very unique way. There are tons of commands and functions to learn. Getting to know and understand how to use Git properly can only happen over time. If you understand what Git is and how it works, then you will have a better understanding of the Git cheat sheet below.

GitHub provides desktop clients that include a graphical user interface for the most common repository actions and an automatically updating command line edition of Git for advanced scenarios.

Here is a little Git cheat sheet for you to reference for all things Git.

GitHub Desktop and Platform Installations

Find the GitHub desktop here: https://desktop.github.com/

You can find Git for any platforms here: https://git-scm.com/

If you want to know how to install Git on any platform please review these detailed articles:

How To Install Git on MAC OS and Linux

How To Install Git on Windows

Configure Tooling Commands

Some commands to configure user information for all local repositories.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git config –global user.name “[name]”[/ht_message]

This sets the name you want attached to your commit transactions.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git config –global user.email “[email address]”[/ht_message]

This sets the email you want attached to your commit transactions.

Create Repositories

Some commands to start a new repository or obtain one from an existing URL.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git init [project-name][/ht_message]

This command creates a new local repository with the specified name.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git clone [url][/ht_message]

This command downloads a project and its entire version.

For more information on repositories please review the following articles:

How To Create a Git Repository

How To Manage a Git Repository

How To Inspect a Git Repository

Git Repository Structure

Refactor File Names

Some commands to relocate and remove versioned files.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git rm [file][/ht_message]

This command deletes the file from the working directory and stages the deletion.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ $ git rm –cached [file][/ht_message]

This command removes the file from version control but preserves the file locally.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git mv [file-original] [file-renamed][/ht_message]

This command changes the file name and prepares it for commit.

Suppress Tracking

Exclude temporary files and paths.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]*.log
build/
temp-*[/ht_message]

A text file named .gitignore suppresses accidental versioning of files and paths matching the specified patterns.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git ls-files –others –ignored –exclude-standard[/ht_message]

This command lists all ignored files in the project.

Save Fragments

Commands to shelve and restore incomplete changes.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git stash[/ht_message]

This will temporarily store all modifies tracked files.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git stash pop[/ht_message]

This command restores the most recently stashed files.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git stash list[/ht_message]

This command lists all stashed changesets.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git stash drop[/ht_message]

This command discards the most recently stashed changeset.

Make Changes

Commands for reviewing edits and crafting a commit transaction.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git status[/ht_message]

This command lists all new or modified files to be committed.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git diff[/ht_message]

This command shows file differences that are not yet staged.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git add [file][/ht_message]

This command snapshots the file in preparation for versioning.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git diff –staged[/ht_message]

This command shows file differences between staging and the last file version.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git reset [file][/ht_message]

This command unstages the file, but preserves its contents.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git commit -m”[descriptive message]”[/ht_message]

This command records file snapshots permanently in version history.

Group Changes

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git branch[/ht_message]

This command lists all local branches in the current repository.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git branch [branch-name][/ht_message]

This command creates a new branch.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git checkout [branch-name][/ht_message]

This command switches to the specified branch and updates working directory.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git merge [branch-name][/ht_message]

This command combines the specified branch’s history into the current branch.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git branch -d [branch-name][/ht_message]

This command will delete a specified branch.

Review History

Here are some commands that will allow you to browse and inspect the the evolution of project files.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git log[/ht_message]

This command lists version history for the current branch.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git log –follow [file][/ht_message]

This command lists version history for the file, including renames.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git diff [first-branch]…[second-branch][/ht_message]

This command shows content differences between two branches.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git show [commit][/ht_message]

This command outputs metadata and content changes of the specified commit.

Redo Commits

Some commands to erase mistakes.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git reset [commit][/ht_message]

This command Undoes all commits after [commit], preserving changes locally.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git reset –hard [commit][/ht_message]

This command discards all history and changes back to the specified commit.

Synchronize Changes

Some commands to register a remote (URL) and exchange repository history.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git fetch [remote][/ht_message]

This command downloads all history from the remote repository.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git merge [remote]/[branch][/ht_message]

This command combines the remote branch into the current local branch.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git push [remote] [branch][/ht_message]

This command uploads all local branch commits to GitHub.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]$ git pull[/ht_message]

This command downloads bookmark history and incorporates changes.

A Little More

For an overview of even more top Git commands and examples view this article:

Top 20 Git Commands and Examples

For more detailed information on how to undo changes in Git please read:

How To Undo Changes in Git

There you go. A little Git cheat sheet to help you on your journey.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.