Home > database >  Is there any way to edit and push the commit message of previous commit if I already have push the p
Is there any way to edit and push the commit message of previous commit if I already have push the p

Time:02-16

Things which I have done:

  1. I have added and committed the file1.py and pushed it to GitHub.
  2. I have created many files onwards that and push that all to GitHub.

Thing what I have to do

I want to edit the commit message of file1.py and push it to GitHub.

What I did to edit and push commit

git checkout file1.py (used hash)

git commit --amend (edited commit)

git push origin master --force-with-lease

But while going to master branch and checking the log then it's showing the same previous one commit. I checked in GitHub but the commit is not changed there too.

Please help me!!!

CodePudding user response:

Suppose you have the following commits:

$ git log
    
    commit 0a1dd64f87729209a92d50341f7e4de89d773371 (HEAD -> master)
        Author: 
        Date:   Wed Feb 16 11:59:27 2022  0100
        
            add file2.py
        
        commit 6399325dedef67714654660a36e2765aae005b13
        Author: 
        Date:   Wed Feb 16 11:59:13 2022  0100
        
            add file1.py
        
        commit ef87594ad7caec773fd2c00a2fa18cdd96def021
        Author: 
        Date:   Wed Feb 16 11:58:55 2022  0100
        
            init

Now perform an interactive rebase onto the commit right before the one you want to rename. Here we are rebasing onto init. You will be prompted to choose what action to perform on each commit. Choose reword instead of pick on the commit you want to rename. When you press enter, you will be prompted to choose the new commit name.

$ git rebase -i ef87594ad7caec773fd2c00a2fa18cdd96def021

        pick 6399325 add file1.py
        pick 0a1dd64 add file2.py
    
# Reword file1.py :
    
        reword 6399325 add file1.py
        pick 0a1dd64 add file2.pygit log

Now the results: beware that the commit hashes changed. You cannot push this on master if other people are working on the same branch, as this would break your history.

$ git log 
        commit 44f54a1fbccde9796a795e1c4e17a873c9d5d29d (HEAD -> master)
        Author: 
        Date:   Wed Feb 16 11:59:27 2022  0100
        
            add file2.py
        
        commit ac73685a4d249137d4ee7566dfbbbcf6c890297c
        Author: 
        Date:   Wed Feb 16 11:59:13 2022  0100
        
            add new name for file1 commit
        
        commit ef87594ad7caec773fd2c00a2fa18cdd96def021
        Author: 
        Date:   Wed Feb 16 11:58:55 2022  0100
        
            init

CodePudding user response:

Ground of being: Commits in Git cannot be changed in any way. You cannot really change a commit's message. What you can do is make a new commit that looks just like the old commit except that it has a different message.

So, what you did actually worked perfectly. You made an amended commit and you pushed it. But this is a new and different commit! And you did not replace the original commit with the new and different amended commit, so you cannot find the amended commit! It exists but is not useful to you. This is your situation:

A -- B -- C -- D -- E -- F (myBranch)
           \
            -- DAmended

You created and pushed the DAamended commit. But this is not useful to you because it's just sitting there in space serving no purpose.

To be useful, you would need your amended commit to occupy the same spot in the sequence of your overall history that the original commit now still occupies. To do that, you would need to rebase all the commits after the original commit onto the amended commit.

So in the diagram, you need E to change its parentage so that DAmended is its parent, and you need F to come along with that. But you cannot change the parentage of a commit! You have to make a new copy of the commit, one that has a different parent. That is what rebasing does. After you rebase, you will have this:

A -- B -- C -- D -- E -- F
           \
            -- DAmended -- Ecopy --Fcopy (myBranch)

The old D, E, and F will now be permitted to die, and your actual branch will look the way you want it to.

  • Related