1. What is Git?
Git is currently the world's most advanced distributed version control system (no one).
1.1CVS and SVN are both centralized version control systems, while Git is a distributed version control system. What is the difference between centralized and distributed version control systems?
Let’s talk about the centralized version control system first. The version library is centrally stored on the central server, and when you work, you use your own computer, so you must first get the latest version from the central server, and then start working. Live, and then push your live to the central server. The central server is like a library. If you want to modify a book, you must first borrow it from the library, then go home and modify it yourself. After you have finished modifying it, put it back in the library.
Distributed version control systems do not have a "central server" at all. Everyone has a complete version repository on their computer. This way, when you work, you don't need to be connected to the Internet, because the repository is on your own computer. Since everyone has a complete repository on their computer, how can multiple people collaborate? For example, you have changed file A on your own computer, and your colleague has also changed file A on his computer. At this time, the two of you only need to push your changes to each other, and you can see each other's files. edited.
Compared with the centralized version control system, the security of the distributed version control system is much higher, because everyone has a complete version library in the computer. That's it. And if the central server of a centralized version control system fails, everyone can't work.
When actually using a distributed version control system, it is actually rare to push revisions of the version library between the two computers, because maybe you two are not in the same local area network, and the two computers cannot access each other, and maybe your colleagues today Sick, his computer didn't turn on at all. Therefore, a distributed version control system usually also has a computer that acts as a "central server", but the role of this server is only to facilitate the "exchange" of everyone's modifications. .
Of course, the advantage of Git is not only that it does not have to be connected to the Internet. Later, we will see Git's extremely powerful branch management, leaving SVN and so on far behind.
2. Install git
Install Git on Windows
To use Git on Windows, you can download the installer directly from the Git official website (for students with slow internet speeds, please move to the domestic mirror ), and then install it according to the default options.
After the installation is complete, find "Git" -> "Git Bash" in the start menu, and a similar command line window pops up, indicating that the Git installation is successful!
After the installation is complete, you need to set the last step, enter at the command line:
$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址。
注意
git config
命令的--global
参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。
3. Create a repository
3.1 Create a directory.
$ mkdir learngit
$ cd learngit
$ pwd
/Users/michael/learngit
3.2 Turn the directory into a git-controlled repository through git init.
3.3 Add files to the Git repository, in two steps:
-
The first step, use the command
git add <file>
, note that it can be used repeatedly to add multiple files; -
The second step, using the command
git commit
, is done.
git commit
Command, -m
followed by the description of this submission, you can enter any content, of course, it is best to be meaningful, so that you can easily find the change records from the history.
4. Time machine shuttle.
4.1 git status
Commands allow us to keep track of the current state of the warehouse
4.2 git diff [files]
As the name implies, it is to view the difference, you can view the specific content of the modification
4.3 To be continued
5. Remote warehouse
5.1 Associate a remote library and use the command git remote add origin git@server-name:path/repo-name.git
;
5.2 After the association, use the command git push -u origin master
to push all the contents of the master branch for the first time;
5.3 After that, after each local commit, as long as it is necessary, you can use the command to git push origin master
push the latest modification;
SSH warning
When you connect to GitHub for the first time using Git clone
or the push
command, you will get a warning:
The authenticity of host 'github.com (xx.xx.xx.xx)' can't be established.
RSA key fingerprint is xx.xx.xx.xx.xx.
Are you sure you want to continue connecting (yes/no)?
This is because Git uses the SSH connection, and the SSH connection requires you to confirm whether the fingerprint information of the GitHub Key really comes from the GitHub server when verifying the key of the GitHub server for the first time, and then press yes
Enter.
Git will output a warning telling you that the GitHub key has been added to a local trust list:
Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
This warning will only appear once, and subsequent operations will not have any warnings.
If you are really worried about someone pretending to be the GitHub server, yes
you can check whether the fingerprint information of GitHub's RSA Key is the same as that given by the SSH connection before entering it.
5.4 To clone a repository, you must first know the address of the repository, and then use the git clone
command to clone.
5.5Git supports a variety of protocols including https
, but is the fastest via ssh
the native protocols supported .git
6. Branch management
6.1 First, we create dev
the branch, then switch to the dev
branch:
$ git checkout -b dev
Switched to a new branch 'dev'
git checkout
The command plus -b
parameters means create and switch, which is equivalent to the following two commands:
$ git branch dev
$ git checkout dev
Switched to branch 'dev'
6.2 Then, use thegit branch
command to view the current branch:
$ git branch
* dev
master
git branch
The command will list all branches, the current branch will be marked with a *
number.
6.3 dev
The work of the branch is complete, we can switch back to the master
branch:
$ git checkout master
Switched to branch 'master'
6.4 master
After switching back to the branch, the content you just added is gone! Because that commit is on the dev
branch, and master
the commit point of the branch has not changed at the moment.
Now, let dev
's merge the branch's work into the master
branch:
$ git merge dev
Updating d17efd8..fec145a
Fast-forward
readme.txt | 1 +
1 file changed, 1 insertion(+)
git merge
The command is used to merge the specified branch into the current branch. After merging, you can see that dev
it is exactly the same as the latest commit of the branch.
Noting the above Fast-forward
information, Git tells us that this merge is in "fast-forward mode", that is , the current commit master
pointed to directly dev
, so the merge speed is very fast.
Of course, it is not possible to merge every time Fast-forward
. We will talk about other ways of merging later.
6.5
Once the merge is complete, you can safely delete dev
the branch:
$ git branch -d dev
Deleted branch dev (was fec145a).
After deleting, check branch
, only the master
branch is left:
$ git branch
* master
6.6 There is a conflict in merging into the master branch.
git will tell us what files are in conflict, and git status will also tell us.
Manually modify the conflict file, and then submit it, it can be resolved.