Skip to content

Git Cheat Sheet

Setting up repository

Setup

git config --global user.name "{firstname lastname}" Set the author name to be used for all commits by the current user
git config --global user.email "{email}" Set the email to be used for all commits by the current user
git config --global --edit Open the global configuration file in a text editor for manual editing

Initializing

git init Creates a new git repository in the directory

Clone

git clone {url} Clone the repo
git clone {url} {directory} Clone repo into the directory

Changes

Adding

git add {file_name} Add the file to staging
git add . Add all file to stagin
git add *{extension} Add all extensions (like .vue) to staging
git add {directory} Stages the changes in all files in directory

Removing

git rm {file_name} Removes the file from both git and file system
git rm --cached {file_name} Only remove file from git index

Reseting

git reset {file_name} Remove file_name file from staged area.
git reset Reset staging area to match most recent commit, but leave the working directory unchanged
git reset --hard Same as previous and overwrites all changes in the working directory
git reset {commit} Move the current branch tip backward to commit, reset the staging area to match, but leave the working directory unchanged
git reset --hard {commit} Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after commit

Diff

git diff View the changes made but not yet staged

Commiting

git commit -m "{message}" Commit the staged snapshot
git commit --amend Combine staged changes with the previous commit, or edit the previous commit message without changing the snapshot
git commit --amend --no-edit Amend a commit without changing it's message

Pushing

git push {my-remote} {my-branch} Pushes the commit to my-remote my-branch
git push -f Force push to remote branch with local branch.
git push --force-with-lease is a safer option that will not overwrite any work on the remote branch if more commits were added to the remote branch

Branching

Show

git branch Shows all branch and current branch is shown with a asterisk
git branch -a Shows all the branches including local and remote

Creating and Deleting

git branch {branch} Creates a branch
git branch -d {branch} Deletes a branch
git push origin --delete {branch} Delete a remote branch

Switch branch

git switch {branch} or git checkout {branch} Switch to branch
git switch -c {branch} {start_point} or git checkout -b {branch} {start_point} Create a new branch and switch to it
git switch -C {branch} {start_point} or git checkout -B {branch} {start_point} Create a new branch and switch to it, even if the branch already exists.If the branch already exists, this option will overwrite it with the current HEAD commit

Switch to a specific commit without changing branch

git switch --detach {commit} or git checkout [--detach] {commit} Switch to a specific commit without creating a branch

Detached head

git switch --detach {branch} Switch to the latest commit of a specific branch. This is also known as being in "detached HEAD" state

Log

git log Shows the log of commit
git log --oneline --graph --decorate Shows log of commits, each commit with single line with a graph
git log --oneline --graph --decorate --all -n 30 Shows 30 log with all branches in the repo with graph
git log -p {file_name} Changes over time for a specific file

Rebasing

git rebase {branch} Rebase the current branch onto the specified branch
git rebase --continue If there is any conflicts during rebase, git will pause and after resolving them use this command to continue
git rebase --abort Abort the rebase process and return to the original state
git rebase --skip To skip a specific commit during the rebase process
git rebase -i {commit_id} Rebase commits from a commit

Stashing

git stash Stashes the staged and unstaged changes.
git stash -u Stashes everythings including unstashed file
git stash list List of all stashes
git stash pop Delete the recent stash and apply it
git stash pop stash@{2} Delete the {2} stash and apply it
git stash show Shows the description of stash
git stash apply Keep the stash and apply it
git stash drop Delete all stash