Skip to content

Instantly share code, notes, and snippets.

@realdubb
Last active March 11, 2022 15:29
Show Gist options
  • Save realdubb/82794d995ad1c5dabdb778c3c9517d2f to your computer and use it in GitHub Desktop.
Save realdubb/82794d995ad1c5dabdb778c3c9517d2f to your computer and use it in GitHub Desktop.
Often googled git questions.

How do I show the changes which have been staged?

git diff --cached

How do I force “git pull” to overwrite local files?

git fetch --all git reset --hard origin/<branch_name>

How do I pull changes to local branch that has been force pushed on remote?

git fetch
git reset origin --hard
git pull

or

git fetch
git checkout another-branch
git branch -d affected-branch
git checkout affected-branch

How do I checkout files or folders from a different branch?

origin is not necessary if local branch

git checkout <origin/branch_name> -- <path>

How do I resolve merge by accepting all incoming/their changes?

git checkout --theirs <filename/pattern>

During rebase "ours" refers to the other branch, not the current one we're in

How do I unstage files from previous commits? ~n for n commits (going back n commits)

git reset --soft HEAD~1

How do I remove untracked files

Step 1 is to show what will be deleted by using the -n option:

git clean -n

Clean Step - beware: this will delete files:

git clean -f

How do I reset my local branch to be just like the branch on the remote repository

Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master

If you want to save your current branch's state before doing this (just in case), you can do:

git commit -a -m "Saving my work, just in case"
git branch my-saved-work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment