A brief record of Liao Xuefeng's Git tutorial commands

A brief record of Liao Xuefeng's Git tutorial commands

1. Create a version library

serial number Order effect importance***
1 makdir xxx Create a new xxx directory
2 cd xxx Enter the xxx directory
3 pwd Show current directory path
4 ls -ah Show files in current directory
5 vi xxx.txt Open the file xxx.txt for editing *
6 cat XXX.txt View file xxx.txt **
7 git init Set the current directory to the git management directory *
8 git add xxx.txt Add the file xxx.txt to the temporary library ***
9 git commit -m "file description" Submit the files added to the temporary repository to the repository and record the file description ***
10 git status View current warehouse status ***

2. Time machine travel

serial number Order effect importance***
1 git diff xxx.txt View the current modifications to the xxx.txt file ***
2 git log Show version logs from most recent to farthest ***
3 git log --pretty=oneline Display version logs by row from most recent to furthest **
4 go reflog Display all version commit or jump (commit, reset) commands ***
5 git reset --hard HEAD^ Return to previous version ***
6 git reset --hard version number Return the version corresponding to "version number" ***
7 git checkout – xxx.txt Returns the last add or commit version of file xxx.txt **
8 git reset HEAD xxx.txt Unstage the modifications in the temporary storage area of ​​xxx.txt and put them back into the workspace
9 rm xxx.txt Linux command to delete file xxx.txt
10 git rm xxx.txt Delete the xxx.txt file in the repository

Pay attention to the usage scenarios of the two commands 7 and 8:

Scenario 1): When you change the contents of a file in the workspace and want to directly discard the modifications in the workspace, use the command git checkout – file.

Scenario 2): When you not only change the contents of a file in the workspace, but also add it to the temporary storage area, and want to discard the changes, you need to do it in two steps. The first step is to use the command git reset HEAD <file_name> to return to the scene. 1. In the second step, operate according to scenario 1.

3. Remote warehouse

In order to manage files more securely, a remote warehouse is established for management. Due to the difficulty in connecting to the github remote library network, domestic gitee is used for remote warehouse management.

1. To connect to the remote library, first generate the files id_rsa and id_rsa.pub in the folder "...\username\.ssh", where id_rsa.pub is the SSH public key. input the command:

ssh-keygen -t rsa -C "<邮箱[email protected]>"

Note that if an SSH file has been generated before, you will be asked whether to overwrite the previous file. After entering y and pressing Enter, you will be asked to enter a password (if you do not enter the password, the password will not be set). Just enter it twice and press Enter.

2. Open the id_rsa.pub file, copy its contents to the "gitee personal homepage->Personal Settings->Security Settings->SSH Public Key" public key box, enter a name for the public key in the title bar, and then click Confirm. Can.

3. Then create a new warehouse in gitee. The warehouse name should be consistent with the local folder name.

4. Associate the local library with the gitee remote library. input the command:

git remote add origin [email protected]:liaoxuefeng/learngit.git

Note that "liaoxuefeng/learngit" is changed to your own gitee ID and warehouse name.

5. After the local file is committed, push it to the gitee remote library and enter the command:

git push -u origin master

6. View the remote library and enter the command:

git remote -v

Summary of remote warehouse commands


serial number Order effect importance***
1 ssh-keygen -t rsa -C "<email [email protected]> Generate SSH public key and only need to do it once *
2 git remote add origin [email protected]:liaoxuefeng/learngit.git Associate the local library with the gitee remote library. The local library and the gitee remote library only need to be operated once after they are created. *
3 git push -u origin master Push the local master branch to the remote library. Use this command for the first push. ***
git push gitee master Push the local master branch to the remote library. This command can be used if it is not the first push.
4 git remote -v View remote library ***

3. Branch management

serial number Order effect importance***
1 git checkout -b <name> Create and switch to a new branch ***
git switch -c <name> Create and switch to a new branch, only new versions of git support it ***
2 git branch <name> Create a branch ***
3 git branch View current branch ***
4 git checkout master Switch to branch master ***
git switch master Switch to branch master, only new versions of git support it ***
5 git merge <name> Merge branch into current branch ***
6 git branch -d <brc_name> Delete a branch. The branch needs to be merged before deletion. ***
git branch -D <brc_name> Forcefully delete unmerged branches
7 git log --graph View the branch merge diagram of each version **
git log --graph --pretty=oneline A single line displays the branch merge diagram of each version ***
git log --graph --pretty=oneling --abbrev-commit A single line is reduced to display the branch merge diagram of each version. **
8 git merge --no-ff -m “<comment>” <branch_name> Merge branches in non-fast forward mode ***
9 git stash Hide work site ***
10 git stash list Show hidden site list ***
11 git stash apply Restore the hidden scene, but the hidden content will not be deleted ***
12 git stash drop Remove hidden content ***
13 git stash pop Restore hidden scenes and delete hidden content ***
14 git cherry-pick <commit> "Copy" the changes submitted by <commit> to the current branch **

Notice:

  1. You may encounter conflicts when merging branches in commands 5 and 8 (the same file has different contents in the two branches to be merged). The solution is to manually edit the conflict file and then submit it.
  • When Git cannot merge branches automatically, it must resolve conflicts first. After resolving the conflict, submit again and the merge is completed.

  • To resolve conflicts, we need to manually edit the files that failed to merge with Git to the content we want, and then submit them.

Guess you like

Origin blog.csdn.net/family5love/article/details/119146706