This article has participated in the call for good writing activities, click to view: back end, big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!


How do YOU use GIT to develop projects at work? (Even better at the end)

Note: This article will be twice as fast if you are familiar with Linux

Also: If you are familiar with SVN, Git is just a more advanced version management tool

But: If you’re not familiar with Linux or SVN, at least you’ve used a cloud disk to manage your daily files.

If you haven’t even used the cloud tray. Then you’d better get back to Mars. Earth is dangerous.


Why do YOU need version management?

Assumptions:

The year-end presentation is next week and you plan to finish your powerpoint presentation today.

It took me a whole morning to finish half of it.

When you have lunch, you’re still wondering how to finish the second half.

Then you suddenly realize that there is something wrong with the morning plan and it needs to be changed again. 5:20pm…..

After you’ve read your plan over and over again, you still think that the morning plan is better

But this time the trouble came, the morning plan had been changed beyond recognition

The words you wrote, the pictures and animations you used, have all been deleted

Want to use Ctrl + Z? Unfortunately, powerpoint only supports a maximum of 150 rollback operations, and the default setting seems to be only 80

At this time, is not particularly want to a bottle of regret medicine?

My friend, it’s time to get to know X degree cloud disk members. Don’t be 998! Don’t be 998! For 96 yuan, you can enjoy the annual package service

Okay, that was a joke, but the X-degree cloud disk does have the ability to record file versions

Therefore, we believe that a good cloud disk software, should have the following functions:

  1. Users A and B can easily share files with each other
  2. Users A and B can modify the same file, and conflicts can be resolved in A timely manner
  3. User A’s modification of the file will be synchronized to user B
  4. Every submission of a file by all users is recorded, and in the event of a misoperation, the previous version can be restored

Isn’t that why version management got its name?


The advantage of SVN

Before using Git, you must learn about SVN

Speaking of which, SVN was once the most popular version management tool in the world

The use of SVN software is really very simple

It comes with two installation packages

The second installation package is the server side, that is, you can easily set up a SVN server in the company by installing the server side software

So people can share files or code on the company Intranet

Programmers often need to collaborate when writing code (multiple people maintaining a project)

The convenience brought by SVN is self-evident. Almost all mainstream development tools are equipped with SVN plug-ins

The specific usage of SVN is not described in detail here. Readers are invited to baidu by themselves


With SVN, why Git?

The author of git software is Linus(creator of Linux kernel, legendary programmer in programming world).

I have to say that Git is not an order of magnitude worse than SVN in terms of ease of use

But it’s for programmers, so who cares about user experience?

Because of his strong personal interests, Linus strongly dislikes centralized file management

Thus git software has been distributed since its inception

Simply put: anyone can submit code to anyone’s computer, anyone can download code from anyone’s computer, and anyone can be considered a server

Coupled with Linus’s command-line mentality, Git has never had a graphical interface from the start

So when you use Git, you feel like a hacker, right?

Although Git pioneered the branching design, its poor user experience failed to make it popular worldwide until github came along…..


What is the relationship between Git and Github?

Github has been dubbed the world’s largest gay dating site

How did it rise?

Github offers the following services:

  1. You can go to this website and register an account
  2. With this account, you can set up your own warehouse
  3. Use your local Git software to submit your code to github
  4. If your colleague has a Github account, you can share it with them with one click
  5. Github projects are publicly shared with others
  6. We do not provide friends, only code sharing and management

As you can see, Github brings us two huge benefits

First, Github makes us seem to return to the SVN server era, convenient.

Second, all of the world’s well-known open source code is submitted to Github and made public, so that anyone can submit their own modification suggestions to the open source project

It led to the popularity of Git software, which is why you have to learn Git today


How does Git work?

First, next git software https://git-scm.com/downloads and install it

On Github, create an empty project

After you have built the project, you will see the address of the project

Now, copy the project’s download address

Find a suitable directory, such as drive E, right-click it and select Git bash here

Type the command git clone https://github.com/ay8yt/teach.git

The project will download automatically, and drive E will have an extra teach directory

Now you can open your development tools, go to this directory, and write your code

The project is finished. How do I submit it?

At the command line, use CD teach to access the teach directory

Enter git add -a to add all files to the cache and prepare to commit

Then, git commit -m ‘Here is some description, what changes have been made this time’

Submission successful!


It may take some preparation

If you are using Git for the first time, you will be prompted to set user information first.

Git config --global user.email

Git config --global user.name git config --global user.name

Also, since projects are often multi-person, you have to set up a branch of your own

I know you're gonna want to askWhat is a branch? . It’s a long story

Suppose Sam and Sam are working on a project together

Although, they are responsible for different modules

However, if there are bugs or errors in Sam’s module, it will lead to the failure of Sam’s project

To avoid conflict and mutual interference

Zhang SAN and Li Si made a clone copy of the project before developing it

Next, they make changes and commit on their own clones

Next, Joe can make changes and commit on his copy without any conflict with Joe

So that’s the idea of branching

Of course, All of Zhang SAN’s operations are carried out locally on his own computer

If something goes wrong with the local computer, all versions of the local record are still lost

So, Joe can push all his local versions to the remote Github server

Similarly, Li Si can also push all his local versions to the remote Github server

When we look at the Github remote, it looks like this:

This way, not only did we keep a record of everyone’s versions, but there was no overlap or conflict between John and Tom throughout the development process

How’s that? Does this branch of invention feel particularly handsome?

Although SVN quickly rolled out branching functionality, it didn’t have a server as popular and unified as Github.

Github is not only hosting code, it has evolved into the world’s largest code-sharing community

In June 2018, it was sold to Microsoft for $7.5 billion

The only song left for SVN is “Cool cool”.


You know the basics, or do you need to learn how to use them

First me: Hey, how do I use GIT to develop projects at work?

Second me: You can’t say hello, how are you? Or, excuse me, something like ~?

The first me: I found you a lot of things!

Second me: could you please make your question more specific?

# 1 me: How can I get new gibhub projects down?

The second me: please don’t call me god, I’m just a lot better than you, although I also have a moment to feel that he is indeed a god

# 1 ME: What were the moments that made you misunderstand yourself?

The second I: drunk moment, wake up! Well, to answer your question,

If you still have that question, obviously you haven’t been paying attention, because it’s already been covered.

Forget it. Again, it won’t happen again. git clone https://github.com/ay8yt/teach.git

First me: How do I clone a copy locally, i.e. create a branch?

Second me: Use the branch command. git branch yintao01

First me: How do I get into this branch?

Git Checkout yintao01

Well, you should now be able to clearly see that you have switched to your own branch, and all subsequent operations will be performed on this copy.

First me: I have written part of the function, want to save a version, how to do?

Second me: step 1, git add-a, commit all files to cache

First me: Wait! I have some files I don’t want to submit, such as the node_modules folder. How do I ignore it?

Second me: You will create a.gitignore file in the root directory of your project, which will look like this:

First me: but I have just committed the files that I don’t need to the cache, how can I undo?

Second me: very simple, just reset, git reset, and you can commit.

First me: Wait! I want to check before I commit, which files have been committed to the cache?

The second me: you are so busy! Well, you can use git status to see what changes have been made and the status of all the files.

First me: Ok, is the official commit command?

Second me: Yes! Git commit -m

First me: Do I have to write every time I submit a version? How much trouble is that? Would you mind not making it clear?

Second me: you can try, make you regret.

First me: just after submitting, I regret it. I feel that the submission was wrong. I want to cancel or overwrite the submission.

Me # 2: The great thing about version management is that it allows you to regret that you fixed the wrong stuff first

Add the cache again, but this time with git commit — amend-m ‘the commit overwrites’

First me: I want to go straight back to the previous version, how do I do that?

Git reset head^

First me: I want to restore the first two versions directly. How do I do that?

Git reset head^^

First me: What about going back to the first N versions?

Git reset head^^^^^^^^^^…

First me: Are you kidding me? So much trouble?

Second me: No kidding, that’s it, of course you can specify the version number to restore directly

git reset --hard 7e2ec0f51e9ae2e7cbc7c4deca18b77b242148d6

The first me: is the version number a hash? How can I remember that long?

Second me: you can view all versions with git log

The first me: I checked, but each version has changed what can not see ah, this aaAAA, BBBB, CCCC, DDDD are what ghost?

The second me: Aren’t these the notes you wrote when you submitted them? This is what happens when you don’t write notes carefully. Deserve it.

In addition, whenever you write code, you must think carefully and submit it after passing the test. This is the basic quality of a programmer.

While the rollback operation is useful, it shouldn’t be your daily command. If a programmer is very familiar with various rollback operations

It’s usually a sign of poor code.

The first me: I see you use quite familiar with it

The second me: go ~~~~~~

First me: If I have tested and submitted locally, how can I push it to remote Github?

Second me: In order to facilitate remote push, we usually make a shortcut to the remote warehouse address.

git remote add miaodong https://git.oschina.net/ay8yt/test.git

This miaodong is the shortcut name, and then when you submit it, you can write:

Git push-u Miaodong Yintao01 this means push the yintao01 branch to miaodong this warehouse address

-u means yintao01 as the default branch

Git push miaodong

First me: My module has been developed and passed the test, and has been pushed to Github. How can I merge my branch back to the main branch

Second me: Merging branches on Github is easy. Find the Pull Request and read the instructions for yourself

First me: That’s it? I will be!

Second me: you don’t run!!

So far, I have succeeded in offending me. You’ve successfully mastered the basics of using Git in your daily work