git
git What is it?
- Officially :Git Is a free open source distributed version control system , Designed to handle everything from small to large projects quickly and efficiently
- To quote Mr. Liao Xuefeng , It can automatically help me keep track of every file change , You can also let colleagues work together to edit , So you don't have to manage a bunch of similar files yourself , There's no need to send documents around . If you want to see a change , You just need to meow in the software .
Why study Git
- Interview to be asked . Can handle interviews ‘
- Many companies use GIt To deal with projects . Not now , I'm sure I'll learn more in the future
- in my opinion Git It's something all programmers need to master today , In the future, we will work with colleagues to develop projects that are bound to be used , Have a good command of Git command , Can improve development efficiency
install git
- windows
Download it directly from the official website . When the download is complete , Right click any file, if any Git Bash Here The installation is successful . After installation , And then you have to type it on the command line
$git config --global user.name " What's your name "
$git config --global user.email " Your email "
global Global representation , This machine has all the GIt The repository will use this configuration . Allow a single warehouse to use another name and mailbox
Warehouse
- A local warehouse is for a remote warehouse
- Local repository = work area + Version area
- A workspace is a collection of files on disk
- The version area is
.git
file - Version Library = Temporary storage area (stage) + Branch (master) + The pointer (head)
- I use it most frequently git Command as an example , Submit to github For example
git init
Originally, the local warehouse only contained the working area , This is the most common working state . here ,git init
once , Indicates that a.git
file , Build version areagit add .
It means to submit all the files in the workspace to the staging area in the version area- Of course you can too
git add ./xxx/
Batch by batch to the staging area git commit -m 'xxx'
Submit all documents from the staging area to the warehouse area , The staging area is emptygit remote add origin https://github.com/name/name_canku.git
Connect the local warehouse to the remote warehousegit push -u origin master
Submit the files in the warehouse area to the remote warehouse- Once submitted , If you don't make any changes to the workspace , So the work area is clean . There will be information like this
noting to commit,working tree clean
Submitted to the Github
git init
. initialization , Turn this file into Git Manageable warehouse . After initialization, open the hidden file and you can see that there is a.git
file .git add .
The following point indicates that all the files are submitted to the staging area .git add ./readme.md/
Put the file below readme.md File submitted to the staging area .git commit -m " You have to comment on something "
git commit
It means to submit all the files in the temporary storage area to the local warehouse .-m
Followed by comments .git remote add origin https://github.com/name/name_cangku.git
Connect your local warehouse with GitHub Connect remote warehouses on the Internet . Just connect once , You don't need to thank this command when you submit it later .name
Is your github name ,name_cangku
It's your warehouse name . Be careful Don't put the back.git
It's missing . Because that's how I came in front of me , It took a lot of detours . As for how to be in GitHub New warehouse on , There are many tutorials online , I won't repeat it here .git push -u origin master
Submit the local warehouse to the remote warehouse .( The last step ) Refresh on your remote repository to see the files you submitted .- Last but not least , stay
git commit -m ""
Before , Can be repeatedgit add
To temporary storage area . howevergit commit
All the files you have stored in the temporary storage area before will be sent to you Disposable All submitted to local warehouse .
Retrospection and advance of version
Submit a document , Sometimes we submit many times , In the history of submission , So there are different versions . Each submission ,GIt It'll string them into a timeline . How to go back to the last version we submitted , use git reset --hard+ Version number
that will do . Version available git log
Check it out. , Each version will produce a different version number . After going back ,git log
Check it out and find that our latest version is gone . But we also want to move on to what the latest version should be ? as long as git reset --hard + Version number
Just go . Let's take a step back , Although we can pass git reset --hard+ Version number
, By remembering the version number, you can go back and forth between different versions . however , Sometimes you lose the version number ?git reflog
I've recorded every order for you , So you can find the version number , So you can go through git reset
Here we go .
revoke
- scene 1: In the work area , You changed something , You want to undo the changes ,
git checkout --file
. Miss Liao Xuefeng pointed out as like as two peas , Use the version in the version library to challenge the version in the workspace . - scene 2: You've modified a content , And already
git add
It's in the staging area . What to do with revocation ? Backtracking version ,git reset --hard --hard + Version number
, Againgit checkout --file
, Replace the version of the workspace - scene 3: You've modified a content , And already
git commit
here we aremaster
. Follow the scene 2 equally , Version backtracking , In the process of revocation
Delete
- If you
git add
A file to the staging area , And then delete the file in the workspace ,git You'll know you deleted the file , If you delete files from the repository ,git rm
alsogit commit -m 'xxx'.
- If you delete files in the workspace by mistake , What do I do ? Use the undo command ,
git checkout --<file>
Can . This proves once again that the undo command is actually replacing the version in the workspace with the version in the version library , Whether the workspace is modified or deleted , All can be restored with one click
Branch
Branch , It's like a parallel universe , This is what Mr. Liao Xuefeng said . You create a branch of your own , No one else can see , And continue to work on the original branch , And you work in your own branch , Submit to, submit to , Until the end of development , Merge to the original branch once more , such , Both safe , It doesn't affect other people's work
Create and merge branches
When there are no other branches coming in , only one master The main branch . Every time you git push -u origin master
Submit is to add an event axis ,master It's going to move with it
Create a other The branch of , adopt other Submit , Although the timeline goes forward , But the main branch is still in its original position
The theoretical analysis is finished , Let's see what the order says
- Create a branch
other
, Switch toother
Branch
git branch other
git checkout other
- View all current branches
git branch
* other
master
The current branch will have a *
- use
other
Submit
git add ./xxx/
git commit -m 'xxx'
other
Branch to complete , Switching meetingmaster
git checkout master
here ,master There's No... on the branch other
The file of , Because the branches haven't merged yet
- Merging branches
git merge other
- After the merger , You can go to master The file is viewed on the branch
- Delete
other
Branch
git branch -d other
- In the future work , It should be a development team working together to develop a project , The team leader will create many branches , Each branch can be handed over to one person to develop a certain function , It's developed by a team that doesn't interfere with each other . Whose function is complete , You can have a team leader to merge the completed branches .
Solve the problem of merging branches
If there is such a situation , Branch other
already commit
了 , But at this point the pointer points back to master
when , also master
No merger , It is git add/commit
submitted . such , There's a conflict , The main branch master
The content of the document and other
The content of the branch is different . It doesn't merge
- Modify the contents of the file , Make it consistent .
git add ``git commit
Submit- The branches merge
git log --graph
View branch merge graphgit branch -d other
Delete the branch , End of the task .
Delete the branch
git branch -d + Branch
It is possible that the deletion will fail , Because it protects branches that are not mergedgit branch -D + Branch
force delete , Discard branches that are not merged
Multiplayer collaboration
git remote
View remote library information , Will be displayedorigin
, The default name of the remote warehouse is origingit remote -v
Show more detailsgit push -u origin master
pushmaster
Branch toorigin
Remote warehousegit push -u origin master
pushother
Toorigin
Remote warehouse
Grab the branch
When there's a conflict in the picture above ,
git pull
Grab the latest Submission from the remote repository , Merge locally , Resolve conflicts . Proceed againgit push
- If
git pull
And I failed , Also specify links between branches , This step git I know how to do it . And then againgit push
Teacher Liao Xuefeng's summary : The working mode of multi person cooperation is usually like this :
- First , You can try to use
git push origin <branch-name>
Push your own changes - If push fails , Because the remote branch is newer than your local , We need to use it first.
git pull
Attempt to merge - If there is a conflict in the merger , Then the elder sister conflicts , And submit locally
- There is no conflict or after the conflict is resolved , Reuse
git push origin <branch-nam>
Push will succeed - If
git push
Tipsno tracking information
, The link relationship between the local branch and the remote branch is not created , Commandgit branch --set-upstream-to <branch-name>origin/<branch-name>
Label management
For example, a APP Go online , It's usually labeled in the repository (tag), This determines the tagged version . Whenever in the future , Take a version of a label , Is to take out the historical version of the time when the label was placed . therefore , The tag is also a snapshot of the version Library
git Although the label is a snapshot of the version Library , But it's actually pointing to something commit The pointer to .
tag In fact, it's a meaningful name that people can easily remember , It's with a certain one. commit Tied together . such as
tagv2,1
It's a version of history calledv2.1
Create a label
git branch
View current branch ,git checkout master
Switch tomaster
Branch .git tag <name>
tagging , The default isHEAD
. such asgit tag v1.0
The default tag is on the latest submittedcommit
Upper . If you want to tag in the pastcommit
On , wantgit log
Find history submittedcommit
id- If one
commit id
yesdu2nd9
, performgit tag v1.0 du2nd9
Just type this versionv1.0
The label git tag
View all labels , You can know the historical version oftag
- Labels are not listed in chronological order , It's in alphabetical order
git show <tagname>
View label informationgit tag -a < Tag name > -m " explain "
, Create labels with instructions .-a
Specify label name ,-m
Specify description text . useshow
You can see the instructions
Operation label
git tag -d v1.0
Remove the label . Because the created tags are only stored locally , It doesn't push automatically , therefore , Wrong tags can be deleted safely locallygit push origin <tagname>
Push a label to remotegit push origin --tags
All one-time push has not yet been pushed to remote local tags- If the tag is pushed remotely .
git tag -d v1.0
Delete local label first v1.0.git push origin:refs/tags/v1.0
Remove remote label v1.0
Commonly used git Command summary
git config --global user.name " What's your name "
Let all of youGit
Warehouse binding your namegit config --global user.email " Your email "
Let all of youGit
The warehouse binds your emailgit init
Initialize your warehousegit add .
Submit all the files in the workspace to the staging areagit add ./<file>/
Put... In the work area<file>
File submitted to the staging areagit commit -m "xxx"
Submit all documents from the staging area to the warehouse area , The staging area is emptygit remote add origin https://github.com/name/name_cangku.git
Connect the local warehouse to the remote warehouse
git push -u origin master
Take the main branch of the warehouse area master
Submit to the remote repository
git push -u origin < Other branches >
Submit other branches to the remote repositorygit status
View the status of the current warehousegit diff
Check the details of the file modificationgit log
Show the most recent to farthest submission historygit clone + Warehouse address
Download the clone filegit reset --hard + Version number
Backtracking version , The version number iscommit
Time andmaster
Follow togethergit reflog
Show command historygit checkout -- <file>
Revocation order , Replace the files in the workspace with the files in the version library . I think it's likeGit
Worldctrl + z
git rm
Delete version library filesgit branch
View all current branchesgit branch < Branch name >
Create a branchgit checkout < Branch name >
Switch to branchgit merge < Branch name >
Merging branchesgit branch -d < Branch name >
Delete the branch , It is possible that the deletion will fail , becauseGit
Will protect branches that are not mergedgit branch -D + < Branch name >
force delete , Discard branches that are not mergedgit log --graph
View branch merge graphgit merge --no-ff < Branch name >
Disable when merging branchesFast forward
Pattern , Because this pattern will lose branch history informationgit stash
When other tasks come in , Put the current job site “ Storage ” get up , Continue to work after recoverygit stash list
See what you just “ Deposit ” Where's the work I got up fromgit stash apply
Restore without deletingstash
Contentgit stash drop
Deletestash
Contentgit stash pop
Restore at the same time stash The content has also been deletedgit remote
View remote library information , Will be displayedorigin
, The default name of the remote warehouse isorigin
git remote -v
Show more detailsgit pull
Grab the latest Submission from the remote repository , Merge locally , andgit push
contrarygit rebase
Split the history of submission “ Arrangement ” In a straight line , It looks more intuitivegit tag
View all labels , You can know the historical version of taggit tag <name>
tagging , The default isHEAD
. such as `git tag v1.0git tag <tagName> < Version number >
Label the version number , The version number iscommit
when , A string of alphanumerics following
git show <tagName>
View label information
git tag -a <tagName> -m "< explain >"
Create labels with instructions .-a
Specify label name ,-m
Specify description textgit tag -d <tagName>
Remove the labelgit push origin <tagname>
Push a label to remotegit push origin --tags
All one-time push has not yet been pushed to remote local tagsgit push origin :refs/tags/<tagname>
Remove remote label `
git config --global color.ui true
Give Way Git Display color , Will make command output look more eye-catching
git add -f <file>
Force submission of ignored filesgit check-ignore -v <file>
Check why Git The file is ignored