foreword
Recently, I have frequently used git version management in my work, and encountered a lot of problems during the period. I also use it at ordinary times. However, when I did not encounter any major problems, I was not particularly skilled in using it. Recently, I was playing by myself. At the time, I found a lot of problems. At the same time, I also solved these problems with git. I found that git is really a good thing. ! !
So, take a moment today to summarize some of the most commonly used git commands. No matter when, these commands need to be used proficiently, so as to ensure that there is no error, let's start! ! !
Create repository/initialize/commit operations
1、git init
Initialize warehouse operations so that git can be used for code management.
Here I share a collection of my recent original articles.
2. git clone repository address
Copy the code of the remote repository to the local.
3、git add XXX
Add a new local file to the local repository, but at this time, it is only submitted to the local repository, not to the remote repository.
4、git add .
The difference between this operation and the above is that this command will add all new files, that is, in the current directory.
5、git commit -m 'message'
Submit the code to the local warehouse, but not to the remote warehouse. If you don't understand it, you can learn about the principle of git.
6、git commit -am 'message'
This command combines the above two steps add and commit into one.
Log View/Information Display
1、git log
This command is mainly used to view the commit log
2、git status
It can be used to check the status of the warehouse. In development, this command is probably the most used command. It is recommended to git status during the development process.
If you don't know the status of your git branch or repository, remember to git status, otherwise, problems may occur.
branch management
This is the highlight. In actual work, branches can be created to develop new functions, and there can be more branch switching. If the operation is not performed properly, it may cause a lot of trouble. I have encountered a lot of unnecessary troubles in my work. And it's very difficult to solve.
1. Create a branch git branch XXX
Branches can be created in the remote interface, or using commands git branch XXX
.
The code of the new branch created generally comes from the master, so, for example, if you create a new branch test, the code of the test branch is the same as the code of the master.
We can also use the git branch
view branch.
2. Switch branch git checkout XXX
Switch branch: git checkout XXX
, which switches to the XXX branch. Then we go to the XXX branch to develop the function.
3. Create a branch and switch branches git checkout -b XXX
Command: git checkout -b XXX
, this command executes the previous two branches, git branch XXX and git checkout XXX, creates and directly switches to the XXX branch. The advantage of this command is that when you need to develop new functions, you can directly Create a new branch, then switch directly, and you can start it directly.
In fact, we will encounter many technologies in the process of development. If you are interested, you can take a look at my blog blog.ouyangsihai.cn , which is full of my original articles and technical dry goods.
4. Check which branch git branch is in
Command: git branch
, you can directly view all local branches and which branch you are currently in.
If you ask, which command do I use to view all branches locally and remotely?
5. View all local and remote branches git branch -a
git branch -a
,Is not it simple.
6. Merge branch git merge
There are so many branches locally. What should we do when we complete feature development and need to merge into master?
- Switch to the master branch, git checkout master
- Merge XXX branch, git merge XXX
- At this time, if there is a conflict, it needs to be resolved.
7. Delete the local branch git branch -D XXX
When we are done with feature development and merged into master, we can delete our current branch, command git branch -D XXX
.
Note: Currently in XXX branch, XXX branch cannot be deleted, you need to switch to other branches first.
8. Delete the remote branch git push origin --delete XXX
Deleting a remote branch is a dangerous operation and can cause major problems if the permissions are unreasonable.
Suggestion: git branch -a View all branches, and then operate.
Update management
1. Submit the code to the remote git push origin XXX
The local code is written and submitted to the remote. The most commonly used operation, XXX is the name of the remote warehouse. Most commonly used: git push origin master
, added to master.
2. Pull the remote code to the local git pull origin XXX
Pulling down the remote code to the local and merging it is equivalent to executing the two steps of fetch and merge together. However, this is actually the most commonly used command. Generally, when pulling new code, this command is used directly.
In addition, this article GitHub https://github.com/OUYANGSIHAI/JavaInterview has been included, this is the Java interview summary of the first-line big factory that I spent 3 months summarizing, and I have taken the offer from the big factory.
version rollback
In fact, in the usual development, there are still some improper operations, which lead to problems in the branch. At this time, the role of version management is highlighted. We can perform version rollback operations through the version management provided by git, which can quickly Solve our problems.
Scenario: When we develop for a period of time, we find that there is a big problem with the master branch. We may need to roll back to a more suitable code version, and then carry out related development work.
1、 git reset --hard XXX
You need to pay attention to the use of this command. It will roll back all the code of the current branch to a previous version, which is irreversible and needs to be used with caution.
Although this command is not commonly used, it can play a great role when there is a big problem, and it can directly fall back to a previous version.
Of course, sometimes when we roll back by mistake, what should we do if we want to go back to the original way? Can't we go back to 18 in the code?
of course can. We can use git reflog
to view all head records.
Finally, we are passing git reset --hard 766f905f
, back to the previous version of the fallback.
Ok, the above is the most commonly used git command at work.
Give me a like, a recent series of original articles.