1

I created a new branch new_feature and started working on it. I did 4 commits so far and pushed all of them to remote. But for some official reason I need to remove all the commits from both local and remote but I need to keep the changes locally and then commit everything again in one single commit. So the steps I need to do are

  1. Remove commits from local branch new_feature
  2. Remove commits from remote branch new_feature
  3. Need to keep all the changes so that during removing the commits I do not lose any changes I have made so far.
  4. Create a new commit with all the changes I have made so far in the new_feature branch
  5. Push the last commit to remote new_feature branch. At the end new_feature branch should have only one commit with all the changes I have made till now in this branch.

5 Answers5

2

To keep your changes while effectively resetting your tree to latest remote commit, use git reset origin/master. Then commit everything as a single new commit. Then push (this will require --force).

Rebasing will work, but in this particular case it is not really needed.

aragaer
  • 17,238
  • 6
  • 47
  • 49
0

Use git rebase -i ... and squash commits into one, then do git push --force to overwrite the remote branch with the rebased local branch.

kan
  • 28,279
  • 7
  • 71
  • 101
0

I'm not sure why you need to do what you're asking but here is how I would do it based on the scenario you've described.

  1. Use git rebase to squash all of your local commits in to a single commit. (https://git-scm.com/docs/git-rebase)
  2. Use git push with --force to overwrite the remote branch with your rebased local branch. (https://git-scm.com/docs/git-push)

Note, if anyone has checked out your remote branch, they're going to be left in a difficult spot when you overwrite the history of the remote branch.

I hope this helps.

Adam Nierzad
  • 942
  • 6
  • 19
0

What you are being asked to effectively "squash your commits".

This is quite a common request at the point your changes are ready to be merged to master.

Some of your options are:

  • use an interactive rebase, e.g. git rebase -i HEAD~10 to pick and squash the commits.

  • use git merge --squash as in https://stackoverflow.com/a/5190323/631619

  • copy the changed files outside of the project and then make a new branch from master (or delete your branch and then create it anew) and then copy the files in.

Note that the first two options may require you to force push (using `-f) so make sure no-one else has already pushed other commits to that branch This is one reason why option 3 can be safer for newbies.

Community
  • 1
  • 1
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • Option 3 will also require force push as he already pushed the branch. It is most dangerous option as it's require more manual work and could go wrong. – kan Apr 18 '16 at 15:24
0

Sounds like you want to rebase -- this will allow you to fold several commits into one. Let's say you have the following commits (most recent on top):

cdef... Finished feature X new_feature/HEAD
89ab... Continue feature X
4567... Start feature X
0123... Fix typo in docs master

You want to fold the last three commits into one, so you rebase on the last one you want to keep:

$ git rebase -i 0123

Editor pops up:

pick cdef... Finish feature X
pick 89ab... Continue feature X
pick 4567... Start feature X

Change "pick" to "squash" for the later commits:

squash cdef... Finish feature X
squash 89ab... Continue feature X
pick 4567... Start feature X

All the changes will be combined into one commit, you'll be asked for a new commit message, and Bob's your uncle.

Btw, this is a great way to work if you want the security of commits you can roll back on, but want/need a legible commit history: branch, commit like crazy locally, then rebase into logical hunks.

Back to your current situation, now you've got the problem that you can't push this to the remote, because you've changed history. You can force the push, but don't do this lightly -- you'll break things for anyone who's pulled that branch in.

$ git push --force <remote> new_feature
Andrea Reina
  • 1,170
  • 8
  • 19