Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncommit all commits in current branch but leave all changes at the current state

Tags:

git

I have a branch A in my repo with 10 commits. What I want is to find a way to delete all commits but leave all changes in the current branch (so the state of the code would be the same but all commits in A would be gone). The purpose is that I now can go over all the changes and cerry pick the once I want and structure better commit messages before I merge back into master. Is this flow even possible?

like image 404
user3139545 Avatar asked Apr 07 '17 09:04

user3139545


People also ask

How do you Unstage commits but keep changes?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case.

Can I change branch with uncommitted changes?

You may switch branches with uncommitted changes in the work-tree if and only if said switching does not require clobbering those changes.


2 Answers

git reset master will change HEAD and the index to be the same state as master, but leave the working tree untouched (e.g. the current state).

like image 179
ephemient Avatar answered Oct 04 '22 03:10

ephemient


git reset --soft <commit-id> will do what you need. It will move the HEAD to point to but won't change the state of the working tree (so it will leave your files alone). Find out what the commit hash is for the start of A using the log command.

Check out the docs here if you want to find out more about reset.

like image 26
mamapitufo Avatar answered Oct 04 '22 03:10

mamapitufo