With git, how do I remove a local copy of a remote branch? Or at least pull without merging?
Asked Answered
P

3

18

Another developer has deleted and rebuilt a remote branch called "development" which I already have a checked out copy. He did this delete and rebuild to remove some cruft from it. Which is great.

But when I do a "git pull origin development" it keeps getting merge conflicts. Yet I don't want what I have in my copy. I want what is in origin only.

So how do I delete my local copy of it and pull it back down? Or at least pull without merging my local info into it?

Pressure answered 1/3, 2013 at 16:50 Comment(0)
N
24

Grab the most up-to-date changes from the origin remote. This step is the “pull without merge” step you asked for in your question. git pull is the equivalent of git fetch followed by git merge.

git fetch origin

Get to a clean branch if necessary.

git stash

With a clean branch, switch to the development branch if you are not already there.

git checkout development

Finally, force your local development branch to point to the same commit as the one on origin.

git reset --hard origin/development

Always think twice before using git reset --hard, as with rm -rf. It is a sharp, useful tool, but it can destroy work if you are not careful.

Nadaba answered 1/3, 2013 at 17:12 Comment(1)
Fantastic, this is exactly what I was looking for. I was already working from a neutral state, so I didn't need the stash and I wasn't worried about losing any work. Awesome!Pressure
I
20

To delete a local branch permanently, do:

git branch -D <branch-name>

Fetch changes from the remote repo but do not merge:

git fetch

Now create and checkout a local copy of the remote branch and track it:

git checkout -b development origin/development

git should tell you "Branch development set up to track remote branch development from origin", and that it's switched locally to the development branch.

Irredentist answered 1/3, 2013 at 16:57 Comment(1)
Why would you update the master branch? (did you mean: git fetch?)Constantinople
O
1

Found out this link with the 'prune' command:

https://opensource.com/article/22/8/delete-local-reference-remote-branch-git

It worked for me (even) in case of previously removed remote branch.

Odonnell answered 3/10, 2023 at 6:39 Comment(1)
While the link may answer the question, it's best to include the relevant details in case it ever goes down.Alliterative

© 2022 - 2024 — McMap. All rights reserved.