Git enterprise development control theory and practice - from entry to in-depth (4)|Git remote operation|Gitee

foreword

Well, the blogger here will first post some columns full of dry goods!

The first is a summary of bloggers’ high-quality blogs. The blogs in this column are all the bloggers’ most thoughtful writing. They are full of dry goods. I hope they will be helpful to everyone.

Then there is a column "Git Enterprise Development Control Theory and Practical Operations" that the blogger spends the most time recently. I hope everyone will pay more attention!


Git remote operation

The blogger uses Gitee to explain, and the operation method is exactly the same as Github.

Understanding Distributed Version Control Systems

Everything we've talked about so far (workspaces, staging areas, repositories, etc.), is local! That is, on your laptop or computer. And our Git is actually a distributed version control system! What does it mean?
It can be simply understood that each of us has a complete version library on our computer, so that when you work, you don’t need to be connected to the Internet, because the version library is on your own computer. Since everyone has a complete version library on their computer, how do multiple people collaborate? For example, you changed file A on your computer, and your colleague also changed file A on his computer. At this time, you The two only need to push their respective modifications to each other, and then they can see each other's modifications.

insert image description here

Distributed version control systems usually also have a computer that acts as a "central server", but the role of this server is only to facilitate "exchange" of everyone's modifications. Without it, everyone can work the same, but it is inconvenient to exchange and modify. With this "central server" computer, you are not afraid of losing files due to local problems.

The central warehouse is called "remote warehouse". There is a very good website called Githubthe central warehouse!

Of course, some readers of my article may have some partners who are slow to visit foreign websites. Here, the blogger will use an alternative domestic website called Code Cloud to demonstrate.

Create a remote repository

Create a new warehouse on Gitee

insert image description here
insert image description here

easy to understand issuesandpull request

insert image description here

The Gitee platform provides us with four manager identities.

Remote repositories are owned by their own members.

This issuesis a place where some people with problems communicate with warehouse members.

pull requestwhat is it then?

First of all, for developers, it is definitely not on masterthe development, we may be devdeveloping on the branch, and then we have to mergeoperate.

Of course, we know that mergeoperations are risky. So we can't let people come here casually merge.

mergeTherefore, the developer who wants to proceed must write a PR (pull request), which can be understood as a "merger application form" for the warehouse manager to read.

After the administrator agrees, we can proceed with mergethe operation.

insert image description here

Clone the remote repository

insert image description here
These are the data transmission protocols provided by Gitee, and the commonly used ones are https and ssh.

HTTPS protocol clone

insert image description here
Use the URL given here, copy it, and use git clonethe command.

git clone https://gitee.com/Yufch/remote-gitcode.git

insert image description here

Note: This command cannot be executed under any local repository.

insert image description here

This is what is in the remote warehouse. Usage is exactly the same as before.

git remote -v # 查看远程仓库的一些信息
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git remote -v
origin  https://gitee.com/Yufch/remote-gitcode.git (fetch)
origin  https://gitee.com/Yufch/remote-gitcode.git (push)
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ 

Indicates that we have remote warehouse fetchpermissions (pull permissions) and pushpermissions (push permissions).

SSH protocol clone

The ssh protocol is a mechanism that uses a public key encryption and public key login. So we have to put a public key of our local server on the git server for management.
insert image description here
insert image description here

We have not configured the public key yet.

**Step 1: Create an SSH Key. **In the user's home directory, check to see if there is .ssha directory. If so, check to see if there are id_rsa and id_rsa.pubthese two files in this directory. If there are already, you can skip to the next step. If not, you need to create an SSH key.

ls -al ~ | grep .ssh # 可以搜索一下~下有没有.ssh文件夹,如果没有就要创建SSH Key

This command is the knowledge of the system, so I won’t explain it too much here.

To set the ssh public key, you need to configure the mailbox, and then the mailbox must be consistent with the one on the code cloud. You can watch it here.

insert image description here

Then configure the SSH public key command as follows.

ssh-keygen -t rsa -C "xxx.com"
(base) [yufc@ALiCentos7:~]$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yufc/.ssh/id_rsa): 
Created directory '/home/yufc/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/yufc/.ssh/id_rsa.
Your public key has been saved in /home/yufc/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:1PWuf6UgtkssYGh2x5QasdfaSrD4YmS7EL0Yf7VamusiQ [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|   .+..     .    |
| . + . o + . .   |
|..o o o * .   .  |
| .o+ o *     .   |
|oo+ B * S     .  |
|o+.= B o .o ..  .|
|E.. . o ..oo.. ..|
| o.  .   o.  .. .|
|  .o.     ..  .. |
+----[SHA256]-----+
(base) [yufc@ALiCentos7:~]$ 

All the following things can be entered by default.

(base) [yufc@ALiCentos7:~/.ssh]$ cat id_rsa.pub
ssh-rsa AAAAB3NzaasdasdfadSMIpxADoRltTaMiJadsfasdflBld/qF7EHq+KwJY4CMhb65A6tsvGPxAPAeYNgufjc4LC5r0v2hdgs0Pk86XIX7vUK6DUrpe6LPTBOdAGTsWmSbcynpJhX97mdM0P2p/3HK0KYmlymyAIzPpEyNF5YRF9goDzI6OBw7Y9WZlp0QWPyWRbnSMOe4olXKY7EFcon5uCrlD3vJNac/3ZIZpKy2TrtvPvcm1fk9sJW5JlcC7P6DByL0CG8ohxGwz2/4l [email protected]
(base) [yufc@ALiCentos7:~/.ssh]$

This thing is your public key. Just get it on the code cloud.

At this point you can clone.

git clone [email protected]:Yufch/remote-gitcode.git

insert image description here

Push to remote warehouse

ready to operate

The local warehouse needs to perform an pushoperation to push the content of a local branch to a branch in the remote warehouse.

Then don't forget, remote-gitcodeafter entering the warehouse, you need to configure the name and email. This should be consistent with the code cloud.

(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config -l
sendpack.sideban=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=[email protected]:Yufch/remote-gitcode.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config user.name "Yufch"
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config user.email "[email protected]"
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git -l # 这里打错了 ...
Unknown option: -l
usage: git [--version] [--help] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config -l
sendpack.sideban=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=[email protected]:Yufch/remote-gitcode.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
user.name=Yufch
user.email=[email protected]
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ # 这样就配置好了

So now let's create a file. Then add him to the repository first (the content of the previous chapter)

(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ ls
README.en.md  README.md
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ vim file.txt
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ cat file.txt 
hello gitee
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git add .
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git commit -m "create file.txt"
[master 7352d02] create file.txt
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$

git pushOrder

git push origin master:master
# origin: 远程仓库的名字:一般都叫origin, 使用 `git remote` 命令可以查看
# 第一个master: 要推送的本地分支
# 第二个master: 要推送到的远程仓库的分支
# 如果冒号左右两边一样,可以省略一个
git push origin master

insert image description here
At this point, we can see the modified content in the remote warehouse.

Pull the remote warehouse

If the current situation is that the code in the remote warehouse is ahead of the local warehouse (maybe someone else developed it and then pushed it up), and we want to see the content in the remote warehouse locally, we have to pull it.

insert image description here

Suppose someone adds a row hello worldand then pushes it up, and we also want to see this row locally.

Note: At work, do not directly modify the code on the cloud or github. If you want to modify it, you have to pull it locally and then go to PR after modification.

git pull origin master:master
# origin: 远程仓库的名字:一般都叫origin, 使用 `git remote` 命令可以查看
# 第一个master: 要拉取的远程仓库的分支
# 第二个master: 要拉取到的本地仓库的分支
# 如果冒号左右两边一样,可以省略一个
git pull origin master
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git pull origin master:master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From gitee.com:Yufch/remote-gitcode
   7352d02..8513266  master     -> master
Warning: fetch updated the current branch head.
Warning: fast-forwarding your working tree from
Warning: commit 7352d02962528eae4ef12d0669a40f4f7cdf35ec.
Already up-to-date.
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ 

Note: After pulling down the branch, a merge operation will be done, sopull = 拉取 + merge

ignore special files

In daily development, we do not want or should not submit some files to the remote end, such as the configuration file that saves the database password, so how do we let Git know? Create a special file in the root directory of the Git workspace, and then put .gitignorethe Fill in the ignored file names, and Git will automatically ignore these files. There is no need to write .gitignorethe file from scratch, gitee can generate it for us when creating the warehouse, but we need to actively check it.

insert image description here

Of course, we didn’t choose it when we created it just now, so we can just create one ourselves.

touch .gitignore

insert image description here

The like means to let git ignore .sothe ending and .iniending files.

As shown below: Logically speaking, after we create the .gitignore,, file, we will recognize that three files have been created in the workspace, but as shown below, we key.soonly see that they have been created. That's because we set ignore.token.inigit statusgit status.gitignore

(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ touch key.so
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ touch token.ini
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
nothing added to commit but untracked files present (use "git add" to track)
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]

If at this time git add ., the two files that were set to ignore just now will not be submitted to the temporary storage area.

But under this condition, I still want to submit one, key2.sowhat should I do? The following commands can be used.

git add -f key2.so # 强制add

But we generally don't want to break .gitignorethe rules of the file, so git also provides us with a syntax.

insert image description here

That way you can rule out ignoring.

Then if one day, your .gitignorefile is written very long, and then you create one key3.so, and you forget why it was ignored, you can use a command to check why it was ignored.

git check-ignore -v key.so # 查看一个文件为什么被忽略
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git check-ignore -v key.so
.gitignore:4:*.so       key.so
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ 

Then we can see that it is the fourth line that makes it ignored.

Finally, we push it, and the remote end can see these files.

Configure command aliases

If I think I need to use git statusa command a lot, but it's too hard to type, think of an alias: git stok? OK. But we have to do some configuration.

git config --global alias.st status

**The meaning of this command is to give statusan alias st. ** --globalYou can choose whether you want it or not.

git stIt works now .

(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git st
# On branch master
nothing to commit, working directory clean
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$

Make another one.

We have learned this before.

git log --pretty=oneline --abbrev-commit

Create an alias.

git config --global alias.lpa 'log --pretty=oneline --abbrev-commit'
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git lpa
7393ea0 add .gitignore
8513266 update file.txt.
7352d02 create file.txt
a9af182 Initial commit
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$

It is not recommended to configure it when you are a beginner. It is recommended to simplify it when you are working after you are familiar with it.

Guess you like

Origin blog.csdn.net/Yu_Cblog/article/details/132527466