Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git branch diverged

I am new to GIT, although I understand it's concept I don't think I understand it's practice as well.

I was given a branch, uitest to work on, because I may have not done the push, commit, pull correctly, I have now diverged the branch.

Because I am new here, n I also don't want to override other devs codes as my code or changes are simply experimental to get used to git and how it works I wouldn't mind discarding my changes as I have a copy of everything I need to re-do everything.

$ git status
# On branch uitest
# Your branch and 'origin/uitest' have diverged,
# and have 47 and 6 different commits each, respectively.
#
nothing to commit (working directory clean)

How would I undiverge? discard my changes and pull the most recent changes from the latest changes and then continue working without messing other peoples work.

Also, as I am new to this, be a little descriptive about your answer as it may not make much sense to me.

Many thanks

like image 673
Val Avatar asked Apr 05 '13 09:04

Val


People also ask

What is branch diverged in git?

A branch in git is a series of interrelated commits. If two branches follow a non-linear path then they diverge each other.

What is git pull rebase?

Git pull rebase is a method of combining your local unpublished changes with the latest published changes on your remote. Let's say you have a local copy of your project's main branch with unpublished changes, and that branch is one commit behind the origin/main branch.


1 Answers

There a few ways.

Merge

First, you could simply merge origin/uitest, but that doesn't leave a clean history in that it introduces what looks like a branch and merge even though it was meant to be the same branch all along. I believe Linus liked to call these kind of merge commits "pointless". Unfortunately, this is also the easiest approach.

Rebase

Rebasing tends to be a more advanced topic and can introduce a whole host of other problems if you aren't careful. That said, it's also a great way to get clean history without the pointless commits. In this case, you could do:

git rebase origin/uitest

from you uitest branch, and it would take all the work you did, and put it on top of the work in origin/uitest.

There are a couple of catches though. First, if you've merged any other branches into your branch, git rebase will drop them. You need to pass the -p flag to keep any merge commits you introduced, but that's not always the right thing to do either. If all you did was commit your own changes, you should be fine with the command I've given about.

Second, any time you use rebase you should always remember to never rebase public commits. Rebase will change the commit ids because the parents have changed. If people are merging you work, and you rebase it, they'll end up with several copies of your commits in the history--which is badness. So be careful about when you apply this technique.

All that said, you want to make git rebase your friend. It's a powerful and useful tool, but as with any power tool, it can be dangerous.

Discard your work

If you simply want to discard what you've done, you can run:

git reset --hard @{u}

or

git reset --hard origin/uitest

That will reset your uitest branch to match the upstream or origin/uitest. It will discard your commits.

Personally, I'd rebase the work or at least give it a try. If it fails, or becomes to complicated from merge conflicts, you can always abort with git rebase --abort, and then fallback to merge or discarding your changes (although merging will likely show you the same merge conflicts).

like image 124
John Szakmeister Avatar answered Oct 03 '22 06:10

John Szakmeister