kaparekh
kaparekh

Reputation: 23

How to update local branch with changes from remote git branch without having to 'merge' commit

I have a remote branch where I have pushed some changes from PC A (I am the only one working on this branch). I also have PC B where the same branch is cloned (is tracking the same remote branch as from PC A), but it is missing the latest changes that I just pushed to the branch from PC A. When I run git pull on PC B to get the latest changes, git obviously complains about 'merge conflicts'. Since I am tracking the same remote branch from PC A and from PC B, I do not want to make a merge commit every time and I also do not want to rebase. I just want the branch on PC B to get updated to the exact state as the remote branch without having to make any commits whatsoever. How can this be achieved in git?

Upvotes: 2

Views: 2015

Answers (1)

TTT
TTT

Reputation: 29159

You can easily reset with:

git fetch
git reset --hard @{u}

Note @{u} is shorthand for the upstream branch, usually origin/your-branch-name.

Note: since you're sometimes getting conflicts, that means you are re-writing your commits and force pushing. (Maybe with rebase, or amend.) That's a totally fine workflow (I do the same on my own branches), but, when you force push, it's a good habit to use:

git push --force-with-lease

By force pushing that way you're less likely to accidentally blow away a change you made on another computer that you haven't seen yet. If you do that and get an error, then you need to git fetch and compare your local and remote branch to see if you need to reset (or pull with rebase) first, before pushing out your latest change.

If you don't always force-push your amends/rebases right away, then you may want to compare local and remote before doing the reset as well, so you don't blow away something locally that you never pushed out.

Upvotes: 3

Related Questions