DEV Community

Cover image for Github git cheat sheet
Sadisha Nimsara
Sadisha Nimsara

Posted on

Github git cheat sheet

GitHub Git Cheat Sheet

Git is an open source distributed version control system that facilitates GitHub activities on your laptop or
desktop. This cheat sheet will help you with commonly used Git command line instructions for quick reference.

Catalog

INSTALL GIT

For Windows users

  1. Download Git for Windows Setup
  2. Install Git through the setup. Usually all configurations can be left in default settings.

For macOS users

Download Git for Mac

For Linux users

Debian/Ubuntu

For the latest stable version for your release of Debian/Ubuntu

$ sudo apt-get update
$ sudo apt-get get install git
Enter fullscreen mode Exit fullscreen mode

For Ubuntu, this PPA provides the latest stable upstream Git version

$ sudo add-apt-repository ppa:git-core/ppa
$ sudo apt update
$ sudo apt install git
Enter fullscreen mode Exit fullscreen mode

Fedora

For releases up to Fedora 21

$ yum install git
Enter fullscreen mode Exit fullscreen mode

For Fedora 22 and later

$ dnf install git
Enter fullscreen mode Exit fullscreen mode

Arch Linux

$ pacman -S git
Enter fullscreen mode Exit fullscreen mode

HOW TO USE GIT

Windows

Right click on any location and click git bash.

Linux and Mac

Open Terminal to use git.

CONFIGURE TOOLING

Configure user information for all local repositories

Set the name you want attached to your commit transactions

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

Set the email address you want attached to your commit transactions

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

CREATE REPOSITORIES

Start a new repository or obtain one from an existing URL

Create a new local repository with the specified name

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

Download a project and its entire version history

$ git clone [url]
Enter fullscreen mode Exit fullscreen mode

MAKE CHANGES

Review edits and craft a commit transaction

List all new or modified files to be commited

$ git status
Enter fullscreen mode Exit fullscreen mode

Show file differences not yet staged

$ git diff
Enter fullscreen mode Exit fullscreen mode

Snapshot a file in preparation for versioning

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

Snapshot all files of the current directory in preparation for versioning

$ git add .
Enter fullscreen mode Exit fullscreen mode

Unstage the file (reset), but preserve its contents

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

Record file snapshots permanently in version history

$ git commit -m "[descriptive message]"
Enter fullscreen mode Exit fullscreen mode

GROUP CHANGES

Name a series of commits and combine completed efforts

List all local branches in the current repository

$ git branch
Enter fullscreen mode Exit fullscreen mode

Create a new branch

$ git branch [branch-name]
Enter fullscreen mode Exit fullscreen mode

Switch to the specified branch and updates the working directory

$ git checkout [branch-name]
Enter fullscreen mode Exit fullscreen mode

Combine the specified branch's history into the current branch

$ git merge [branch-name]
Enter fullscreen mode Exit fullscreen mode

Delete the specified branch

$ git branch -d [branch-name]
Enter fullscreen mode Exit fullscreen mode

REFACTOR FILENAMES

Relocate and remove versioned files

Delete the file from the working directory and stages the deletion

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

Remove the file from version control but preserves the file locally

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

Change the file name and prepares it for commit

$ git mv [file-original] [file-renamed]
Enter fullscreen mode Exit fullscreen mode

SUPPRESS TRACKING

Exclude temporary files and paths

List all ignored files in this project

$ git ls-files --other --ignored --exclude-standard
Enter fullscreen mode Exit fullscreen mode

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

*.log
build/
temp-*
Enter fullscreen mode Exit fullscreen mode

SAVE FRAGMENTS

Shelve and restore incomplete changes

Temporarily store all modified tracked files

$ git stash
Enter fullscreen mode Exit fullscreen mode

List all stashed changesets

$ git stash list
Enter fullscreen mode Exit fullscreen mode

Restore the most recently stashed files

$ git stash pop
Enter fullscreen mode Exit fullscreen mode

Discard the most recently stashed changeset

$ git stash drop
Enter fullscreen mode Exit fullscreen mode

REVIEW HISTORY

Browse and inspect the evolution of project files

List version history for the current branch

$ git log
Enter fullscreen mode Exit fullscreen mode

List version history for a file, including renames

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

Show content differences between two branches

$ git diff [first-branch]...[second-branch]
Enter fullscreen mode Exit fullscreen mode

Output metadata and content changes of the specified commit

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

REDO COMMITS

Erase mistakes and craf replacement history

Undo all commits afer [commit], preserving changes locally

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

Discard all history and changes back to the specified commit

$ git reset --hard [commit]
Enter fullscreen mode Exit fullscreen mode

SYNCHRONIZE CHANGES

Register a repository bookmark and exchange version history

Download all history from the repository bookmark

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

Combine bookmarkโ€™s branch into current local branch

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

Upload all local branch commits to GitHub

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

Download bookmark history and incorporates changes

$ git pull
Enter fullscreen mode Exit fullscreen mode

Top comments (0)