当前位置:网站首页>Get started, GIT

Get started, GIT

2020-11-07 20:19:27 KEE

During the summer vacation, I used to do some small projects with my friends Git, Share your harvest .

zero 、Git What's the usage?

1. A small example

If you play a little game , Now name it “ game v0.1”, After a few weeks, I didn't feel very satisfied with the role skill setting , Renamed as " game v0.2".

You invited your partner to play the game , He made some suggestions , You made a change , So there's a game "v0.3".

After a lot of time, your folder is full of " game v0.1"~“ game v999.9", It's very inconvenient to manage , With the number of versions , And you can't remember clearly what you changed with each version . So there was Git This tool for managing versions .

meanwhile Git It can also be used for teamwork , If your friend wants to play this game with you , You're in charge of the function A, He's responsible for the function B, Then you may need to use QQ Send the file , To learn about the latest changes of the other party .

Finally, when it's done , You may also need to manually turn the function A And function B Copy, paste and merge the code . Two people are so troublesome , If it's a slightly larger team of more than a dozen people , This is even more troublesome . So this time Git You can come and help us .

2. other

One of the earliest Git yes Linus use C language-written , And it took less than two weeks of vacation . you 're right Linus That's the one Linux The father of , Kneel to the great God programmer .

The following is an excerpt from Liao Xuefeng's official website , It's the part that I think is more useful

(1) First , The distributed version control system doesn't have “ Central server ”, Everyone's computer is a complete version library , such , When you work , There's no need for networking , Because the version library is on your own computer . Since everyone has a complete version library on their computer , How do many people work together ? For example, if you change a file on your own computer A, Your colleague also changed the file on his computer A, At this time , You two just need to push each other's changes , You can see each other's changes .

Compared with centralized version control system , The security of distributed version control system is much higher , Because everyone has a complete version Library in their computer , It doesn't matter if someone's computer breaks down , Just copy one from someone else . If there is a problem with the central server of the centralized version control system , No one can work .

In the actual use of distributed version control system , In fact, it is rare to push the revision of version library between two people's computers , Because maybe you two are not in the same LAN , Two computers can't access each other , Maybe your colleague is ill today , His computer didn't turn on at all . therefore , A distributed version control system usually has one as well “ Central server ” Our computer , But this server is only for convenience “ In exchange for ” Everyone's modification , Without it, everyone would work as well , It's just that it's not convenient to exchange changes .

(2) All version control systems , In fact, you can only track changes to text files , such as TXT file , Webpage , All the program code and so on ,Git No exception . Version control system can tell you every change , For example, in the first place. 5 Add a word to the line “Linux”, In the 8 Delete a word in line “Windows”. And pictures 、 Video these binary files , Although it can also be managed by version control system , But there's no way to track changes in files , You can only string binary files with each change , That is to say, we only know the pictures from 100KB Changed to 120KB, But what has changed , Version control system doesn't know , I don't know .

Unfortunately ,Microsoft Of Word The format is binary , therefore , Version control system can't track Word The change of the document , If you really want to use version control system , It's about writing documents in plain text .

If there is no historical legacy , It is highly recommended to use standard UTF-8 code , All languages use the same code , There is no conflict , Supported by all platforms .

Never use Windows Edit any text file with Notepad . as a result of Microsoft The team that developed Notepad used a very retarded behavior to save UTF-8 Coded file , They cleverly added... At the beginning of each document 0xefbbbf( Hexadecimal ) The characters of , You're going to have a lot of incredible problems , such as , The first line of the page may show a “?”, It is clear that a correct program will report a syntax error as soon as it is compiled , wait , It's all brought about by the retarded behavior of Notepad . I suggest you download Notepad++ Instead of Notepad , Not only powerful , And it's free .

 

One 、Git install

See https://www.runoob.com/git/git-install-setup.html

Two 、 Local order

1. Simple submission

(1) Initialize the repository git init

First, you can create a simple directory ,cd Go to that directory , That catalog is Git Where the warehouse is located

adopt git init Command to make this directory Git Manageable warehouse

There is one more in the current directory “.git” Folder , This directory is Git To track and manage the folder , Do not manually modify the files in this directory if you have nothing to do , Otherwise, it will be disordered , Just put Git The warehouse was destroyed .

If you don't see it “.git” Catalog , That's because this directory is hidden by default , use ls -ah Orders can see . If it is windows System click “ see ”->“ Show hidden items ” that will do .

(2) Simple submission git add && git commit

We can create a directory called reademe.txt The file of , Try a simple commit to it . The order is as follows

git add readme.txt
git commit -m "wrote a readme file"
#-m This submission is followed by comments , Notes must be added , Otherwise, we can't commit

Why? Git Adding files requires add,commit There are two steps ? because commit You can submit many documents at a time , So you can... Many times add Different files .

I usually use git add . and git commit -m "xxx" Two commands , It's rare to manually add , Because it's too much trouble .

“.” Used to represent all files in the current directory , therefore git add . All files in the current directory can be add go in .

(3) Delete staging area file git rm

git How to delete already add The file of ? Use git rm Command is enough , There are two options ,

One is git rm –cached “ File path ”, Do not delete physical files , Only delete the file from the staging area ;

One is git rm –f “ File path ”, Not only will the file be removed from the cache , Physical files are also deleted ( It won't be recycled to the trash ).

(4) Warehouse status git status && git diff

We can use git status Understand the current state of the warehouse

git diff As the name implies, it is to check difference, The format of the display is exactly Unix General purpose diff Format

2. edition

(1) Submission log git log

git log The command displays the most recent and farthest commit logs .

You see a bunch of things like 1094adb... Yes. commit id( Version number ), and SVN Dissimilarity ,Git Of commit id No 1,2,3…… Increasing numbers , It is a SHA1 It's a very big number , In hexadecimal notation . Why? commit id It needs to be represented by such a large number ? because Git It's a distributed version control system , Later, we will study how many people work in the same repository , If everyone uses 1,2,3…… As version number , That must be a conflict .

(2) Version rollback git reset

git reset The command is used for version fallback .

Or it's for version hopping , Because he can also change the backward version into the previous one .

stay Git in , use HEAD Represents the current version , That is, the latest submission 1094adb...( Pay attention to my submission ID It must be different from yours ), The last version was HEAD^, The last version is HEAD^^, Of course up 100 Version (s) 100 individual ^ It's easier not to count , So it's written as HEAD~100.

Actually ^ and ~ Can be combined to use , So you can even write git reset HEAD^~2, But it's easy to get dizzy , It's not necessary .

git reset There is one --hard Parameters . If you add --hard Parameters ,--hard Parameters will also roll back your own files in the current folder , If you don't back it up elsewhere , Then you will not have the painstaking efforts to revise this time .

--hard Parameters modify the contents of the workspace , No addition --hard When you're running the staging area , No impact on the work area ,--hard One step in place , No addition --hard It needs to be done separately , Two step operation . added --hard Parameters are obviously much more convenient , But it may cause unnecessary loss due to carelessness .

We'll talk about what a workspace is later .

If you don't commit Your local modification ( Even you didn't pass git add I've tracked these documents , When they are deleted ,git reset --hard For those who have not been commit I haven't git add It's a devastating change )

② If we want to cancel the fallback , use git reflog View command history , In order to determine which version to return to in the future .

And then execute git reset --hard [ Version number ], Add... Here --hard Parameters are necessary , Because you obviously don't want to keep your current folder content in the past ,

git reset --hard 10fc

Git The version of backoff is very fast , because Git Inside there is a... Pointing to the current version HEAD The pointer ,Git Just modifying the pointer .

It's not necessary to write all the version numbers , The first few will do ,Git Will automatically find . Of course, you can't just write about the first one or two , because Git Multiple version numbers may be found , You can't be sure which one .

③ Staging and workspace

work area (Working Directory) It's the directory you can see in the computer .

Version Library (Repository) Workspace has a hidden directory .git, This is not a workspace , It is Git Version Library .

Git There's a lot of stuff in the library , The most important one is called stage( Or call it index) Staging area for , also Git The first branch we automatically created master, And pointing master One of the hands of is HEAD.

Let's go to Git When the version library is added , It's done in two steps :

The first step is to use git add Add files to , It's actually adding file changes to the staging area ;

The second step is to use git commit Submit changes , In fact, all the contents of the staging area are submitted to the current branch .

You can simply understand it as , All changes of documents to be submitted shall be placed in the temporary storage area , then , Submit all changes to staging area at once .

Once submitted , If you don't make any changes to the workspace , So the workspace is “ clean ” Of .

Why? Git Better than other version control systems , because Git Tracking and managing changes , Not documents .

(3) Discard workspace changes git checkout -- [ file name ]

git checkout -- [ file name ] Changes to the workspace can be discarded , Such as git checkout -- readme.txt

There are two situations here :

One is "readme.txt" It has not been put into the staging area since the modification , Now? , As like as two peas, the same is true. ;

One is "readme.txt" After it has been added to the staging area , It was revised again , Now? , Undo the changes and go back to the state you added to the staging area .

3. Branch

Before you create a new branch , All our operations are in master On . With a new branch , There can be multiple versions in one repository , Instead of having one version over the other .

(1) Check all branches in the local area git branch

git branch The command can be used to view local branches , The current branch will use * Mark it out .

If your warehouse is associated with a remote warehouse , Need to add -a Parameter to see the remote branch , namely git branch -a

(2) Create a branch git branch [ Branch name ]

git branch [ Branch name ] Used to create a new branch , Such as git branch dev

(3) Delete the branch git branch -d [ Branch name ]

git branch -d [ Branch name ] Used to delete a branch .

(4) Switch branches git checkout [ Branch name ]

git checkout [ Branch name ] Can be used to switch to another branch .

If you add -b Parameters , namely git checkout -b [ Branch name ], It creates and switches to another branch .

Such as git checkout -b dev amount to git branch dev and git checkout dev Abbreviation .

(5) Merging branches git merge [ Branch name ]

git merge test It's equal to putting the current branch and test The contents of the branches are merged .

If you add --rebase Parameters , Will use rebase Methods to merge . The two merger strategies are different ,rebase Merging is cleaner ,merge Merging is easy .

About how to merge , It has its own algorithm , You can see “ 6、 ... and 、 Recommended links ” Inside “Git principle ” and "Git Branch merging ".

3、 ... and . Remote warehouse related

This is often the case , Find a computer to act as a server , Every day 24 Turn it on every hour , Everyone else from this “ The server ” Clone a copy of it to your computer , And push each submission to the server warehouse , Also pull other people's submissions from the server Repository .

1. start

(1)SSH key

When you want to pull a warehouse from the far end , perhaps push When the local warehouse goes to the remote end , Need to verify your identity , Otherwise, anyone can modify the warehouse , It's dangerous .

You are usually asked to enter a user name and password , If you often operate a warehouse , It's too cumbersome to enter a user name and password every time .

You need to set SSH key To verify your identity , The remote warehouse gives your computer a white list .

About how to set up SSH key You can search for it yourself .

(2) Cloning of warehouse git clone

git clone It's the simplest command ,git clone < Warehouse address > You can put the remote warehouse directly clone To local , Simultaneously being clone The remote warehouse will become the default associated remote warehouse of this local warehouse .

git clone It will also copy the remote branch to a local one , This branch is not updated in real time , It's just copied to your local place .

Remote branch naming conventions are usually <remote name>/<branch name>

Remote branches cannot be modified directly , When you try to checkout After the remote branch ,Git It will become separation HEAD state (HEAD Point to nodes, not branches ), So it can't be modified .

You need to modify the local branch locally , Then copy the local branch to the remote branch .

About how to operate , This is explained below .

(3) Clone warehouse designated branch git clone -b

If a warehouse has many branches , But you just want one of them , You can use the command :git clone -b < Branch name > < Warehouse address >

(4) View all branches git branch -a

add -a Parameter to see all branches ( Include remote branches )

(5) Copy remote branch data git fetch

Getting data from a remote repository git fetch

  • Download the missing submission records from the remote warehouse

  • Update remote branch pointer ( Such as o/master)

git fetch In fact, the remote branch in the local warehouse is updated to the latest status of the corresponding branch in the remote warehouse .

git fetch Does not change the state of your local warehouse . It won't update you master Branch , And it won't change the files on your disk .

in other words git fetch Only change the state of your local remote branch .

(6) Associated remote warehouse git remote

If you use git clone Command to clone the remote repository , Will automatically establish an association with the cloned remote repository , You can skip this step .

But if you have a folder of your own , adopt git init The command turned it into a warehouse , Want to establish a connection with a remote warehouse .

You can use the following command to establish a connection :

git remote add origin [ Warehouse address ] # Add a remote warehouse as the remote warehouse of the current warehouse 

If you want to change to an associated remote repository , You can delete the current .

git remote rm origin # Delete the remote warehouse associated with the local warehouse 

So you can continue to associate with the new remote repository .

The remote repository associated in this way , When you try to do push In operation , It will warn you , And you can't push.

Because your local warehouse is a new one , Not consistent with the commit log of the remote repository .

You need to start from remote pull once , A merge with the remote warehouse , In this way, your local repository will have a submission log similar to that of a remote warehouse , Now push There will be no problem going up there .

Of course, if you have a head iron ( Very not recommended ), You can add -f Parameter execution git push origin master -f Force push In the past .

2.push and pull

(1) Pull a branch git pull

git pull yes git fetch and git merge An abbreviation for two commands .

First step : Synchronize the latest changes of remote warehouse to local remote branches ;

The second step : Put the local remote branch and the current branch ( Or specify the branch ) A merger .

Some common commands :

git pull origin < Remote branch name >:< Local branch name > # Branch will be specified remotely   Pull to   On the local designated branch :
git pull origin < Remote branch name > # Branch will be specified remotely Pull to On the local current branch :
git pull # The remote branch that will have the same name as the local current branch Pull to On the local current branch ( You need to associate the remote branch first )

If git pull origin < Remote branch name >:< Local branch name > Leave the local branch name of the , A local branch will be created .

Methods of associating remote branches :git push --set-upstream origin < Local branch name >

(2) Push the branch git push

push The operation modifies the remote branch of the local warehouse , And it will modify the remote branch of the remote warehouse .

Common commands :

#( Be careful :pull It's remote in front, local in back ,push contrary )
git push origin < Local branch name >:< Remote branch name > # Will local current branch Pushed to the On the remote specified branch
git push origin < Local branch name > # Will local current branch Pushed to the On the remote branch with the same name as the local current branch
git push # Will local current branch Pushed to the On the remote branch with the same name as the local current branch ( You need to associate the remote branch first )

If git push origin < Local branch name >:< Remote branch name > Leave the local branch name of the , This remote branch will be deleted .

3. Remedies

(1) Fallback remote branch version

This operation cannot be performed directly on the remote branch .

We need to roll back the local branch to the previous version , And then again push To remote .

git reset --hard HEAD~1 # Roll back the local branch to the previous version 
git add .
git commit -m "rollback"
git push -f origin < Remote branch name > # Have to add -f Parameters

-f The parameter is mandatory submission , because reset After that, the local library is one version behind the remote library , So you need to force submission .

You can also use revert Command to go back .revert Is to discard the specified commit changes , But a new submission will be generated , You need to fill in the submission notes , The history of the past is in , and reset It means to be HEAD Pointer to the specified commit , There will be no abandoned commits in the history .

git revert It's copying the last record to a branch .

Specific use revert still reset Communicate with the team leader .

(2) Delete remote branch

I often create a branch with the wrong name , Every time this happens, I want to delete it at the speed of light ......

git push origin --delete < Remote branch name >

Four 、Git Intranet agent

Summer vacation and the development of small partners together ,git The warehouse is on the campus network , But my body and computer are at home . So I used vmess Agent to access the campus intranet .

If the company's warehouse is on the intranet , Or need to be right github The warehouse is speeding up ( If you've done the front work ), You can refer to my practice .

Even though I used vmess agent , The web page can visit the campus network normally , But still can't pull git Warehouse , Later it was suspected that git I didn't take this vmess Because of agency .

Enter the following command on the command line to solve the problem :

# Setting agent 
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy https://127.0.0.1:7890
# Cancel the set command 
git config --global --unset http.proxy
git config --global --unset https.proxy

Please put 7890 Change to your own software (clash,v2rayn) The port on which the proxy is performed .

The above method is to hang the global proxy , As long as it is git The operation is done by proxy , If you hang up with a foreign agent , May pull github Soon , It's slow to get the domestic warehouse , At this time, it's better to be only right about github Make this setting .

git config --global http.https://github.com.proxy http://127.0.0.1:7890
git config --global https.https://github.com.proxy https://127.0.0.1:7890

 

5、 ... and 、 About Git Other questions

1. When you're developing on your own , It's customary to make changes on your own . But when a team develops, it usually needs to create a new branch , Name the branch name according to the agreement, indicating the purpose of this revision , then Merge In the past , Now it makes perfect sense to think about it , But the first time I didn't pay attention to this, I made a joke .

2. But in the team , We use Gitlab The warehouse of , Each time you merge into the main branch, you need to send a Merge Request Check with the person in charge .

6、 ... and 、 Recommended reading

1.Git Branching exercises https://learngitbranching.js.org/

2.Git principle https://www.lzane.com/tech/git-internal/

Git Branch merging https://www.lzane.com/tech/git-merge/

版权声明
本文为[KEE]所创,转载请带上原文链接,感谢