Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwriting and Pushing a Git Branch

Tags:

git

In merging my changes against an upstream master, I frequently find myself doing the following:

git checkout somefeature
git checkout -b integration
git rebase master # resolving conflicts along the way
git checkout somefeature
git merge integration # or rebase, doesn't matter in this example

I'll often find that merging the integration branch back into my feature branch fails do to some conflicts. The first question I have is, "why is this happening if my integration branch is a descendent of somefeature and I've already resolved conflicts against the upstream master?"

If you're wondering why I'm using an integration branch to begin with, it's to prevent polluting my current branch with a half-failed merge.

My current workaround is to do this:

git checkout integration
git branch -f somefeature # overwrite the branch

The problem now is that I can't push my changes back to a remote branch:

git push origin somefeature
! [rejected]        somefeature -> somefeature (non-fast forward)

So now I have to remove the remote branch and re-push my changes. This can't be the optimal way to do this, so I'm wondering, "what's the best way to overwrite a branch and push the changes to a remote branch?"

like image 921
tmountain Avatar asked Jan 19 '11 21:01

tmountain


People also ask

Does git push overwrite?

It's the counterpart to git fetch , but whereas fetching imports commits to local branches, pushing exports commits to remote branches. Remote branches are configured using the git remote command. Pushing has the potential to overwrite changes, caution should be taken when pushing.

Does Force push overwrite?

Only when you are up-to-date will you be able to push your own new commits to the remote. The --force option for git push allows you to override this rule: the commit history on the remote will be forcefully overwritten with your own local history.

How do I push to GitHub without overwriting?

Use git push to push those changes to the remote repository origin , specifying the master branch. In order to prevent you overwriting remote work, Git has refused to execute your push. Use git pull to bring your repository up to date with origin . It will open up an editor that you can exit with Ctrl+X.

How do I push changes to an existing remote branch?

In some cases, you may want to push your changes to another branch on the remote repository. In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.


1 Answers

The problem is caused because git rebase generates a new series of commits that aren't descended from the somefeature branch, then when you try and merge them back into somefeature the conflict resolution done during the rebase doesn't apply. If you were to just merge instead of rebase then this would work as the merge commit would descend from the somefeature branch.

In terms of pushing to the remote branch you can just use --force to make the push succeed, that will cause problems for anyone else that has a copy of it though.

like image 161
Nemo157 Avatar answered Sep 28 '22 05:09

Nemo157