Learn Git in 20 minutes, allowing you to quickly get started with Git [recommended for novices]

Foreword: Today, when Git is in full swing, I am embarrassed to tell people that I am a programmer without knowing git. Have you been eager to try it a long time ago, just because there is no excuse (opportunity). Well, the opportunity is today. Give me 20 minutes, yes, only 20 minutes, so you can quickly use git.

One, what is git

The premise of this article is that you know or have used at least one source code management tool, such as SVN, CVS or TFS, etc. You must know what source control is. If these are not satisfied. Please skip this article.

Let's take svn as an example. We all know that traditional source code management is server-centric:
Insert picture description here
each developer directly connects to the intermediate server, makes local modifications, and then commits to the svn server.

This approach seems perfect, but it has fatal flaws:

  1. Developers cannot track the code version locally. All information is on the server. You have changed a lot of local code, but you cannot submit it. Generally, only one version can be cached locally. For small projects, it doesn't matter, but when the project is complicated, it will be troublesome if there are more people. Usually your local code is all red. I don't know what was changed today, and what changes should really be submitted to svn.

  2. Because of the first point, once you leave the server, the programmer will not work properly. Because the code version cannot be tracked locally. Your (almost) any operation must be connected to the server. For example, show log, blame, show history, etc.

  3. The central server is under too much pressure. Because everyone must submit the code to the server and do a daily build.

  4. For large projects, svn is almost unavailable. For example, for the Linux kernel project, there are more than tens of thousands of developers? If you submit them directly to the svn server, it won't be messy.

  5. For personal private projects (or for projects of small companies), it is certainly not possible to use version control, but it is a bit reluctant to set up an svn server for version control.

Is there anything that can solve the above-mentioned problems? Well, the answer is yes.

The author of the Linux kernel also encountered these problems, so he decided to change the world again and rewrite a svn that can be used locally.

Yes, this is git. Distributed code version management system. (Speaker: svn without server)

Let's look at the structure of git:
Insert picture description here
git has no central server. After you install the git software, you can specify a local folder for version control. You can unplug the network cable and modify locally, commit, revert (rollback), branch, restore, show log, blame, history, etc., all the operations you can use in svn before. Simply put, you are perfect.

You may realize a problem. The world is in chaos.
Everyone develops on their own, how to collaborate? Well, usually git has two more operations than svn, namely pull and push.

Let's look at a more complicated picture:
Insert picture description here
developers use pull and push operations to pull other people's modifications, or push their own modifications to others.

Well, you may still think there is a problem. In our production environment, whose code is the final version?

This problem is really tricky, because, purely technically speaking, each developer's machine is equal. There is no primary or secondary.

We still have ways:

We can solve the problems that cannot be solved technically. ---- Mencius

We simulate a center from the distributed environment:

Insert picture description here
We introduce the role of Leader. His machine is the final version. Each developer must push the final modified version to the leader. The leader will review and submit. He is the final version.

Well, we seem to have missed a big problem. Speaking of git, why did we miss github? What is github.

We know that if everyone modifies locally, it may be insecure locally (the hard disk is broken, and the laptop is stolen...)

We may need a secure server to store/backup code. For open source projects, you may need a place to share your code. You can’t open your notebook 24 hours a day and let others pull the code from you.

As a result, so-called source code hosting websites appeared on the Internet. github is like this.

Looking at this picture, I only modified one of the points, which is to replace the leader's machine. Online account provided by github.com.
Insert picture description here
Therefore, git and github are not necessarily related.

Here are a few commonly used online hosting addresses, you are interested to see for yourself:

  1. github.com, famous. Free, only supports open source projects, not private projects.

  2. bitbucket.com is also famous. Free, support open source projects and private projects. Up to 5 free users. Unlimited items.

  3. git.oschina.net, the top domestic hosting platform, this is what I am using. Support open source projects and private projects. Unlimited members, 1000 projects. Alibaba Cloud server is used, which is extremely fast. I recommend 5 stars.

Second, the general development process using git

The above has actually involved the general structure of using git. So how is git applied in a production environment.

The model I know is as follows:
Insert picture description here
each developer submits code to his project server (leader), and the leader submits it to the company server. Of course, this leader is optional. If the project is small, the developer can submit it directly to the company server. The company's server is the final version. Most companies will also have continuous integration CI servers. We can set up git hooks on the company's source server. Automatically trigger the CI server to work. This is something later, not much to say.

Third, quickly install new projects

  1. This is the official website of git: http://git-scm.com/ to download the windows client.

    If it is linux, sudo apt-get install gitcore

  2. Note that the clients on the official website are all command lines. The command line is high-level usage. I won't talk about it here. We next gui.

I use TortoiseGit. https://code.google.com/p/tortoisegit/, everyone is probably familiar with the tortoise crawl in the svn era. Get started quickly. Our following operations are all on the gui.

Not to mention the installation process. Next all the way. We skip. Straight to the end. Assuming you have now installed it.

For example, I already have a project, helloworld:

Insert picture description here
This is the internal structure of the project file:
Insert picture description here
now what do we want helloworld to do with git? In the project root directory, click the right mouse button.
Insert picture description here
Choose Create repository.

Do not check this option. We will explain what Bare is later.
Insert picture description here
Then it's done.
Insert picture description here
There is an extra .git directory inside. The current directory (and all its subdirectories) is already under git monitoring. But the current code has not been added to the git tracking database. Simply put, git has not stored any version information. We need to make the first submission:

By default, git will have a master branch locally.
Insert picture description here
We write some comments and tick the files we want to add to git. (If there are subdirectories, they will all be displayed here.) The
Insert picture description here
submission is complete and close. This push button will be discussed later.

Insert picture description here
In order to thank you for your attention and support, this time I prepared a limited time to receive benefits: Ali interview questions, Baidu interview questions, Didi interview questions, Huawei interview questions, JD interview questions, Meituan interview questions, Tencent interview questions, headline interview questions , ZTE interview questions.
Insert picture description here
What are you waiting for? I recommend my linuxC/C++ language exchange group: [ 1106675687 ] I have compiled some learning books and video materials that I think are better to share in the group files, and you can add them if you need them! The top 100 enter the group to receive, an extra copy of C/C++, linux materials worth 199 is included (video tutorials, e-books, actual combat projects and codes), the following part is displayed.
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_50662680/article/details/112678255