jnicklas
jnicklas

Reputation: 2225

Make adjustment to previous commit without recreating commit

I used to use gitx to stage changes for commit, but I have switched to a purely terminal workflow + Fugitive.vim. One feature that I really miss from gitx though is the ability to make an adjustment in a previous commit. For example, if some section was added in a previous commit, but shouldn't have been, gitx makes it really easy to pull out that section and just amend the previous commit.

My current workflow for making such a change is:

  1. git show
  2. Copy the commit message
  3. git reset --soft HEAD^
  4. Use Fugitive or git add -i to make whatever change I want to make
  5. git commit
  6. Paste the previously copied commit message
  7. Confirm

What previously was a convenient two step process has now become pretty cumbersome. Is there some other way to do this that I'm missing?

git commit --amend -CHEAD --interactive seemed promising to me, but it doesn't seem to work at all.

Upvotes: 1

Views: 838

Answers (2)

IMSoP
IMSoP

Reputation: 97996

The git command line doesn't know anything about editing files, so you're always going to have two steps: make the change, and apply that change to some commit.

If the commit you want to amend is the most recent on the branch, you can use two of the options to git commit:

--amend: Replace the tip of the current branch by creating a new commit ... the message from the original commit is used as the starting point, instead of an empty message

--no-edit: Use the selected commit message without launching an editor. For example, git commit --amend --no-edit amends a commit without changing its commit message.

If you want to amend a commit deeper in history, you need to use git rebase --interactive and change the command against the commit from "pick" to "edit". You then do the same thing, but once you're finished, you run git rebase --continue so that git replays the changes after that point.

Upvotes: 0

Antonio Petricca
Antonio Petricca

Reputation: 11050

To accomplish the same task I use the following approach:


# Find the starting commit where I need to place changes

git log --oneline --graph --decorate

# Ask for editing commit(s)

git rebase -i {{commit-hash-from-log-history}}

# Enter "edit" for commit(s) you want change, then save
# Add, Edit or Remove files

# Finalize editing

git rebase --continue

# Or, if you want to abort

git rebase --abort

Please, tell me if it works for you too.

Upvotes: 1

Related Questions