Git stash and solving Git stash conflicts

Git stash

Adapt to the scene:

When using git, we often use branches to solve task switching problems. For example, we often build a branch of our own to modify and debug code. , We often submit the half-completed code committo the local warehouse, then switch branches to modify bugs, and then switch back after the changes are completed. In this case, there will often be a large number of unnecessary records on the log. In fact, if we don't want to submit half-completed or imperfect code, but have to modify an emergency bug, then git stashusing Your work area is exactly the same as the last submitted content, so you can fix bugs with confidence, wait until the bugs are fixed and submitted to the server, and then apply half of the previous work back git stash apply.

git stash
git checkout master

# do your job

# when your job's done
git checkout feature_branch
git stash pop

Commonly used git stash commands:

  • git stash  save "save message" : When performing storage, add a note for easy search. Only git stash is also available, but it is not easy to identify when searching.
  • git stash list   : Check which storage is stash
  • git stash show  : Show what changes have been made, the default show is the first storage, if you want to display other storage, add stash@{$num} after it, such as the second git stash show stash@{1}
  • git stash show -p  : Display the changes of the first storage. If you want to display other storages, command: git stash show stash@{$num} -p , such as the second one: git stash show stash@{1} -p
  • git stash apply  : Apply a certain storage, but will not delete the storage from the storage list. By default, the first storage is used, namely stash@{0}. If you want to use other ones, git stash apply stash@{$num}, For example, the second one: git stash apply stash@{1} 
  • git stash pop  : The command restores the previously cached working directory, deletes the corresponding stash in the cache stack, and applies the corresponding modification to the current working directory. The default is the first stash, namely stash@{0}, if you want to apply And delete other stash, command: git stash pop stash@{$num}, such as apply and delete the second one: git stash pop stash@{1}
  • git stash drop  stash@{$num} : drop stash@{$num} stash, remove this stash from the list
  • git stash Clear removes all cached stash

 Git stash conflict resolution

For the scenario mentioned above, it is assumed that some codes have been modified in the workspace to support new functions. Because it is still being perfected, I don’t want to submit the code for the time being. At this time, I found that my colleague submitted the code of the new function, and I needed to incorporate his code to complete the work.

git stash
git pull --all
git rebase college_branch
git stash pop

If there is a conflict between my colleagues and my modified code, the last command will Merge conflict report an error .

It's a pity: Git does not provide the function of forcing pop out.

git stash show -p | git apply && git stash drop

reference:

Solve the conflict problem of Git stash | fish longing for freedom

Summary of git-stash usage - Tocy - 博客园

Guess you like

Origin blog.csdn.net/qq_19446965/article/details/122026888