Git common command usage records and personal understanding

Keywords: git

catalogue

  (no js authority, damn it, I remember this account qaq)

brief introduction

I recently started to contact git team development projects, because I am not familiar with git, so I make this essay to prevent forgetting.

If there is any mistake or mistake, please advise.

Record a teaching game website using git https://learngitbranching.js.org/?locale=zh_CN

This paper is divided into two parts. The upper part is the set of common commands, and the lower part is the complete steps of pulling and pushing code

 

Common commands

Initialize local warehouse

git init 

 

Add / disassociate remote warehouse

git remote add origin +ssh route 
git remote remove origin

 

View the path of the current connection

git remote -v 

 

New local branch

git checkout -b <Branch name> 
git checkout -b dev It is generally used to create a branch. If a branch exists, it will be switched 

 

New remote branch & push code

git push origin <Own local branch name>:<Own remote branch name> 
git push origin dev:dev

 

Clone code

git clone -b <Specify branch name> <Remote warehouse address>
git clone -b dev https://gitee.com/xxx.git
 If there is no local call dev A new branch will be created

 

Find submitted version information

git log 

 

Force backtracking version

git reset --hard 

About origin

Let's mention origin, which is the alias of the remote warehouse link

For detailed understanding, you can go to - > https://www.zhihu.com/question/27712995/answer/39946123

Pull code

git pull origin <Remote branch name>:<Local branch name>
git pull origin dev:dev
//When pulling other people's codes, it is recommended to create a new branch with the same name as the remote branch locally. When pulling codes, switch to the corresponding branch, which will not affect your own branch
​
git push -u origin dev:dev
//about-u Role, use git push -u origin dev:dev After, git Your local branch dev Make a link with the remote warehouse branch and want to do it next time push You can use it directly git push Instead of following parameters, it can be used to simplify push commands
​
git push -u origin dev:dev -f //Force submission to remote branch (not recommended, not recommended, not recommended, it is easy to have strange problems)

 

Merge code

git merge <Merged branch name>   //Remember to switch to your own branch for merging operation first, and then merge the merged branch

 

  About branch operations

 

git branch            //Lists the branches that already exist locally, and precedes the current branch with"*"sign  
git branch -r      //View remote version library branch list remote git branch -a      //View a list of all branches, both local and remote git branch -d dev    //delete dev branch

 

Switch branch

git checkout dev  

 


General process for pushing code

The first is to add all files to the staging area

git add .  (Notice the space and then .)

 

Then submit to the local warehouse

git commit -m""Remarks"

 

If your code version is the team's latest code, you can skip the merge code step

Switch branch

git checkout dev
dev: This generally refers to the main branch. Normally, the main branch code is the latest version,You can dev Change to the latest branch name in your team

 

Pull the latest code from the local dev branch

git pull <Remote host name> <Remote branch name>:<Local branch name>

git pull origin dev:dev

 

There are also ways to use fetch. The differences between git fetch and git pull will be described in detail below

Switch to your own branch

git checkout <Own branch name>

 

Merge code

git merge <Merged branch name>

 

Finally, push the local code to the remote branch

git push origin <Own local branch name>:<Own remote branch name>

 

 

General flow of pull codes

For example, at present, there are local branches a and b, and the local branch a wants to pull the remote branch b code

First switch to branch b

git checkout b

 

Then pull the code

git pull origin b:b  //The first is a distant branch and the second is a local branch

 

Switch to your own a branch

git checkout a

 

Merge b branch codes

git merge b

 

The advantage of this is that if there is a problem with the pull, it will not affect your own code (although sometimes you can't go back to your own branch without resolving the conflict)

 

The difference between git fetch and git pull

After git pull, it will be merged directly, which is prone to problems

If git fetch is down, the new code will not be merged or displayed (saved in "to be supplemented"), you can compare it first and then decide whether to merge

I'll talk about the specific advantages and disadvantages in a few years (November 28, 2021)

 

How to merge code after using git fetch

Method 1

git fetch origin <Branch name>             //Download from remote branch to local and create a new branch
git log -p <Local branch name>.. origin/<Remote branch name> //Compare the difference between a local repository and a remote reference ​ 

git merge origin
/<Branch name>           //Merge the remote downloaded code into the local warehouse, and merge the remote and local code

 

 

Method 2

git fetch origin <Remote branch name>:<New local branch name>  //Download from remote branch to local and create a new branch
​
git diff <New branch>                      //Compare the difference between the current branch and the new branch. Before this step, you should first switch back to the branch you want to merge
​
git merge <New branch>                     //Merge new branch to current branch
​
git branch -d <New branch>                 //Delete new branch

 

 

Method 3

git rebase (in doubt, to be supplemented / 2021 / 11 / 28)

 

 

Posted by driver on Sun, 28 Nov 2021 16:28:41 -0800