Monday, May 15, 2017

Git rebase to stop, view and edit each on commit

Leave a Comment

I notice when I rebase and have a merge conflict, it has the staged changes for the current commit, so it is easy to find what was done in the commit and change it, but then you can also just do 'git rebase --continue' and it will apply the change with the same commit message as before.

Is there a way to force this to happen for specific commits, or all commits, so I can easily rebase and edit the previous changes I made to work with the changes I have rebased on? It seems like this would be much easier than just setting edit on all the commits and then doing 'git reset --soft HEAD~', so that the changes are visible and I can check they make sense for the new HEAD, editing, and the having to do 'git commit -m "message" '.

My use case is that the repository I am working with has had its file structure heavily refactored and git is unable to make the appropriate changes, but says the merge is successful.

Thanks!

3 Answers

Answers 1

I think what you are looking for is interactive rebasing and rewriting git history. It will let you squash commits as well as change the commit messages.

git rebase -i branch 

Rewriting git history

Answers 2

Just do git commit --amend before git rebase --continue.

Alternatively - use git-cherry-pick --no-commit commit1..commitN.

Answers 3

force this to happen for specific commits

Start an interactive rebase with git rebase -i HEAD~~~.

This opens an editor with a list of commits:

pick 1234567 fix stuff pick 789abcd new features pick 0102030 break everything 

Change the pick to edit on a commit (or commits) that you want to edit. Save, and exit the editor.

Then, git will bring you back to these commits, one by one, and you can edit them. I think what you're missing is that, at this point, you can view diffs that are staged, with git diff --cached.

After you've edited each one, use git rebase --continue to continue.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment