Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 894
Patrick Avatar asked Mar 01 '13 16:03

Patrick


People also ask

How do I delete a local copy of a remote branch?

Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.

Does deleting a local branch delete the remote branch?

In Git, local and remote branches are separate objects. Deleting a local branch doesn't remove the remote branch.


2 Answers

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.

like image 124
Greg Bacon Avatar answered Oct 20 '22 06:10

Greg Bacon


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.

like image 27
more tension Avatar answered Oct 20 '22 05:10

more tension