GDYendell
GDYendell

Reputation: 386

Git rebase to stop just before each commit to then edit and rebase --continue

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 then 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!

Upvotes: 19

Views: 9604

Answers (4)

GDYendell
GDYendell

Reputation: 386

This is not really an answer, but my alternate solution for the original goal is now just doing the rebase and then editing the commits using lazygit.

Upvotes: 1

hspandher
hspandher

Reputation: 16733

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 # <branch> should be the parent branch i.e. develop/master

Rewriting git history

# for instance
>>> git rebase -i branch
pick 8705209 first
edit 7979797 second # Use edit wherever you want to view and edit commit
squash 9793757 third # Use squash to squash commits into the previous one.

Upvotes: 15

guest
guest

Reputation: 6698

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.

Upvotes: 5

kan
kan

Reputation: 28951

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

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

Upvotes: 4

Related Questions