web123456

git--local branch and remote branch

  • Create an empty folder locally, and then git clone the remote address, which can pull all the projects of the remote master to the local area
  • git branch is used to view branch information, git branch -a to view all branches (including remote remote)
$ git branch
  * master

$ git branch -a
  * master
  remotes/origin/HEAD -> origin/master
  remotes/origin/Isaac
  remotes/origin/align
  remotes/origin/color_transfer
  remotes/origin/dev_Br20190103
  remotes/origin/dev_yx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

* indicates the current branch, remotes indicates the remote branch
In addition, you can also passgitstatus View all current branches

$ git status
On branch size_expand
nothing to commit, working tree clean
  • 1
  • 2
  • 3

1. Create a local branch in git

$ git checkout -b local branch name
  • 1

It means creating a local branch and switching to a new branch, which is equivalent to the following two lines:

1. Create a local branch: git branch local branch name
 2. Switch to the newly created branch: git checkout local branch name
  • 1
  • 2

Use git branch to see that you have switched to the newly created local branch:

$ git branch
  master
* size_expand
  • 1
  • 2
  • 3

If you find that the content after git branch is empty, you need to add the file content that needs git to the repository.

$git add *
  • 1

2. Associate the local branch to the remote branch

$ git push origin Local branch name: remote branch name
  • 1

Note: Pull can only be pulled after push
If you use $git pushoriginThe local branch name is the same as the remote branch by default
At this time, the remote branch has been established.

3. Delete branches

Delete the local branch:
 $git branch -d local branch name

 Delete the new remote branch `Insert code chip here`
 $ git branch -r -d origin/remote branch name
 or
 $ git push origin:Remote branch name

 The second type of push is empty, which is equivalent to deleting the branch
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4. Merge branches

checkout Switch to master and merge the required branches. Note: During the switching process, uncommitted modifications exist in the suspended area or working directory, which may cause conflicts, so try to keep the work area clean.

$ git checkout master
 $ git merge branch name
  • 1
  • 2

Pull remote branches to local

1. Create a local repository

$git init
  • 1

2. Get associated with remote warehouses

$ git remote add origin
  • 1

3. Pull the remote branch

$ git fetch origin remote branch name
  • 1

4. Create a branch dev locally and switch to that branch

$git checkout -b Local branch name origin/remote branch name
  • 1

Create a new empty branch locally and update it to remote

1. Put your own code directory into an empty folder and execute it in the same level directory

$ git init
  • 1

2. Create a local branch

$git checkout -b branch name
  • 1

3. Add the code to the temporary storage area

$ git add *
  • 1

4. Add cache content to local repository

$ git commit -m 'comment'
  • 1

5. Get associated with remote warehouses

$ git remote add origin
  • 1

6. Push the local version library to the remote server

$ git push origin Local branch: Remote branch
  • 1

Update locally updated content to remote

1. First determine whether the local master branch is the latest content of the remote master, that is, first pull the remote master to the local master

$ git pull-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  • 1

2. Switch to the local branch, merge the content of the master to the local branch, and modify the content in the local branch (the content will be automatically synchronized to the local master). Add all code modified in the current directory from the workspace to the temporary storage area. Represents the current directory

$ git checkout local branch name
 $ git merge master ---------Update the content of the updated master to the local branch
 $ git add .
  • 1
  • 2
  • 3

2. Add cache content to local repository

$git commit -m "Comment"
  • 1

3. Push the local version library to the remote server

$ git push origin Local branch: Remote branch
  • 1

git conflict

When using git pull to update the code, I encountered the following problem:

error: Your local changes to the following files would be overwritten by merge:
***/***/***.py
Please, commit your changes or stash them before you can merge.
Aborting
  • 1
  • 2
  • 3
  • 4

The reason for this problem is that others have modified **.py and submitted it to the version library, and you have also modified it locally. At this time, you can perform git pull operation and conflict. The solution is also very clear in the above prompt.

1. Retain local modification method
1) Direct commit local modification ----This method is not used generally
2) Through git stash --- This method is usually used

$ git stash
$ git pull
$ git stash pop
  • 1
  • 2
  • 3

Restore the workspace to the last submitted content through git stash, and back up local changes.
After that, you can pull git normally
After git pull is completed, execute git stash pop to apply the previous local modifications to the current workspace.

git stash: Back up the content of the current workspace and read the relevant content from the most recent submission, so that the workspace ensures that it is consistent with the content of the last submission. At the same time, save the current workspace content to the Git stack.

git stash pop: Read the last saved content from the Git stack and restore the relevant content of the workspace. Since there may be multiple Stash content, it is managed by stack, and pop will read the content from the most recent stash and restore it.

git stash list:Show all backups in the Git stack, you can use this list to decide which place to restore it.

git stash clear: Clear the Git stack. At this time, using graphical tools such as gitg, you will find that all the nodes of stash have disappeared.

2. Abandon local modification method----This method will discard locally modified code and cannot be retrieved

$ git reset --hard
$ git pull
  • 1
  • 2

Reference article:
/LZbethelight/article/details/84072236
/wenlj/p/