Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: pushing amended commits

Tags:

git

I am currently working on a project and using machines in two different locations to do it. I have created a branch for the feature I am working on and when I finish some work on it I amend my commit to that branch and push it to the server so I can pick up where I left off on my other machine.

When I try to send my amended commit it rejects my push. I assume this is because I am pushing up a commit that is intended to clobber the current HEAD of the feature branch. I typically just use --force.

Is there a better way to do this?

mike@sleepycat:~/projects/myproject$ git pull origin topx
From heroku.com:myproject
 * branch            topx       -> FETCH_HEAD
Already up-to-date.
mike@sleepycat:~/projects/myproject$ git add app/models/reward.rb
mike@sleepycat:~/projects/myproject$ git commit --amend
[topx 82a9880] Added topX reward
 9 files changed, 106 insertions(+), 21 deletions(-)
 rewrite app/views/ceo/_reward_criteria.html.erb (96%)
 create mode 100644 public/javascripts/jquery.multiselect.min.js
 create mode 100644 public/site/javascripts/jquery.multiselect.min.js
 create mode 100644 public/stylesheets/jquery.multiselect.css
mike@sleepycat:~/projects/myproject$ git push origin topx
To [email protected]:myproject.git
 ! [rejected]        topx -> topx (non-fast-forward)
error: failed to push some refs to '[email protected]:myproject.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.
like image 262
mikewilliamson Avatar asked Oct 19 '10 12:10

mikewilliamson


2 Answers

There is a better way, the option --force-with-lease

 git push --force-with-lease

It will only force the update if nobody has pushed after you, or if you haven't pushed on another machine;. If you got a rejection you will then fetch/rebase before pushing again, so working in a more secure way.

From the documentation

--force-with-lease alone, without specifying the details, will protect all remote refs that are going to be updated by requiring their current value to be the same as the remote-tracking branch we have for them.

like image 69
pdem Avatar answered Oct 11 '22 08:10

pdem


There is no way to overwrite remote branch without --force in this case. You have 2 options:

  • continue to use git push --force
  • don't use --amend. If some work you've done deserves a separate commit (is stable enough, compiling, passing more tests, etc...), create new commit. If not, create temporary branch (something like my-feature-unstable if your branch is my-feature) and commit there. When it becomes stable again, you can do git reset --soft my-feature, checkout my-feature and commit to create single commit in your branch from several unfinished commits. After that, temporary branch can be deleted.
like image 24
max Avatar answered Oct 11 '22 10:10

max