[git] Solve the problem of inconsistency between the local warehouse and the remote warehouse

Table of contents

Method 1: (not verified)

Method 2: (verified)

Method 3: Conventional

1. Pull the remote warehouse code to the local as a new local branch temp

2. Compare the branch of this temp with our main branch to see what is different and what are the conflicts

3. Merge the new temp branch with the local master branch

4. Resubmit the local master branch to the remote warehouse

5. Delete branch

Method 4. When no commit is made

 1. Store all changes under the current branch

2. View existing stash

3. Apply the specified stash to the workspace, but do not delete the stash

4. Expansion:

4.1 Release the last saved content, and pop the saved content out of the stack (that is, pop the top of the stack, release the last saved content and delete the saved content)

4.2 Store specified file modifications

4.3 Check which files have been modified by a push stack

4.4 View the modified content of the specified storage

4.5 Delete all cached stash


Forget to pull the code to the remote warehouse, use pull to report an error:error: Your local changes to the following files would be overwritten by merge:

If you don't want the code you just wrote to be overwritten, you can solve it like this:

Method 1: (not verified)

If you want to keep the code you just modified locally, and pull the code on the git server to the local (the code you just modified locally will be temporarily archived)

git stash
git pull origin master
git stash pop

In this way, the code on the server is updated locally, and the code you modified locally is not overwritten. Afterwards, use the , command addto  update the local code to the server.commitpush

Method 2: (verified)

If you want to completely cover the local code and only keep the server-side code, then go back to the previous version directly, and then proceed pull:

git reset --hard
git pull origin master

Note: where origin master represents the main branch of git.

Method 3: Conventional

1. Pull the remote warehouse code to the local as a new local branch temp

git fetch origin master:temp 

 expand:

  git branch without parameters: list the branches that already exist locally, and add a "*" mark in front of the current branch.

  git branch -r lists remote branches, for example:

  git branch -a lists local and remote branches, for example:

  

  git branch -m | -M oldbranch newbranch Rename the branch, if the newbranch name branch already exists, you need to use -M to force the rename, otherwise, use -m to rename.

2. Compare the branch of this temp with our main branch to see what is different and what are the conflicts

git diff temp

3. Merge the new temp branch with the local master branch

git merge temp

If there are conflicting files that cannot be merged automatically, you need to open and modify them yourself. The basic format of the conflict file is <<<<<<< to ======= is the content of the file before the current branch is merged, and ======= to >>>>>>> is in other branches For the revised content, you need to choose one of these two versions, and then delete the mark symbols together.

After manually resolving conflicts, you can push to the remote branch.
 

4. Resubmit the local master branch to the remote warehouse

git push -u origin master

5. Delete branch

git branch --delete temp
# 命令删除本地分支

# 这个好像也可以
git branch -D temp  //删除本地temp分支

extension:

Use the "git push origin --delete branch" command to delete the remote branch;

Use the "git branch --delete --remotes" command to delete the tracking branch.

Method 4. When no commit is made

Usually we will encounter such a scenario. We are implementing a function but have not finished it. At this time, we need to modify the bug. We don’t want to submit our modification now to add too many useless log records, but we can’t go back if we don’t submit it. to this state. At this time, you can use the git sta sh command to save the contents of the local workspace and return to the state after the previous commit.

 The following is a small tidying up, first look at the 1-2-3-4 part below, and then look at this command

# 保存当前未commit的代码
git stash

# 保存当前未commit的代码并添加备注
git stash save "备注的内容"

# 列出stash的所有记录
git stash list

# 删除stash的所有记录
git stash clear

# 应用最近一次的stash
git stash apply

# 应用最近一次的stash,随后删除该记录
git stash pop

# 删除最近的一次stash
git stash drop

 1. Store all changes under the current branch

git stash save -m "标识注释"

2. View existing stash

git stash list

3. Apply the specified stash to the workspace, but do not delete the stash

git stash apply 对应stash的名字  # 上面的标识

# 或者
 git stash apply stash@{0}


删除则是将apply换成drop

expand:

By default, git stash caches the following files:

        Modifications added to the staging area
        Modifications tracked by Git but not added to the staging area

But the following files are not cached:


        Files that are ignored for         new files in the working directory

4. Expansion:

4.1 Release the last saved content, and pop the saved content out of the stack (that is, pop the top of the stack, release the last saved content and delete the saved content)

git stash pop

4.2 Store specified file modifications

git stash push .../.../ .../.../ .../.../

     /.../... is the modified file path you want to store. When you have modified many files, but you only want to store some modified files, you can add the path after git stash push. If there are multiple files, just add spaces between these file paths

4.3 Check which files have been modified by a push stack

When there are too many modifications in the storage, you may forget what files have been modified in a certain storage. If you want to check which files have been modified in a certain storage, you can use the git stash show stash@{0} command, stash@{0} is the most recently stored Modify, if you want to see other storage, you only need to change the number of stash@{0}, such as stash@{5}. As shown in the above picture, I first use git stash list to view the storage list, and then use the git stash show stash@{0} command Looking at the first modification, you can see that I only modified one file test.txt in the most recent modification store.

git stash show stash@{0} 

4.4 View the modified content of the specified storage

If you want to view all the contents of a stash modification, instead of just viewing what files have been modified, for example, to view all the files and contents of the latest stash modification, you can use the git stash show -p stash@{0} command to view.

git stash show -p stash@{0}

4.5 Delete all cached stash

git stash clear

Guess you like

Origin blog.csdn.net/legend818/article/details/130065179