preface
Recently went to drops internship, and then also start to make the demand, because haven’t zha specific understanding before the use of git, only know simple code pull and submit, in company with the gitlab warehouse, then I think it is necessary to mend their own a piece of knowledge, also convenient and make a summary, convenient back after check, At the same time, it is also convenient for some children who do not have a detailed understanding of git use.
Git some common directives and specifications
When we finish writing code must be the need to submit code, this time you need to use Git, so we must understand some common Git instructions. In large projects with many people working together, we also need to know some rules.
First of all, we need to install Git on our computer. After installing Git, we need to do some configuration and set up SSH before we can use Git normally
Git official website You can find the download address of Git on the official website
After installing Git, you can open the git bash console on Windows to use git commands. On MAC, you can press Command + space. Search terminal to open the console, and then type git –version to see the git version we installed (after entering Didi, the company issued a Macbook Pro, and it was the first time to use MAC. I have to say that MAC is really friendly to programmers, saving a lot of trouble on Windows).
Then we need to configure the user environment for the first time using Git.
Initialize the git
When using Git for the first time, you need to configure a user name and email address for Git. You can use github or your own git Lab repository account
#Configuring a User NameGit config --global user.name git config --global user.name#Configure your emailGit config --global user.emailCopy the code
Once this is configured, we can type in and see all of our configuration information, and then see if user.name and user.email are configured correctly
git config -l
Copy the code
In addition to the email and user name, we also need to configure the SSH key (of course, it is ok not to configure, that is, every time we need to enter the password when we pull and push code, it is troublesome, so we should configure the SSH key once and for all for future development).
Add an SSH key to Github
For details on how gitlab can configure SSH keys, please refer to this article
Git Basic Use
In development projects, we generally need to create our own code branch, which is easy to cause conflicts when multiple people are developing at the same time. Therefore, it is a good choice for each of us to have his or her own development branch. Finally, when the development is completed and launched, we will submit the pre-release environment branch for testing. When the final test is completed and passed, it is submitted to the master branch of the project, and then it can be deployed through CI/CD. This is a set of basic development processes, so let’s look at how to create your own branch.
1. Pull items
We go to the company, the first is to clone the company’s code to the local
Git Clone repository addressCopy the code
After clone was completed, the local repository was connected to the remote repository itself, and then we could carry out our own development and submit our own code.
2. Create branches
After first pulling the project down, we will create our own development branch, and then we will commit the code mainly on our own development branch
We can start by looking at all the branches so far
git branch -a
Copy the code
You can then create your own branch
Git Branch Branch nameCopy the code
Switch to the branch you created
Git Checkout is the branch you want to switch toCopy the code
The two instructions above can be combined into the one below to create and switch to the branch
Git checkout -b branch nameCopy the code
After the branch is created and switched, view your current branch
git branch
Copy the code
Commit code to cache
git add .
Copy the code
4. View the data that has been committed to the cache
git status
Copy the code
5. Delete the files in the cache
Gitignore accidentally uploaded something you didn't want to uploadCopy the code
6. Comments for code submission
Generally, when submitting code, we need to write comments, and there are certain specifications for writing comments, and then this specification is related to your company, you can submit according to your company’s specifications. In addition, we usually use submission specifications like the following:
Feat: Add new functions
Fix: Fixes bugs
Docs: Changes only the document
Style: format (changes that do not affect code execution)
Refactor: refactoring
Test: Adds a test
Git commit -mCopy the code
Git commit -m “feat: complete the order download module”
5. Code submission
Once the code is written, it can be submitted to our own development branch
Git push origin commit branch nameCopy the code
This is a set of simple pull other people’s repository code and the development of the submission code process.
Code repository setup
We can create a Git repository in the folder of a project, which will give us some files about Git (probably hidden folders).
git init
Copy the code
Or we can use the following command to create a new directory that will act as a Git code base
Git Init project nameCopy the code
Remote operation instruction
1. Display all remote warehouses
git remote -v
Copy the code
2. Get changes in the remote warehouse
Git merge Git merge Git Merge Git merge
Git fetch origin branch nameCopy the code
3. Merge the code
Git merge branch nameCopy the code
4. Pull the remote repository code and merge with the local branch
Git fetch is a combination of git merge and Git fetch. Git merge is a combination of git fetch and Git merge. Git merge is a combination of git fetch and Git merge
git pull
Copy the code
5. Forcibly push the current branch to the remote repository, even if there is a conflict
git push --force
Copy the code
Other instructions
1. Display the changed file status
git status
Copy the code
2. Display the current branch version history
git log
Copy the code
3. Display the submitted history and changed files
git log --stat
Copy the code
4. Display the past 5(n) commits
git log -5 --pretty --oneline
Copy the code
5. Display all users of the warehouse who have submitted codes, and rank them according to The Times of submission
git shortlog -sn
Copy the code
6. Show the number of lines of file changes and code changes committed today
git diff --shortstat "@{0 day ago}"
Copy the code
Code the rollback
Before rolling back the code, we first use git log to view our code commit record, and then after viewing the code commit record we can roll back according to the version
1. Roll back to the previous version
git reset --hard HEAD^
Copy the code
2. Roll back to before n commits
git reset --hard HEAD~n
Copy the code
3. Roll back to the specified committed version
Git reset --hard commit hash value
#The hash value is just input gitlogA large string of characters that can be seen later
#Such as git reset - 92 f1eb5aa5db9e04753e75a37ffd76f793cb281e hard
Copy the code
After the rollback, it is possible that the code will fail to commit and must be forced to remote
git push origin HEAD --force
Copy the code
4. Go back to the original state of merge and cancel the merge modification
git merge --abort
Copy the code
Remote warehouse control
1. View the address of the remote warehouse
git remote -v
Copy the code
2. Change the address of the remote warehouse
Git remote set-url origin Specifies the repository URLCopy the code
3. Delete the remote warehouse address
git remote rm origin
Copy the code
4. Add the remote warehouse address
Git remote add Origin Repository URLCopy the code
conclusion
Is some of the basic use of git commands above, there are many, many orders and we also need to understand some of the git principle, back to use to know when can, now these instructions have basic daily development we use enough, so for now the record, facilitates the use of access, and hope can help you.