Solve the trouble of finding that the remote branch has been updated after the local code commit!

Solve the trouble of remote branch update after local code commit!

  In the process of code development, when we prepare to push the local code to the remote branch, sometimes we encounter the situation that the remote branch has been updated. This poses some challenges for our development work, because we need to ensure that our modifications are consistent with the updates of the remote branch, so as not to cause code conflicts or overwrite other people's modifications.

Step 1: Undo the last commit and undo the temporary storage operation

  If you have executed git committhe command and you find that the remote branch has been updated when you are ready to push the code to the remote branch, you can use the following command to undo the last commit operation and cancel the added files to the temporary storage area:

git reset HEAD~

  This command will make HEAD point to the last submitted version, and undo the temporary storage operation for this submission, which is equivalent to returning to the state before modifying the code.

Step 2: Use git stash to temporarily store code modifications

  Another way to deal with it is to use git stashthe command to temporarily save the currently modified code. This command will hide uncommitted changes for subsequent processing.

  First, execute the following command to temporarily save the current code modification:

git stash

  Then, pull the latest code from the remote branch with the following command:

git pull origin 分支名

  Next, unstage the code modification with the following command:

git stash pop

  This way your changes will be reverted to the workspace and merged with the updates from the remote branch.

Step 3: Submit the code normally

  If you've handled updating the remote branch without any conflicts, then you can continue committing your code normally. Commit the modified code to the remote branch with the following command:

git commit -m "提交信息"
git push origin 分支名

Summarize

  When we are preparing to push the code to the remote branch locally, we find that the remote branch has been updated. We can choose to undo the last commit and undo the temporary storage operation, or use git stash to temporarily store the code modification. After processing updates to the remote branch, we can continue committing and pushing code as normal. In this way, we can ensure that the local code is consistent with the update of the remote branch, avoiding code conflicts and overwriting other people's modifications.

Guess you like

Origin blog.csdn.net/java_cpp_/article/details/132210464