What is the difference between “git reset” vs “git rebase”?

后端 未结 2 1060
执笔经年
执笔经年 2021-01-30 06:33

I have been playing around with git (still very noob) and I wanted to know the difference between \"reset\" and \"rebase\". Is the one more powerful than the other?

Say

2条回答
  •  庸人自扰
    2021-01-30 07:03

    They are completely different. git-reset works with refs, on your working directory and the index, without touching any commit objects (or other objects). git-rebase on the other hand is used to rewrite previously made commit objects.

    So if you want to rewrite the history, git-rebase is what you want. Note that you should never rewrite history that was pushed and was available to someone else, as rebasing rewrites the objects making them incompatible with the old objects, resulting in a mess for anyone else involved.

    That being said, what you want to do is interactive rebasing. Invoke it using git rebase -i 262d1f7 and you should get a prompt looking like this:

    pick 262d1f7 Added HTML header (v1)
    pick a006669 Oops, we didn't want this commit
    pick a6faf60 Revert "Oops, we didn't want this commit"
    pick b7b5fce This reverts commit a6faf60631b5fbc6ee79b52a1bdac4c971b69ef8.
    pick cdb39b0 Commit p tags with text (v1.1)
    pick 9b2f3ce Added an author comment
    pick 55649c3 Add an author/email comment
    pick 0b1dd4c Moved hello.html to lib
    pick 5854339 Added index.html
    pick 801e13e Added README
    pick a6792e4 Added css stylesheet
    pick 17a64df Hello uses style.css (HEAD, origin/style, master),
    

    There, simply delete the lines for the commits you want to remove, save and exit the editor and Git will rewrite your history. Again, don’t do this if you already pushed the changes. In general having such commits in the history is perfectly fine.

提交回复
热议问题