git merge and rebase, and the concept of stash

git merge and rebase, and the concept of stash

Source https://git-scm.com/book/en/v2/Git-Branching-Rebasing
https://www.jianshu.com/p/c17472d704a0

In git, there are two ways to integrate changes from one branch to another.
branch
Assume that the current branch is like this, the difference between merge and rebase:

merge

$ git checkout master
$ git merge experiment

Switch to the master branch and merge the experiment changes into the master branch
merge

fox

$ git checkout experiment
$ git rebase master

Switch to the experiment branch and insert the modifications to the master branch before the experiment modifications.
fox

Application scenarios

Merge is suitable for merging into the main version after the branch development is completed?
Is rebase suitable for synchronizing master modifications to branches during branch development? However, if you rebase, you cannot truly see the modification process of the branch. For example, the a modification of the branch will become a' modification after rebase, and the modification date will change.

important point

  • The rebase operation will discard the commits submitted by the current branch, so do not perform the rebase operation on a branch that has been pushed to the remote and is being developed in collaboration with others.
  • When synchronizing with the remote warehouse, the pull command performs two operations of git fetch + git merge --no-ff by default. You can change the merge operation after fetch to a rebase operation by adding the --rebase command. Or just fetch
  • When there are modifications that have not been committed, the rebase operation cannot be performed. At this time, you can consider using the git stash command to temporarily store it.

stash

Switching branches requires submitting all changes to the current branch, but the development may not be completed yet and you do not want to submit. In this case, you can use stash.

git stash

You can use the following command to see what you stash

git stash list

To restore the contents of the last stash, you can use apply

git stash apply

If you want to restore a certain time, add the number you see in the list

git stash apply stash@{1}

Guess you like

Origin blog.csdn.net/ZhaoBuDaoFangXia/article/details/88817935