Skip to content

Instantly share code, notes, and snippets.

@AkshatBajaj
Last active February 25, 2021 04:52
Show Gist options
  • Save AkshatBajaj/7cd2eb431e4f144f0ca810ebf46330ff to your computer and use it in GitHub Desktop.
Save AkshatBajaj/7cd2eb431e4f144f0ca810ebf46330ff to your computer and use it in GitHub Desktop.
Git Cheatsheet

Git Cheatsheet

vim cheatsheet

What is HEAD?

Viewing unpushed Git commits

  • git log origin/master..HEAD
  • git diff origin/master..HEAD

Remove ignored directories and files

  • git clean -fdX

Set the remote

  • git remote add remote-name repository-URL
  • git remote -v (to verify)

Sync the fork

  • git fetch upstream
  • git checkout master (for master)
  • git merge upstream/master (for master)
  • git push origin master (for master)

Sync the branches

  • git checkout branchname (merge to)
  • git merge branchname (merge from)

Sync local branch with remote branch

  • git fetch origin
  • git reset --hard origin/branch-name
  • git clean -f -d

Which remote branch, local branch is tracking?

  • For current branch: git checkout

View the status of changed files

  • git status

Stash

  • git stash push -m "message"
  • Apply: git stash apply stash@{index}. For example: git stash apply stash@{1}

Pushing to remote

  • git add .
  • git commit -m "Commit Message"
  • git push origin BranchName

Remove the staged changes (Added by git add)

  • git reset filename
  • git reset (for all the files)

How to revert a commit pushed to a repo

  • git reset --hard HEAD~ (hard or soft or mixed depending on the requirements)(For Latest commit) OR
  • git reset --hard <last_working_commit_id> (for a specific commit)
  • git push origin -f (To force push it to the remote)

Listing merged branches

  • git branch --merged

Delete a branch

  • git branch -d (local branch)
  • git push origin --delete (remote branch)

Remove branches from git branch -r

  • git fetch --prune
  • git remote prune upstream (for a specific remote)

Undo modifications

  • git checkout -- . (to checkout all the files)
  • git checkout -- index.html (for a specific file)
  • Fetch file from different branch: git checkout master -- a.py

Commit only part of a file in Git

Abort git rebase -i (with error)

  • :cq

Continue git merge after resolving conflicts

  • git merge --continue or git commit

Make an existing Git branch track a remote branch

  • git push -u origin branch-name
  • The equivalent long option is --set-upstream

To fetch the pull request

  • git fetch upstream pull/pr-number/head:pr-pr-number
  • git checkout pr-pr-number

How to rebase against another branch overriding conflicts

Recover from force push

Change commit message

Resetting git config

** Akshat Bajaj, 2018**

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment