How to modify a specified commit?

后端 未结 16 929
清酒与你
清酒与你 2020-11-22 01:53

I usually submit a list of commits for review. If I have the following commits:

  1. HEAD
  2. Commit3
  3. Commit2
16条回答
  •  旧巷少年郎
    2020-11-22 02:49

    The best option is to use "Interactive rebase command".

    The git rebase command is incredibly powerful. It allows you to edit commit messages, combine commits, reorder them ...etc.

    Every time you rebase a commit a new SHA will be created for each commit regardless of the content will be changed or not! You should be careful when to use this command cause it may have drastic implications especially if you work in collaboration with other developers. They may start working with your commit while you're rebasing some. After you force to push the commits they will be out of sync and you may find out later in a messy situation. So be careful!

    It's recommended to create a backup branch before rebasing so whenever you find things out of control you can return back to the previous state.

    Now how to use this command?

    git rebase -i  
    

    -i stand for "interactive". Note that you can perform a rebase in non-interactive mode. ex:

    #interactivly rebase the n commits from the current position, n is a given number(2,3 ...etc)
    git rebase -i HEAD~n 
    

    HEAD indicates your current location(can be also branch name or commit SHA). The ~n means "n beforeé, so HEAD~n will be the list of "n" commits before the one you are currently on.

    git rebase has different command like:

    • p or pick to keep commit as it is.
    • r or reword: to keep the commit's content but alter the commit message.
    • s or squash: to combine this commit's changes into the previous commit(the commit above it in the list).
    • ... etc.

      Note: It's better to get Git working with your code editor to make things simpler. Like for example if you use visual code you can add like this git config --global core.editor "code --wait". Or you can search in Google how to associate you preferred your code editor with GIT.

    Example of git rebase

    I wanted to change the last 2 commits I did so I process like this:

    1. Display the current commits:
      #This to show all the commits on one line
      $git log --oneline
      4f3d0c8 (HEAD -> documentation) docs: Add project description and included files"
      4d95e08 docs: Add created date and project title"
      eaf7978 (origin/master , origin/HEAD, master) Inital commit
      46a5819 Create README.md
      
    2. Now I use git rebase to change the 2 last commits messages: $git rebase -i HEAD~2 It opens the code editor and show this:

      pick 4d95e08 docs: Add created date and project title
      pick 4f3d0c8 docs: Add project description and included files
      
      # Rebase eaf7978..4f3d0c8 onto eaf7978 (2 commands)
      #
      # Commands:
      # p, pick  = use commit
      # r, reword  = use commit, but edit the commit message
      ...
      

      Since I want to change the commit message for this 2 commits. So I will type r or reword in place of pick. Then Save the file and close the tab. Note that rebase is executed in a multi-step process so the next step is to update the messages. Note also that the commits are displayed in reverse chronological order so the last commit is displayed in that one and the first commit in the first line and so forth.

    3. Update the messages: Update the first message:

      docs: Add created date and project title to the documentation "README.md"
      
      # Please enter the commit message for your changes. Lines starting
      # with '#' will be ignored, and an empty message aborts the commit.
      ...
      

      save and close Edit the second message

      docs: Add project description and included files to the documentation "README.md"
      
      # Please enter the commit message for your changes. Lines starting
      # with '#' will be ignored, and an empty message aborts the commit.
      ...
      

      save and close.

    4. You will get a message like this by the end of the rebase: Successfully rebased and updated refs/heads/documentation which means that you succeed. You can display the changes:

      5dff827 (HEAD -> documentation) docs: Add project description and included files to the documentation "README.md"
      4585c68 docs: Add created date and project title to the documentation "README.md"
      eaf7978 (origin/master, origin/HEAD, master) Inital commit
      46a5819 Create README.md
      

      I wish that may help the new users :).

提交回复
热议问题