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.
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 Github
the 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
easy to understand issues
andpull request
The Gitee platform provides us with four manager identities.
Remote repositories are owned by their own members.
This issues
is a place where some people with problems communicate with warehouse members.
pull request
what is it then?
First of all, for developers, it is definitely not on master
the development, we may be dev
developing on the branch, and then we have to merge
operate.
Of course, we know that merge
operations are risky. So we can't let people come here casually merge
.
merge
Therefore, 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 merge
the operation.
Clone the remote repository
These are the data transmission protocols provided by Gitee, and the commonly used ones are https and ssh.
HTTPS protocol clone
Use the URL given here, copy it, and use git clone
the command.
git clone https://gitee.com/Yufch/remote-gitcode.git
Note: This command cannot be executed under any local repository.
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 fetch
permissions (pull permissions) and push
permissions (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.
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 .ssh
a directory. If so, check to see if there are id_rsa
and id_rsa.pub
these 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.
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
Push to remote warehouse
ready to operate
The local warehouse needs to perform an push
operation to push the content of a local branch to a branch in the remote warehouse.
Then don't forget, remote-gitcode
after 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 push
Order
git push origin master:master
# origin: 远程仓库的名字:一般都叫origin, 使用 `git remote` 命令可以查看
# 第一个master: 要推送的本地分支
# 第二个master: 要推送到的远程仓库的分支
# 如果冒号左右两边一样,可以省略一个
git push origin master
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.
Suppose someone adds a row hello world
and 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 .gitignore
the Fill in the ignored file names, and Git will automatically ignore these files. There is no need to write .gitignore
the file from scratch, gitee can generate it for us when creating the warehouse, but we need to actively check it.
Of course, we didn’t choose it when we created it just now, so we can just create one ourselves.
touch .gitignore
The like means to let git ignore .so
the ending and .ini
ending 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.so
only see that they have been created. That's because we set ignore.token.ini
git status
git 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.so
what should I do? The following commands can be used.
git add -f key2.so # 强制add
But we generally don't want to break .gitignore
the rules of the file, so git also provides us with a syntax.
That way you can rule out ignoring.
Then if one day, your .gitignore
file 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 status
a command a lot, but it's too hard to type, think of an alias: git st
ok? OK. But we have to do some configuration.
git config --global alias.st status
**The meaning of this command is to give status
an alias st
. ** --global
You can choose whether you want it or not.
git st
It 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.