0

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.

jnicklas
  • 2,155
  • 2
  • 16
  • 14
  • You can still use gitx just for that. – romainl Jun 16 '21 at 13:53
  • 2
    As a general tip, "it doesn't seem to work at all" isn't a very helpful phrase, because it doesn't tell us anything about what actually happened. Did you get an error? Did it do something other than what you expected? Did nothing appear to happen at all? – IMSoP Jun 16 '21 at 13:54

2 Answers2

1

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.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • I don't think this achieves the same thing. Using `edit` in rebase simply stops at that commit, if I want to adjust the commit, I still need to make the changes manually and then save/commit, so I'm kind of back in the same situation. – jnicklas Jun 16 '21 at 11:22
  • I used to use gitx's "amend" feature *together* with `edit` quite a lot, so they are workflows which complement each other, but not replace each other. – jnicklas Jun 16 '21 at 11:23
0

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.

IMSoP
  • 89,526
  • 13
  • 117
  • 169