1. 程式人生 > 其它 >git commad —— git reset —— 修改branch所在的提交

git commad —— git reset —— 修改branch所在的提交

# checkout 遠端分支hotfix/v3.3.1_20378_website

$ git reset --hard f93b9e23dee9ff0a81ece8a45d572a000d6c86a8

# 強制遠端分支推送。否則如果 commit較新,則會自動 提示需要提交commit
$ git push -f origin hotfix/v3.3.1_20378_website


  

如果merge 回滾,

先git merge --abort(如果已經commit,未push到remote,則 cancle commit first,然後 merge --abort)

或者 git merge revert

git reset 是最後的方式,其他方式可以完成的,有限使用其他方式。因為會影響 team中其他人的開發。(通知team其他人,刪除本地分支,重新 checkout 遠端分支:hotfix/v3.3.1_20378_websit),因其他人 本地的 commit 是舊的,可能commit 比較新,則可能在無意識的情況下,提交 之前 丟棄的 commit

$ git status

$ git cherry-pick 038da250553e4f32752a23d56a01cc2696ec1410


$ git push

$ git cherry-pick ac378fcef8345b3241e94e5713755a4a3fedd848

# 迴圈

# 通知team其他人,刪除本地分支,重新 checkout 遠端分支:hotfix/v3.3.1_20378_website

  

4 Easy Steps to Change Author Name of a Commit After Push

  1. Rebase the repository to the previous commit of the one you want to change by running:
    git rebase –i {{previous-commit-hash}}
  2. The script above prompts you with a list of your commits in descendent order. On this vi/vim view, replace the wordpick
    toeditper each commit you want to edit. Then quit and save.
  3. When the rebase process starts, change the author of a commit by runninggit commit --amend --author="Author<[email protected]>". Then, continue to next commit using: git rebase –continue
  4. Once the rebase process finishes, push your changes by running:git push -f

The steps above will change the author of a commit. But what is the meaning of each commands above and what it means to change a git repository’s history?

I came across this question recently after changing the settings on GitHub page and seen that commits were logged with invalid an invalid name. Reading throughGit’s documentationmade it tricky to find the answer. ThisStack Overflow answernail it but I still decided to write about it to review the concepts behind scenes.

What does the rebase commit do?

The rebase command basically integrates changes from one branch into another. It is an alternative to the “merge” command. The difference between rebase and merge is that rebase rewrites the commit history and creates a linear succession of commits, while merging adds a new commit to the destination branch. The image bellow perfectly shows a visual representation of both processes.

Git amend command

The amend command allows git users to change details of a commit. The amend structure is simple:
git commit –amend

This will prompt a vi/vim view where the details of a git commit can be changed:

update changelog file
# Please enter the commit message for your changes. Lines starting
# with ‘#’ will be ignored, and an empty message aborts the commit.
#
# Author: Author Name < [email protected] >
# Date: Thu Jan 10 22:40:30 2020 -0300
#
# On branch some_branch
# Your branch is up-to-date with ‘origin/some_branch’.
#
# Changes to be committed:
# modified: changelog.md

It is possible to change the author of a git commit directly as seen before:
git commit --amend --author="Author < [email protected] > "

Change git history… Caution!

Those who are familiar with rebasing know how powerful the tool it is. It might be tempting to use it all the time as well.

When getting conflicts during a rebase, Git pauses on the conflicting commit and allows to fix conflict before proceeding. However, solving conflicts in the middle of a long chain of commits is often confusing and another source of potential errors.

Rebasing creates this linear mind-set and give less priority to the actual goal of git. That is why it is not recommended to change the history of a git repository unless there is no alternative to it.

In my case, I was the only user interacting with the repository so it was safe. However, when multiple developers are working on multiple branches, change to the git history can become a real source of problems.

References