Write it at the front
You used Git Do you ？ Maybe you haven't been exposed to Git, Maybe you've been using it for a while , But it may still confuse you .
This article mainly explains self study Git How to solve all kinds of problems you meet Git problem .
First of all, I strongly recommend the Nuggets album to you Git Principle explanation and practical guide .（ Not to ）
- If you're new , Read this booklet and follow the actual operation , You will be right. Git Have a general understanding .
- If you've used it for a while Git, After reading the pamphlet , It'll give you a sense of urgency .
- This booklet suggests that you read it over and over again （ I think at least three times ）, Every time you read, you'll get something different .
Git Common commands
git Four company
git add . Put all changes in the staging area git commit -m " describe " Submit with summary information git pull Pull the code from the remote repository git push Push code to remote warehouse （master Branch ）
Other common commands
git log Check the log git log -p Check out the detailed history git log --stat View brief statistics git status View workspace status git branch name Create a branch git checkout name Switch branches git checkout -b name Create and switch to a new branch git branch -d name Delete the branch （ Cannot delete the current branch , Cannot delete not merged into master The branch on ） git branch -D name Delete the branch （ Can delete not merged into master The branch on ） git commit --amend For the latest one commit Amendment git reset --hard HEAD^ Discard the latest submission （ Uncommitted content will be erased ） git reset --soft HEAD^ Discard the latest submission （ Uncommitted content will not be erased ） git revert HEAD^ Go back to some commit git rebase Target base point Reset the base point git merge name Merge branches into head The branch that points git push origin localbranch Push code to the specified branch of the remote repository git push -d origin branchName Delete remote branch git stash Staging code git stash pop Pop up temporary code
Alias configuration for some commonly used commands , Improve your work efficiency
git config --global alias.st status git status ==> git st git config --global alias.ci commit git commit ==> git ci git config --global alias.co checkout git checkout ==> git co git config --global alias.br branch git barnch ==> git br git config --global alias.sh stash git stash ==> git sh git config --global alias.pop "stash pop" git stash pop ==> git pop
Common problems and solutions
git clone Failure
Could not read from remote repository. Please make sure you have the correct access rights
SSH key invalid perhaps I don't have authority
terms of settlement （ One ）—— To add SSH key
1.ssh-keygen -t rsa -C "username" ( notes ：username For you git User name on )
2.Generating public/private rsa key pair.
Enter file in which to save the key (C:Users You're going to have a good time ┈/.ssh/id_rsa): Directly enter
3.C:Users You're going to have a good time ┈/.ssh/id_rsa already exists.
Overwrite (y/n)? y Input y
4.Enter same passphrase again: Directly enter
5.Your identification has been saved in C:Users You're going to have a good time ┈/.ssh/id_rsa. The address where the private key is stored
Your public key has been saved in C:Users You're going to have a good time ┈/.ssh/id_rsa.pub. Public key storage address
6. Find the public key according to the path , Add the public key to Git On
terms of settlement （ Two ）—— Use http To clone the address of
In this way, you need to enter your own account and password , A little bit of a problem , Not recommended
git pull Failure （ One ）
Your local changes to the following files would be overwritten by merge:
Someone else modified the file and submitted it to the repository , And I modified the file locally , Causes conflicts when pulling code
terms of settlement —— Storage changes
Do the following in turn
git stash Restore workspace to last committed content , At the same time, backup the local repair
git pull Pull
git stash pop Pop up your latest saved content
Check the corresponding file Resolve conflicts
then git Three companies submit their own code
git pull Failure （ Two ）
Pulling is not possible because you have unmerged files.
The revised document was not submitted
This mistake is actually like this —— Actually, I've been pull After the code , And then there was conflict , After resolving the conflict , I want to pull A mistake in the times , I found out later , Once the conflict is resolved, it needs to be done again commit Of
terms of settlement —— Submit to local
git add . Uh, yes , here commit We need to start with add once
git commit -m " Get new code "
git push Failure
fatal: Could not read from remote repository. Please make sure you have the correct access rights
The reason a ：github The latest public key is not added to the Reason two ： Network not connected
Reason one solution —— Configure the public key
1. Find the public key and open , And copy the content
2. Add public key to github
Tell me why , because github and gitlab Share the same public key and private key , When working on company projects , I clone Failure （ The first mistake above ） when , Reconfigured public and private keys , So at this time my github I don't have my latest public key on , That makes me unable to push
Reason two, the solution —— Connect to the network
- The cable is loose
- Out of the company intranet , Need to login again
- WiFi No net
Undo changes to the document
Modified a complex index.vue file , After revision, I feel that I wrote in a mess , No clue , But this modification file Not submitted , I want to get back to where it started .
terms of settlement
Run the command
git status Get the path to our file
git checkout -- File full path （ It doesn't seem to add -- These two horizontal lines can also make ）
Then close the file and open it To remind you whether to save the changes Remember not to save
This is a very dangerous order , After doing this git Will use the latest commit Cover up the entire file .
Unless you really know you don't want to make local changes to that file , Otherwise please do not use this command .
Your own code is pull Next code coverage
My own code has just been submitted , My colleague then pulled down my code and submitted it , And then I'll put the code back again pull Come down , I found that all the code I just wrote was gone （ remarks ： The same document I wrote about the contract ）
terms of settlement （ One ）
git log Find the last submitted commit code
git reset --hard Copy of the commit code Then close the file and open it To remind you whether to save the changes Remember not to save
terms of settlement （ Two ）
ctrl + z To use this method, you must know , What documents have you changed , And the editor has not been closed （ I just got stuck in the editor , And then restart the editor .....）
Just want to pull the remote code Don't want to commit
I only wrote a little bit of my own code , A colleague next to him said he submitted , Call me pull once , Because I didn't write anything , So I don't want to commit
A whimsical attempt
I thought , Can you just pull, Of course not ,git I will report you the following mistakes
our local changes to the following files would be overwritten by merge:
terms of settlement
git stash Save your own code
git pull Pull the code
git stash pop Pop up staging
Want to go back to pull The state before
commit after , Code pull Come down , There's a lot of conflict , And then I want to go back to pull The state before , Format the code before pull
terms of settlement
git merge --abort Back to the pre conflict state
git merge --abort The merge process is discarded and attempts to rebuild the pre merge state . however , When the merger begins, if there is no commit The file of , git merge --abort In some cases, it will not be possible to reproduce the state before merging .（ Especially these are not commit The file will be modified during the merge process ）
Check out your commit Record
The company requires a log , I want to check my own commit Record to log ,
terms of settlement
git log This method is flawed , Only the last time push At the time of the commit Record Last , Only in GitLab I've seen the history of
It is not recommended to use Git Third party visualization tools for , First of all, some written examination or interview is to examine Git Of , Second, feel to use git After the visualization tool , It doesn't smell like that .
This article is continuously updated , If you are interested, please pay attention to , If you have a problem, you can join the group 973961276 To discuss .