Skip to main content

Git Cheat Sheet

Summary

Git is a version control system used for tracking changes in text files. This cheat sheet lists the most common and useful Git commands. Refer to the official Git documentation for more in-depth information.

Configuration #

Configure user information:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Set default text editor to Vim:

git config --global core.editor vim

Set default branch name to main:

git config --global init.defaultBranch main

Create and clone repositories #

Initialize a new repository:

git init

Clone a repository:

git clone repository.git

Basic commands #

Check repository status:

git status

Add changes in file to staging:

git add file

Commit changes:

git commit -m "Commit message"

Commit all changes (including untracked files):

git commit -am "Commit message"

Branching #

Viewing branches #

See all local branches:

$ git branch

See all remote branches:

$ git branch -r

Creating branches #

Create a new branch banch_name:

git branch branch_name

Switch to branch_name branch:

git checkout branch_name

Create new_branch branch from current branch and switch to it:

git checkout -b new_branch

Create new_branch branch from main branch:

$ git checkout -b new_branch main

Deleting branches #

Delete local branch_name branch:

git branch -d branch_name

Delete local branch_name branch with unmerged/unpushed commits:

$ git branch -D branch_name

Delete remote branch branch_name:

$ git push origin --delete branch_name

Merging branches #

Merge changes from branch_name branch into the current branch:

git merge branch_name

Remote repositories #

Add a remote repository:

git remote add origin repository.git

Show remote URL of current repository:

$ git config --get remote.origin.url

Push changes to a remote repository:

git push -u origin branch_name

Pull changes from a remote repository:

git pull origin branch_name

Fetch changes from a remote repository (without merging):

git fetch

Switching remote URLs from HTTPS to SSH

$ git remote set-url origin git@example.com:username/repository.git

Reset (undo) #

Soft reset #

Discard changes in file in the working directory:

git checkout -- file

Unstage changes in file:

git reset file

Amend (change) the last commit:

git commit --amend

Hard reset #

Set Git back to HEAD:

git reset --hard HEAD

Set Git back to the commit before the HEAD:

git reset --hard HEAD^

Set Git back to the n-th commit before the HEAD:

git reset --hard HEAD~n

Stage file for removal (untrack), without removing it from the working dir:

$ git rm -r --cached file

Stashing #

Stash changes:

git stash

Apply stashed changes:

git stash apply

Tagging #

Create a lightweight tag:

git tag tag_name

Create an annotated tag:

git tag -a tag_name -m "Tag message"

List all tags:

git tag

Git Large File Storage (LFS) #

Install Git-LFS:

$ git lfs install

Read Git-LFS help page for <command>:

$ git lfs help <command>
$ git lfs <command> -h

Add .png file type to be handled by Git LFS:

$ git lfs track "*.png"

Remove .png file type from Git LFS:

$ git lfs untrack "*.png"

Log and history #

Show commit history:

git log

Show commit history with a graph:

git log --graph --oneline --all

These commands cover the basics of using Git. Adjust them based on your specific needs and remember to refer to the official Git documentation for more in-depth information.


Further readings #

Sources and recommended, further resources on the topic:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

License

License: Git Cheat Sheet by Jonas Jared Jacek is licensed under CC BY-SA 4.0.

This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:

<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/publications/git-cheat-sheet">Git Cheat Sheet</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>

For more information see the DITig legal page.


“Premature optimization is the root of all evil.”

Donald Ervin Knuth, American computer scientistStructured Programming with Goto statements, - IT quotes