Git cheat sheet

Cheat sheet with useful git commands.

Whenever I find new necessary commands I will add them.

List the variables in the global git configuration file and their values

git config --global --list

Set your name in the global git configuration file

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

Set your email in the global git configuration file

git config --global user.email "Your Email"

Initialize a new git repository

git init

Initialize a new git repository and set the name for the initial branch to be created

git init -b <branch_name>

Show the state of the working directory and the staging area

git status

Add one file to the staging area

git add <file_name>

Add multiple files to the staging area

git add <file_name> <file_name> <file_name>

Add all files to the staging area

git add .

Add all files to the working directory that have been edited or changed to the staging area

git add -A

Create a new commit with a commit message

git commit -m "Your commit message"

Show a list of commits in reverse chronological order

git log

Show a list of commits in reverse chronological order with the commit message

git log --oneline

List all local branches

git branch

List all local branches remotes and local

git branch --all

Create a new branch

git branch <branch_name>

Switch to a branch

git switch <branch_name>

or

git checkout <branch_name>

Create a new branch and switch to it

git checkout -b <branch_name>

or

git switch -c <branch_name>

Delete a branch

git branch -d <branch_name>

Merge a branch into the current branch

git merge <branch_name>

Add a remote repository

git remote add <remote_name> <remote_url>

Upload changes to a remote repository

git push <remote_name> <branch_name>

or

git push

List the remote repositories connected stored to the local repository by short name

git remote

List the remote repositories connected in the local repository with short name and URL

git remote -v

Clone a remote repository

git clone <remote_url> <local_directory>

Download changes from a remote repository

git fetch

or

git fetch <remote_name>

Delete a remote branch

git push <remote_name> --delete <branch_name>

Remove remote-tracking branches that correspond to deleted remote branches and download changes from a remote repository

git fetch --prune

or

git fetch -p

Fetch and integrate changes from a remote repository

git pull <remote_name> <branch_name>

If an upstream branch is defined, fetch and integrate changes from a remote repository

git pull

Merge a branch into the current branch

git merge

Stop a merge in progress

git merge --abort

Rebase the current branch on top of another branch

git rebase <branch_name>

Continue a rebase in progress after resolving conflicts

git rebase --continue

Stop a rebase in progress

git rebase --abort

Restore a file to another version of the file in the staging area

git restore --staged <file_name>

Save changes in the working directory and staging area in a stash

git stash

List all stashes

git stash list

Create a stash with a message

git stash save <message>

or

git stash create <message>

Apply the changes in the last stash to the working directory and staging area

git stash apply

Apply the changes in the last stash to the working directory and staging area and remove the stash

git stash pop

Apply the changes in a specific stash to the working directory and staging area

git stash apply stash@{<stash_number>}

Apply the changes in a specific stash to the working directory and staging area and remove the stash

git stash pop stash@{<stash_number>}

Remove the last stash

git stash drop

Remove a specific stash

git stash drop stash@{<stash_number>}

Clear all stashes

git stash clear