Search code examples
gitgit-push

changing the files in my git commit after performing a Git Push Command


so we make use of GitLab and so I had to send out a mergereq request. It was during the mergereq I realised that I had done a git commit of additional files " temporary and unrequired "; Is there a way of undoing this ? I have tried

git commit --amend

But this changes only the commit message whereas I need to remove the unnecessary files as well.

Is this possible?


Solution

  • No, git commit --amend does allow you to add new modifications to the last commit. The case in which it only updates the commit message is when you use the -m parameter.

    But rather than trying to rm unwanted files from the index, I'd suggest doing the following from your local branch* :

    # undo last commit while keeping changes in the working tree
    git reset HEAD^
    
    # redo the adding part without the unwanted files
    git add file1 file2 file3
    
    # commit and push
    git commit -m "message"
    git push --force origin HEAD
    

    Now just refresh your PR page, it will update itself, replacing the faulty commit with the new one.

    (* assuming this is your feature branch, on which you can force-push without disrupting someone else's work. Correct me if I'm wrong here)