github fork : your branch is 5 commits ahead how to clean this without pushing
Solution 1
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.
Solution 2
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.
cedlemo
Updated on June 09, 2022Comments
-
cedlemo almost 2 years
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
-
cedlemo about 9 yearsThat works! The problem is that I don't really understand the parts reset master and replay the patches. could you explain the git reset --hard upstream/master + git push force and the git rebase
-
VonC about 9 years@cedlemo Sure, I have edited the answer to explain those steps.
-
Harry_Hopkinson about 2 yearsThank you so much - this solved all of my issues.
-
VonC about 2 years@Harry_Hopkinson Great! I am glad this answer is still helpful seven years later.