Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

Git

Reading is boring 2021-01-14 13:02:28 阅读数:4 评论数:0 点赞数:0 收藏数:0

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

img

  • 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 area
    • git 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 empty
    • git remote add origin https://github.com/name/name_canku.git Connect the local warehouse to the remote warehouse
    • git 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

  1. 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 .
  2. git add . The following point indicates that all the files are submitted to the staging area .
  3. git add ./readme.md/ Put the file below readme.md File submitted to the staging area .
  4. 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 .
  5. 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 .
  6. 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 .
  7. Last but not least , stay git commit -m "" Before , Can be repeated git add To temporary storage area . however git 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 , Again git checkout --file, Replace the version of the workspace
  • scene 3: You've modified a content , And already git commit here we are master. 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 also git 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

img

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

img

Create a other The branch of , adopt other Submit , Although the timeline goes forward , But the main branch is still in its original position

img

The theoretical analysis is finished , Let's see what the order says

  • Create a branch other, Switch to other 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 meeting master
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

img

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

img

  • git log --graph View branch merge graph
  • git 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 merged
  • git branch -D + Branch force delete , Discard branches that are not merged

Multiplayer collaboration

  • git remote View remote library information , Will be displayed origin, The default name of the remote warehouse is origin
  • git remote -v Show more details
  • git push -u origin master push master Branch to origin Remote warehouse
  • git push -u origin master push other To origin Remote warehouse

Grab the branch

img

When there's a conflict in the picture above ,

  • git pull Grab the latest Submission from the remote repository , Merge locally , Resolve conflicts . Proceed again git push
  • If git pull And I failed , Also specify links between branches , This step git I know how to do it . And then again git 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 Tips no tracking information, The link relationship between the local branch and the remote branch is not created , Command git 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 called v2.1

Create a label

  • git branch View current branch ,git checkout master Switch to master Branch .
  • git tag <name> tagging , The default is HEAD. such as git tag v1.0 The default tag is on the latest submitted commit Upper . If you want to tag in the past commit On , want git log Find history submitted commit id
  • If one commit id yes du2nd9, perform git tag v1.0 du2nd9 Just type this version v1.0 The label
  • git tag View all labels , You can know the historical version of tag
  • Labels are not listed in chronological order , It's in alphabetical order
  • git show <tagname> View label information
  • git tag -a < Tag name > -m " explain ", Create labels with instructions .-a Specify label name ,-m Specify description text . use show 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 locally
  • git push origin <tagname> Push a label to remote
  • git 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 you Git Warehouse binding your name
  • git config --global user.email " Your email " Let all of you Git The warehouse binds your email
  • git init Initialize your warehouse
  • git add . Submit all the files in the workspace to the staging area
  • git add ./<file>/ Put... In the work area <file> File submitted to the staging area
  • git commit -m "xxx" Submit all documents from the staging area to the warehouse area , The staging area is empty
  • git 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 repository
  • git status View the status of the current warehouse
  • git diff Check the details of the file modification
  • git log Show the most recent to farthest submission history
  • git clone + Warehouse address Download the clone file
  • git reset --hard + Version number Backtracking version , The version number is commit Time and master Follow together
  • git reflog Show command history
  • git checkout -- <file> Revocation order , Replace the files in the workspace with the files in the version library . I think it's like Git World ctrl + z
  • git rm Delete version library files
  • git branch View all current branches
  • git branch < Branch name > Create a branch
  • git checkout < Branch name > Switch to branch
  • git merge < Branch name > Merging branches
  • git branch -d < Branch name > Delete the branch , It is possible that the deletion will fail , because Git Will protect branches that are not merged
  • git branch -D + < Branch name > force delete , Discard branches that are not merged
  • git log --graph View branch merge graph
  • git merge --no-ff < Branch name > Disable when merging branches Fast forward Pattern , Because this pattern will lose branch history information
  • git stash When other tasks come in , Put the current job site “ Storage ” get up , Continue to work after recovery
  • git stash list See what you just “ Deposit ” Where's the work I got up from
  • git stash apply Restore without deleting stash Content
  • git stash drop Delete stash Content
  • git stash pop Restore at the same time stash The content has also been deleted
  • git remote View remote library information , Will be displayed origin, The default name of the remote warehouse is origin
  • git remote -v Show more details
  • git pull Grab the latest Submission from the remote repository , Merge locally , and git push contrary
  • git rebase Split the history of submission “ Arrangement ” In a straight line , It looks more intuitive
  • git tag View all labels , You can know the historical version of tag
  • git tag <name> tagging , The default is HEAD. such as `git tag v1.0
  • git tag <tagName> < Version number > Label the version number , The version number is commit 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 text
  • git tag -d <tagName> Remove the label
  • git push origin <tagname> Push a label to remote
  • git push origin --tags All one-time push has not yet been pushed to remote local tags
  • git 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 files
  • git check-ignore -v <file> Check why Git The file is ignored
Copyright statement
In this paper,the author:[Reading is boring],Reprint please bring the original link, thank you