Git Cheatsheet

created:

updated:

tags: git

Git commands I often use

git pull

Update your local working branch with commits from the remote, and update all remote tracking branches.

git rebase -i <branch>

Reapply commits on top of another base tip.

  • -i is for an interactive mode.

If <branch> is specified, git rebase will perform an automatic git switch <branch> before doing anything else. Otherwise, it remains on the current branch.

git commit --amend

Replaces the most recent commit with a new commit.

git switch -c <new_branch_name>

Create a new branch named <new_branch_name> starting at <start_point> before switching to the branch.

git stash

Stash the changes in a dirty working directory away.

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

Git commands I often get confused

git reset <commit>

Reset your HEAD pointer to a previous commit and preserve all changes as unstaged changes.

git reset HEAD

Get all the files out of the staging area (i.e. undo the last git add)

git reset --hard HEAD

Discard all local changes in your working directory.

git revert <commit>

Revert a commit (by producing a new commit with contrary changes)

git reflog show

Show Reference log.

git cherry-pick

Apply the changes introduced by some existing commits.

git diff

Show the changes between commits, commit, and working tree, etc.

git apply

Apply a fetch to files and/or to the index.

git show

Shows various types of objects.

git pull --rebase

Updates your local working branch from the remote, but rewrite history so any local commits occur after all new commits coming from the remote, avoiding a merge commit.

References