yannisf
yannisf

Reputation: 6126

Pushing a commit in git when local repo has not already merged origin HEAD

Suppose that my local repository is currently one commit behind the origin. Suppose that I commit in my local repository a change that is not conflicting with the origin. How can I push this change to the origin without first pulling/merging changes from the origin?

Upvotes: 3

Views: 96

Answers (3)

Basel Shishani
Basel Shishani

Reputation: 8187

Ok, so you're getting rejected for non-fast-forward push, and you have your reasons not to want to pull the latest update from the remote. I think it's obvious you need to branch off your changes.

So lets create a a branch at the common point, HEAD~n, in your case HEAD~1:

git branch myfeat HEAD~1

Then fast-forward it:

git checkout myfeat
git merge master

Now the new branch has a common point, so just:

git push origin myfeat

The remote repo now looks like this:

--X---X--X--Xbug      (master)
          \
           \--Xfeat   (myfeat)

Now others can merge/rebase your feature. Ultimately you'll have to either fix the bug and pull it locally, or you devise a slightly different workflow as you are keeping a custom local master.

Upvotes: 3

richo
richo

Reputation: 8999

It's possible he wants to push to the remote purely to have a backup of his changes, if that is the case he could do it with

git push origin HEAD:refs/heads/some_branchname_not_in_use

I generally use trash/somebranchname for these things so that it's obvious the purpose.

Upvotes: 0

neevek
neevek

Reputation: 12148

You cannot do what you described, but I guess what you want is:

git pull --rebase
git push

The above command will merge your local repo with remote repo gracefully(without creating a merge commit) and push your local commits to the remote.

Upvotes: 0

Related Questions