Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

github fork : your branch is 5 commits ahead how to clean this without pushing

Tags:

git

github

I forked a project on github, in order to send pull requests. The problem is that my way to do was very ... dirty.

I worked on a local copy of the fork, created a branch for my modifications and merge it on the master. Then I pushed the master on the github fork in order to create a pull request.

The problem is that recently, my pull request have been rejected. So now I am 5 commits ahead from the original master. What are the solutions in order to be even with the original repository?

Should I have to checkout the upstream master in a new-branch and make the new master with it (how to do this?)

Or is it better to delete my github fork (I have previous accepted commits) and recreate it (you can see the github repos here)https://github.com/cedlemo/ruby-gnome2

like image 951
cedlemo Avatar asked Mar 14 '15 13:03

cedlemo


People also ask

How do I reset my fork branch with master?

You can reset your local master branch to the upstream version and push it to your origin repository. (You can define the original repo as "upstream" with git remote add upstream /url/to/original/repo .) It should probably be git reset --hard upstream/master to reset the working directory, too.

How do I fix my branch is ahead of origin master by three commits?

There is nothing to fix. You simply have made 3 commits and haven't moved them to the remote branch yet. There are several options, depending on what you want to do: git push : move your changes to the remote (this might get rejected if there are already other changes on the remote)


2 Answers

First, you should always make your PR form a branch, not from master.
master is for mirroring upstream/master, with 'upstream' being the name of the original repo that you forked.

In your case, make sure that upstream exists, make a branch in order to reference your current patches, and reset master to upstream/master:

Before:

  z--z (upstream/master, with new commits)  / z--y--y--y (master with local patches, origin/master) 

Memorize current work:

git checkout master git checkout -b mybranch git remote add upstream /url/original/repo git fetch upstream  # reset master to upstream/master git checkout master git reset --hard upstream/master git push --force    y--y--y (mybranch)  / z--z--z   (master, upstream/master, origin/master)  # replay the patches (even they are rejected for now) on top of master git checkout mybranch git rebase master git push -u origin mybranch          y'--y'--y' (mybranch, origin/mybranch)        / z--z--z   (master, upstream/master, origin/master) 

Here: git reset --hard upstream/master will reset master HEAD on the updated upstream/master, in order for master to reflect the exact same history as the one in the original repo.

But since some commits where previously done on master and pushed on the fork (origin/master), you would need to replace that history with the new master state. Hence the git push --force.

Rebasing mybranch allows for those current patches to be based on the most up-to-date commit of the original repo.

like image 155
VonC Avatar answered Sep 30 '22 13:09

VonC


I use this small shell script to reset a branch to it's origin

function git_branch_reset_origin() {   branch_name="$(git symbolic-ref HEAD 2>/dev/null)"   branch_name=${branch_name##refs/heads/}   git checkout -b foo   git branch -D ${branch_name}   git checkout ${branch_name}   git branch -D foo } 

Put simply it moves to a new 'foo' branch, deletes the branch you were on, and checks it out again. Checking out a branch you don't have locally makes a new local branch of that name at the position of the origin branch.

like image 44
BookOfGreg Avatar answered Sep 30 '22 11:09

BookOfGreg