Git basics: Branch management and remote repository

Branch management

Liao Xuefeng:

A branch is a parallel universe in a science fiction movie, where you are trying to learn Git while you are trying to learn SVN in another parallel universe.

If the two universes didn’t interfere with each other, it wouldn’t matter to you right now. At some point, though, the two parallel universes merged, and as a result, you learned both Git and SVN!

Branch management allows multiple people to collaborate on code development without frequent code synchronization. Everyone can develop their own functions in their own branches and submit them to their own branches at any time. When the function development is completed, it can be merged into the main branch once again. In this way, it is safe, efficient and does not affect other people’s work, which reflects the advantages of distributed version management system.

The preparatory work

To easily demonstrate the effects of branch management, we created a repository and committed it three times.

  1. Create a filefile.txtAnd make the first commit
  2. Enter information in a filefirst lineAnd make a second submission
  3. Appends information to the filesecond lineAnd submit it a third time

To view submission information:

E:\repository>git log --oneline
4bdd05b (HEAD -> master) write the second line
76e96c6 write the first line
3dbe8cf create new file
Copy the code

The submission record is displayed graphically, as shown below:

Create a branch

Create a branch with git switch -c or git checkout -b.

E:\repository>git switch -c dev
Switched to a new branch 'dev'
# or
E:\repository>git checkout -b dev
Switched to a new branch 'dev'
Copy the code

The preceding command is equivalent to the following two commands:

E:\repository>git branch dev
E:\repository>git switch dev
Switched to branch 'dev'
Copy the code

Then use the git branch command to view all branches with an asterisk before the current branch:

E:\repository>git branch
* dev
  master
Copy the code

As shown in the figure:

At this point, we have successfully created and switched to a new branch.

Add new content from dev to file.txt file and commit new:

E:\repository>git add .
E:\repository>git commit -m "dev commit"
[dev 4bc2e50] dev commit
 1 file changed, 2 insertions(+), 1 deletion(-)
Copy the code

The new branch looks like this:

At this point, the contents of file. TXT are:

first line
second line
content from dev
Copy the code

The first two lines are the commits from the Master branch, and the third line is the commits generated on the new branch dev.

Switch branch

You can switch branches using the git switch command

E:\repository>git switch master
Switched to branch 'master'
Copy the code

View file contents:

first line
second line
Copy the code

Switch back to the dev branch:

E:\repository>git switch dev
Switched to branch 'dev'
Copy the code

View the file contents again:

first line
second line
content from dev
Copy the code

At this point, the powerful function of branch management has been reflected, different branches have different content, many people can feel at ease in their own branch to enjoy the development.

Merging branches

Let’s switch to master:

E:\repository>git switch master
Switched to branch '`master`'E:\repository>git merge dev Updating 4bdd05b.. 4bc2e50 Fast-forward file.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)Copy the code

The git merge command merges the specified branch into the current branch.

Delete the branch

Since we have merged the contents of the dev branch into the master, we can remove the dev branch. The git branch -d command is used to delete a branch:

E:\repository>git branch -d dev
Deleted branch dev (was 4bc2e50).
Copy the code

Looking at the branches again, the only branch left is the master branch:

E:\repository>git branch
* master
Copy the code

summary

Git encourages extensive use of branches:

Check out the branch: Git Branch

Git branch

Git checkout

Git checkout -b

Git merge

Git branch -d

Remote warehouse

Common remote warehouse server comparisons:

  • GitHub: A common problem for Chinese users is slow access and sometimes failure to connect.
  • GitLab: Similar to GitHub, generally used to build private servers in the enterprise, to build their own environment.
  • Gitee: code cloud, domestic access friendly, do not need to build their own environment, can establish their own private warehouse.

Therefore, we chose Gitee as the remote repository for the demonstration.

Generate an SSH public-private key pair

To generate a public-private key pair, run the ssh-keygen -t rsa command. If no special requirements are required, press Enter. The generated public-private key pair is in the. SSH folder of the user directory. The private key file is id_rsa, and the public key file is id_rsa.pub.

Uploading an SSH Public Key

Whether you want to use GitHub or Gitee, you need to upload your SSH public key after registering and logging in. Select “Settings”, then select “SSH”, fill in the title for easy to remember, and paste the contents of the id_Rsa. pub file:

Finally, click OK to see the Key you just added.

Associated with the warehouse

If we now have a local repository called Gaffe, we need to associate it with a remote repository in Gitee.

First, we need to create a remote warehouse, click “+”-> menu “New warehouse” in the upper right corner of the page, fill in the relevant information in the jump page (the project name should be the same as the local warehouse name), and finally click Create:

Git remote add = git remote add = git remote add

git remote add origin https://gitee.com/<username>/gaffe.git
Copy the code

Git remote add git remote add

E:\gaffe>git remote add origin https://gitee.com/<username>/gaffe.git
fatal: remote origin already exists.
Copy the code

Git remote -v git origin origin origin origin

E:\gaffe>git remote -v
origin  https://github.com:<username>/gaffe.git (fetch)
origin  https://github.com:<username>/gaffe.git (push)
Copy the code

As you can see, the local library has been associated with Origin’s remote library and is pointing to Github

We can delete the remote warehouse address that was already bound and rebind it:

E:\gaffe>git remote rm origin

E:\gaffe>git remote -v

E:\gaffe>git remote add origin https://gitee.com/<username>/gaffe.git

E:\gaffe>git remote -v
origin  https://gitee.com/<username>/gaffe.git (fetch)
origin  https://gitee.com/<username>/gaffe.git (push)
Copy the code

You can now see that Origin has been successfully associated with Gitee’s remote repository.

Can ** associate multiple remote repositories at the same time? ** The answer is yes!

Git remote add > git remote add > git remote add > git remote add > git remote add > git remote add > git remote add > git remote add

E:\gaffe>git remote add other https://gitee.com/<username>/other-gaffes.git

E:\gaffe>git remote -v
origin  https://gitee.com/<username>/gaffe.git (fetch)
origin  https://gitee.com/<username>/gaffe.git (push)
other   https://gitee.com/<username>/other-gaffes.git (fetch)
other   https://gitee.com/<username>/other-gaffes.git (push)
Copy the code

After successful association, you can view all the association information through git remote -v. This way, our local library can synchronize with multiple remote libraries at the same time.

Code push

A branch version of a local repository can be pushed to a remote repository and merged with git push:

Git push < remote hostname > < local branch name >Copy the code

If the local branch name is the same as the remote branch name, the colon can be omitted:

Git push < remote host name >Copy the code

If the local version is different from the remote version, but you want to force a push, you can use the –force parameter:

git push --force origin master
Copy the code

To delete a branch from a host, you can use the –delete parameter to delete the master branch of origin:

git push origin --delete master
Copy the code

Code to pull

You can fetch everything from a remote repository using the git fetch command.

Git fetch < remote hostname >Copy the code

To get updates for only one branch, you can specify the branch name

Git fetch < remote host name >Copy the code

Git fetch merge git fetch merge git fetch merge git fetch merge git fetch merge git fetch merge git fetch merge git fetch merge git fetch merge git fetch merge

Git pull < remote host name > < remote branch name >Copy the code

If the remote branch is merged with the current branch, the part after the colon can be omitted:

git pull origin next
Copy the code