dKab
dKab

Reputation: 2848

How to undo git branch and git commit

I was woking on a feature and it was getting late. I wanted to save my work to VCS. I should've used stash but I forgot about it. I didn't want to commit half-done feature, so I decided to create new local branch and save my work there. So I did git branch newlocalbranch and git commit -m 'in the middle of task'; But then I realized, that when I will merge this local-branch into my official feature-dedicated branch (with task id) that invalid commit would still appear in my feature branch. What I want now, is to go back in time to the point where I am in my feature branch with all these changes that I've made. I would then stash them and go home.

Upvotes: 1

Views: 2398

Answers (2)

Vasfed
Vasfed

Reputation: 18454

After merging your newlocalbranch into feature branch and commiting finished work, but before pushing you can squash/fixup commits into one. See git guide on this

Upvotes: 0

poke
poke

Reputation: 387667

You can just do a soft-reset to the branch you branched off from. That way, you will reset the branch pointer to that branch, but keep all made changes. So afterwards, you can continue working on it and then create a proper commit.

So if you came from master before, this would be the command:

git reset --soft master

That would be for a situation like this:

          master
            ↓
* --- * --- *
             \
              \
               * [in the middle of task]
               ↑
         newlocalbranch

The alternative to that would be to continue working on the branch until you complete the work, and then amending that “in the middle of task” commit so it contains the full change and has a proper name. For that you would do git commit --amend --edit. This will basically replace that “in the middle of task” commit with a new one containing all changes from the original commit and whatever you added to the index (Usually, the command is used to fix the last commit when you forgot something). Afterwards, you could do a normal fast-forward merge back onto master and that new refined commit would be the one that appeared in the history.

Upvotes: 3

Related Questions