1

I need to change my name and email in several commits. So I started with the oldest one. The question is: after changing author of the commit can I proceed with changing author for all remaining commits or should I do git push --force-with-lease after each author change?

How can I first change author and push only when author in all the required commits is edited?

git checkout 12345c3

git commit --amend --author "New name <name@new.com>"

Checkout the original branch.

git checkout MyBranch

git replace 12345c3 cd3c123

git filter-branch -- --all

git replace -d 12345c3
samba
  • 2,821
  • 6
  • 30
  • 85
  • 1
    You don't need to push every time. Just push once after everything's done. – iBug Aug 21 '18 at 15:15
  • @iBug I tried to checkout a new commit that needs an author change after git `replace -d 12345c3` I got the following message: `Cannot create a new backup. A previous backup already exists in refs/original/ Force overwriting the backup with -f` Not sure how to iterate the renaming commands correctly – samba Aug 21 '18 at 15:17
  • You'll probably need to create a temp branch, and cherry pick/amend the commits onto it one by one – Mad Physicist Aug 21 '18 at 15:25
  • A better alternative may be .mailmap. If you're open to that, if be happy to write it up. – Mad Physicist Aug 21 '18 at 15:25
  • See here https://github.com/git/git/blob/master/Documentation/mailmap.txt – Mad Physicist Aug 21 '18 at 15:28
  • @samba, did my answer solved your issue? If not, could you explain why? – Arthur Senna Mar 16 '19 at 12:39

2 Answers2

4

The best way to edit multiple commits is with git rebase.

Using rebase you wouldn't even need to checkout to each commit you want to edit. All you would need to do is

git rebase -i <the oldest commit you want to edit>

The -i will open a text editor listing all commits up to the commit you passed. So you wold see someting like this:

pick 2e0ed56d Message 1
pick f38dd6ed Message 2
pick 5518e03e Message 3

# Rebase d29d7e11..2283c3c9 onto d29d7e11 (5 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted. 
#
# Note that empty commits are commented out

Now all you need to do is replace the pick in front of each commit you want to amment with e or edit save you changes and close the file. You will now be asked to ammend each commit individually.

Now be warned: The way git rebase works is by deleting your commits and creating copies of them with your changes. So be very careful if you are sharing the repository with other people.

You can use

git push -f

if you want to push your changes to a server and discart the previous versions of thoses commits. Just make sure no one else you are working with has pulled your current branch.

Arthur Senna
  • 331
  • 3
  • 5
0

If the commits that you want to edit have already been integrated into a shared branch (shared among multiple developers), then you will be required to force push (-f) the updated branch. This is generally not something you want to do unless the entire team is on board. You are not really editing the commits, because commits are immutable. You are creating new commits with different information.

You could use this (filter-branch) approach. Remember, this will re-write every commit back to the oldest commit you change. You will want to update everything and then push a single time (especially if it is a shared branch).

Bill
  • 1,407
  • 10
  • 18