How do I make my branch the same as its corresponding remote branch?
This post follows my previous post on git pull: Git: What EXACTLY does "git pull" do?
Let's say that I want to do a "git pull" to a specific branch, and I want my local copy of that branch to be IDENTICAL to the remote copy of the remote. How to do it?
Assuming you are already on the branch in question (and it is tracking upstream correctly), the simplest way is
git fetch && git reset --hard FETCH_HEAD
In this case, a special FETCH_HEAD
ref is used, so you don't need to type in the name of the upstream branch or whatever.
This should do the trick:
git fetch
git reset --hard origin/{insert branch name}
git clean -fd
If you want it to really be the same, including removing gitignored, use git clean -fdx
.
Git pull always does two things.
1) fetches the remote repository
2) either makes a rebase or a merge
So the best option for removing local changes is fetching and then resetting your local branch to the origin / branch
Once selected with git fetch
, will git reset @{u}
set the branch to point upstream. git reset --hard @{u}
will additionally check that the revision version continually discards any changes to tracked files in your working directory .
If you also want to discard untracked files, you can do this in bash like this:
bash -c '
GLOBIGNORE=.:..
shopt -s dotglob extglob
rm -rf !(.git)
git reset --hard @{u}'