git branch management and conflict resolution

1. Branch management

 

1. View where the current branch is located

 git branch

 

2, switch to the (existing) specified branch

git checkout <name of existing branch>

 

Note: If there is content in the local staging area (that is, you have run the git add command, or use git status to view, there is a green file), the switch will fail, and you need to submit the staging area content or discard the staging area content. to switch.

 

3. Create a new branch with the current local branch as the starting point

git branch <name of new branch to create>

 

Note: It is also possible that the creation fails for the same reason as the switch branch function.

 

4. Using the local current branch as the starting point, create a new branch and switch to the new branch

git checkout -b <new branch name to create and switch to>

Actually this command is a combined command:

(1) git branch <name of new branch to create>

(2) git checkout <existing branch name>

 

5. Using the remote branch of the remote warehouse as the starting point, create a new branch and switch to the new branch

git checkout -b <new branch name to create and switch to> <remote repository name>/<remote branch name>

 

E.g:

git checkout -b develop origin/develop

The first develop is the name of the local branch to create, origin is the name of the remote repository, and the second develop is the name of the remote branch

 

When pulling content from a remote, just use git pull directly without specifying the remote repository and branch name

When pushing content from local to remote, just use git push directly without specifying the remote repository and branch name

 

You can list the aliases and addresses of multiple remote repositories through git remote -v

 

6. Associate the local branch with the remote branch setting (tracking)

git branch --set-upstream-to=<remote repository name>/<remote branch name> <local branch name>

 

E.g

git branch —set-upstream-to=origin/develop develop

origin is the name of the remote warehouse, the first develop is the name of the remote branch, and the second develop is the name of the branch created locally

 

When pulling content from a remote, just use git pull directly without specifying the remote repository and branch name

When pushing content from local to remote, just use git push directly without specifying the remote repository and branch name

 

 

7. View the tracking relationship between the local branch and the remote branch

git branch -vv

 

! Note that there are two v

 

8. Merge other branches into the current branch

git merge <name of branch to be merged into>

 

9. Pull content from remote repository

(1) git fetch <remote warehouse name>

Retrieve all the latest modifications of all branches of the remote warehouse back to the local

(2) git fetch <remote warehouse name> <remote branch name>

Retrieve the latest modification of the specified branch of the remote warehouse back to the local

 

 

10. Pull the remote branch of the remote warehouse and merge it into the current branch

git pull <remote hostname> <remote branch name>:<local branch name>

 

Since the remote branch has the same name as the local branch, it can be simplified to

git pull <remote hostname> <remote branch name>

 

When the associated remote branch corresponding to the local branch is set, it can be simplified to

git pull

 

Note:

The git pull command is a combined command, equivalent to

(1) git fetch <remote warehouse name>

(2) git merge <remote warehouse name>/<remote branch name>

 

Example:

git pull origin develop:develop

git pull origin develop

git pull

When you set the local branch and the remote branch to have the same name, and set the association relationship (that is, the tracking relationship), these three commands have the same effect

 

11, rebase merge

git rebase -i <name of branch to merge into>

Applicable scene

 

Use git rebase -i HEAD~<number of times> to enter the commit management mode, and do the following according to the prompts

  1. Merge multiple commits
  2. Modify the submitted commit message 
  3. Delete a commit by commit id

 

Operations that can be performed in the intermediate state of rebase

git add <filename>

git rebase —continue

git rebase --abort

 

 

2. Conflict Resolution

1. Commands that may conflict

(1) git merge <name of branch to be merged in>

(2) git pull <remote warehouse> <remote branch>

(3) git rebase -i <name of branch to be merged in>

 

2. Conflict resolution

After a conflict occurs, use git status to see that the conflicting files are displayed in red font. Open the file, the conflict part will have a specific representation as follows

<<<<<<< HEAD

The code at the current position before the conflict occurred, when no merge was made

=======

merged conflicting code

>>>>>>>

 

The user needs to delete or keep the corresponding code according to the specific business, and remove the marks << HEAD == >> at the same time.

 

Note:

Sometimes >>>>>>>> will be followed by some descriptive information, such as branch name, comments when committing, etc.

 

3. Replace the undo order

! ! Some commands to use with caution

1, delete the branch

git branch -D <name of branch to delete> Force delete, even if not merged into master branch

git branch -d <name of branch to delete> Non-forced deletion, if it has not been merged into the main branch, it cannot be deleted

 

 

2. Replace a file in the working directory, abandon all modifications to it, and update the file to the latest commit version

git checkout <file path>

Note: After the replacement, the file will never be found again, so use it with caution!

 

3, Clear the temporary storage content of the specified file in the temporary storage area

git reset HEAD <file path>

Note: After clearing, the file will never be found again, so use it with caution!

 

4, undo the commit to the specified version

git reset <specified version number>

Note: After revocation, all commits after the specified version will be revoked and rolled back to the current working directory. You can see by executing git status that all modified files are displayed in red font.

 

Note: 3, 4 can use git reflog to view the change log

 

4. Examples of common usage scenarios

 

1. Daily work

(1) Use git branch to view the current branch before work, use git status to check whether there are uncommitted modifications in our working directory, and use git diff to see what we have modified

(2) Use the git pull or git fetch and git merge commands to pull the remote content to the local and merge it into the current branch

(3) Use git add to add our modifications to the staging area, and use git commit to submit this modification (note the use of -m to add submission instructions)

(4) Use git status to see if the submission is complete

(5) Use git pull to pull the modifications of the remote branch

(6) Use git push to push this modification

 

2. Tasks that cannot be solved quickly, or bugs that need to be fixed urgently

(1) Use git branch to view the current branch

(2) Use git checkout to switch to the branch that needs an urgent fix

(3) Use git checkout -b to check out a repair branch (for example, call fix)

(4) Fix bugs on the fix branch, and use git add and git commit to submit changes

(5) Use git checkout to switch to the branch that needs urgent repair, use git pull to pull the remote content, and use git merge fix to merge the changes

(6) Use git status to see if the merge has conflicts

(7) Use git push to push the changes to the remote warehouse

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326205652&siteId=291194637