Skip to content

Git cheatsheet

Configuring Git

Show Git Configuration

git config -l

Set user name

git config --global user.name "Your Name"

Set email

git config --global user.email "user@example.com"

Enable colorized output

git config --global color.ui auto

Initializing or Cloning a Repository

Initializing a Repository

git init

Cloning a Repository

git clone https://github.com/learntheshell/repository.git

Show Remote Repositories

git remote -v

Staging Files

Add specific file

git add filename.txt

Add all changes

git add .

Checking Status

Display status

git status

Status in short format

git status -s

Committing Changes

Commit

git commit -m "commit message"

Commit as other author (not defined in global config)

git commit --author "test <test@example.com>"

Amending Commits

Change commit message in local repository, that has not been pushed yet

git commit --amend

Changing message of already pushed commit

git commit --amend
git push --force-with-lease origin branch-name

Pushing Changes

Push to the main branch

git push origin main

or shorter version

git push

Push specified branch

git push origin branch-name

Viewing Commit Log

Display commit log

git log

Display commit log for a branch

git log branch-name

Viewing a Graph of Commits

git log --oneline --graph --decorate --all

Pulling Updates

Pull from main

git pull origin main

or

git pull

Pull changes from specific branch

git pull origin branch-name

Viewing Differences

Show changes between working directory and staging area

git diff file.txt

Show changes between the staging area and the repository

git diff --staged file.txt

Comparing Branches

git diff main feature-branch

Branching and Merging

Creating a Branch

git branch new-feature

Switching Branches

git checkout new-feature

Creating and Switching to a Branch

git checkout -b new-feature

Merging a Branch

git checkout main
git merge new-feature

Deleting a Branch

git branch -d new-feature

List Branches

git branch

or with -l parameter

git branch -l

list branches with a pattern

git branch -l feature*

Show a List of All Branches

git branch -a

Rebasing

Rebase

git checkout feature-branch
git rebase main

Interactive Rebase

git rebase -i HEAD~3

Stashing Changes

Stash current changes

git stash

Apply the most recent stash

git stash apply

List stashed changes

git stash list

Drop a specific stash

git stash drop stash@{0}

Cherry-picking a Commit

Apply specific commit from another branch

git checkout main
git cherry-pick abc123

Resetting Commits

Discard all local changes

git reset --

Discard Changes in a File

git checkout -- filename.txt

Soft reset (preserves changes)

git reset --soft HEAD~1

Unstaging a File

git reset HEAD filename.txt

Mixed reset (default, unstages changes)

git reset HEAD~1

Hard reset (discards all changes)

git reset --hard HEAD~1

Reverting a Commit (Create a new commit to undo changes)

git revert abc123

Resolving Merge Conflicts

git status                 # View conflicted files
# Manually edit the conflicting file
git add file_with_conflict.txt
git commit

Working with Tags

Creating a Tag

git tag -a v1.0 -m "Version 1.0"

Listing Tags

git tag

Pushing Tags

Push a specific tag

git push origin v1.0

Push all tags

git push --tags

Submodules

Add submodule

git submodule add git://github.com/jquery/jquery.git externals/jquery
git submodule update --init --recursive

Update submodules

git submodule update --remote

Clone repository and pull submodules

git clone project-with-submodules
git submodule init
git config -l
git submodule update --remote

References