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

后端 未结 2 1087
执笔经年
执笔经年 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 06:56

    I hope this layman explanation is correct.

    Both git reset and git rebase will affect your local branch. They will force your local branch to be in sync with a certain commit. The difference is that:

    1. "git reset --hard {commit-id}" will use a commit in local history.
    2. "git rebase origin/{branch-name}" will use the latest commit in the repo

    Additional Info

    When to use reset?

    Let's say you finish your work and commit locally. Then your cat walk across the keyboard, and somehow you carelessly commit your cat's work. Use reset.

    When to use rebase?

    Let's say you refactor the name of a function/class and this change affect many files. When you commit and try to push to origin, you realized your colleague has make some important changes and already pushed it to origin. Let's say you think refactoring the name again (using an IDE) is easier than going through all the conflict files, you can choose to rebase which will erase your work and keep your colleague's one untouched.

    Why is every answer/tutorial contains so much technical disclaimer?

    Because both reset and rebase can delete local changes forever. So users will need to know how to employ strategies (e.g. create a backup branch) to keep their work.

提交回复
热议问题