0

I am interested in how can I remove a local commit(i.e. a commit which is not pushed to the remote) without loosing the changes contained by it.

What I did : - committed my local changes(I never pushed it to the remote) - reverted that commit - checked if I still have the changes After performing all of the above steps I realized that my local changes were lost. I had to revert the revert commit in order to get them back.

My question is : Which is the proper way for removing a local commit without losing the changes included in it?

Henrik Joe
  • 21
  • 1
  • 2
    Possible duplicate of [How do I undo the most recent local commits in Git?](https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git), i.e: `git reset HEAD~` – Ulysse BN Aug 08 '19 at 18:04

2 Answers2

0

This has been answered several times. See: How do I undo the most recent local commits in Git?

What you want to do is git reset --soft 4817564 where 4817564 is the SHA of the commit you want to revert to.

Danya Smith
  • 76
  • 1
  • 4
0

If we had the commit graph:

branch1    A---B---C

And we wanted to rollback/revert/ the changes in C, but still keep them easily accessible - I would handle this situation as follows:

  1. git checkout branch1 - Switch to the latest branch1 (currently C)
  2. git branch branch2 - Create the branch pointing to the changes
  3. git reset --hard B - Remove the C changes from the branch

To get the final state:

branch1    A---B
                \
branch2          C

I like this approach because it allows me to easily reference the change branch if I need to merge, rebase, or cherry-pick those changes at a later date. Additionally, by keeping it as a branch (as opposed to just writing down the commit Id) I ensure the changes are reachable and thus will never be garbage collected.

NOTE: This approach will work even if the changes are spaced across multiple commits - and have the same benefits

Vlad274
  • 6,514
  • 2
  • 32
  • 44