git structure
1. Initialize the warehouse and generate the .git folder
git init
2. Add to the temporary storage area
git add test1.txt
-A can submit all changes at once
3. Add to local warehouse
git commit –m “第一次提交”
-m is followed by a description
4. View the current status of the warehouse
git status
5. View the before and after comparison of the modified file
git diff test1.txt
6. View operation records
git log
* Check the short version number and output a line
git log --oneline --pretty
* View branch diagram
git log --graph
7. Rollback version
(1) Go back once
$ git reset --hard HEAD^
(2) Roll back twice
$ git reset --hard HEAD^^
Many times
Of course, it is easier to write 100 ^ for the previous 100 versions, so it is written as HEAD~100,
(3) Roll back to a certain version according to the version number
git reset --hard a0016a25 // 可以不用输全
8. How to cancel the rollback
(1) The command line window has not been closed
After the rollback, if you know the version number id submitted before, you can go back to before the rollback, and the version number id does not need to be written in full
(2) The command line window has been closed
git reflog 记录你的每一次命令
9. View the difference between the latest version in the workspace and the repository
Git tracking manages modifications rather than files.
Git commit submission will only submit files in the temporary storage area. Files in the temporary storage area that have not been submitted will not be saved to the local warehouse.
View the difference between the latest version in the workspace and the repository Command:
git diff HEAD -- test1.txt
10. Undo modification
(1) If the modification has not been submitted to the temporary storage area
git checkout -- test1.txt
(2) If the modification has been submitted to the temporary storage area
First undo the modification of the temporary storage area
git reset HEAD test1.txt
Use checkout again
git checkout -- test1.txt
11. Delete
The rm command is to delete a file in the workspace.
git rm is to delete a file, and submit the modification of the deleted file to the temporary storage area
. It is equivalent to git add after deleting the file, and then save the modification.
able to pass
git checkout -- test1.txt
recover
12*, connect to the remote warehouse
Similar to gitee github encrypted by ssh during transmission
Step 1: Create an SSH Key.
In the user's home directory, check if there is a .ssh directory. If so, check if there are two files id_rsa and id_rsa.pub in this directory. If you already have them, you can skip to the next step. If not, open the Shell (open Git Bash under Windows) and
create an SSH Key:
$ ssh-keygen -t rsa -C "[email protected]"
You need to replace the email address with your own email address, and then press Enter all the way to use the default value. Since this Key is not used for military purposes, there is no need to set a password.
If all goes well, you can find the .ssh directory in the user's home directory, and there are two files id_rsa and id_rsa.pub in it. These two are the secret key pair of SSH Key. id_rsa is the private key and cannot be leaked out. id_rsa.pub It is the public key, you can tell anyone with confidence .
Step 2 Take gitee as an example, just paste the public key
The reason why GitHub needs SSH Key
is because GitHub needs to identify that the commit you pushed was indeed pushed by you, not someone else's impersonation, and Git supports the SSH protocol, so as long as GitHub knows your public key, you can confirm that only you can push.
Of course, GitHub allows you to add multiple keys. Suppose you have several computers, and you submit in the company for a while, and submit at home for a while, as long as you add the key of each computer to GitHub, you can push to GitHub on each computer.
13*. Associate the remote warehouse with the local warehouse (associate with the remote warehouse when there is a local warehouse)
establish association
(1) Establish the association of the remote warehouse
git remote add origin 仓库ssh地址
(2) Content push
git push -u origin master
Since the remote library is empty, when we push the master branch for the first time, we add the -u parameter. Git will not only push the content of the local master branch to the remote new master branch, but also push the local master branch and the remote The master branch is associated, and the command can be simplified and used directly when pushing or pulling in the future
git push origin master
Delete remote library
View remote library information:
git remove -v
Then delete by name
git remote rm origin 仓库名
14*, Clone from a remote repository (creating a repository remotely, cloning directly, most of them do this)
git clone <http或者ssh地址>
15. Branch management
(1) Create branch dev
git checkout -b dev
equivalent to
git branch dev // 创建
git checkout dev // 切换
commit remote branch
git push -u origin master
git push --set-upstream origin <branch>
(2) View branch
git branch
(3) Merge the branch to merge the dev modification into the master
After switching to master
git merge dev
When merging branches, Git will use Fast forward mode if possible, but in this mode, after deleting the branch, the branch information will be lost.
If you want to forcibly disable the Fast forward mode, Git will generate a new commit when merging, so that the branch information can be seen from the branch history.
Ways to disable express commit
git merge --no-ff feature
**
(4) Delete branch dev
delete local branch
git branch -d dev
delete remote branch
git push origin --delete <branch> -d -D
The switch method switches branches.
We noticed that switching branches use the git checkout branch name, while the undo modification mentioned above is git checkout – file name. The same command has two functions, which is indeed a bit confusing.
In fact, it is more scientific to use switch for the action of switching branches. Therefore, the latest version of Git provides a new git switch command to switch branches:
to create and switch to a new dev branch, you can use:
git switch -c dev
To switch directly to the existing master branch, you can use:
git switch master
16. Push push, pull pull (fetch + merge)
git pull origin 分支名
git push origin 分支名
17. The reason why .gitignore fails
The directory or file is added to the ignore rule. After defining it according to the above method, it is found that it does not take effect. The reason is that . of.
solve:
(1) Clear the cache
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
git push -u origin master
(2) Delete the corresponding files in the remote warehouse
18. gitignore syntax
(1) Rules
空格不匹配任意文件,可作为分隔符,可用反斜杠转义
开头的文件标识注释,可以使用反斜杠进行转义
! 开头的模式标识否定,该文件将会再次被包含,如果排除了该文件的父级目录,则使用 ! 也不会再次被包含。可以使用反斜杠进行转义
/ 结束的模式只匹配文件夹以及在该文件夹路径下的内容,但是不匹配该文件
/ 开始的模式匹配项目跟目录
如果一个模式不包含斜杠,则它匹配相对于当前 .gitignore 文件路径的内容,如果该模式不在 .gitignore 文件中,则相对于项目根目录
** 匹配多级目录,可在开始,中间,结束
? 通用匹配单个字符
* 通用匹配零个或多个字符
[] 通用匹配单个字符列表
(2) Example:
bin/: 忽略当前路径下的bin文件夹,该文件夹下的所有内容都会被忽略,不忽略 bin 文件
/bin: 忽略根目录下的bin文件
/*.c: 忽略 cat.c,不忽略 build/cat.c
debug/*.obj: 忽略 debug/io.obj,不忽略 debug/common/io.obj 和 tools/debug/io.obj
**/foo: 忽略/foo, a/foo, a/b/foo等
a/**/b: 忽略a/b, a/x/b, a/x/y/b等
!/bin/run.sh: 不忽略 bin 目录下的 run.sh 文件
*.log: 忽略所有 .log 文件
config.php: 忽略当前路径的 config.php 文件
19. Steps to resolve conflicts
git reset
20. Application of git stash
When developing a requirement a by myself, the development has not been completed, and there is a more urgent requirement b that has a higher priority than a. Now because the existing requirement has not been developed, so if you don’t want to commit a version, you can use the git stash command to cache and remove it Modified code.
(1)git stash
git stash // 直接缓存,缓存名称为最新一次commit的内容,如果没有本地提交则是拉远程仓库是的commit内容
git stash save "xxx" // 加上自己的注释进行缓存
stash will only operate on files tracked by git
As shown in the picture:
a. First check the file status
b. Now execute the git stash temporary storage file git stash list to view the cached list
c. Check the file status again and find that the newly added file has not been cached and deleted
Conclusion: The newly added file after stash does not enter the cache. This is because git has not tracked the newly added file. You need to perform git add <file name> to let git track this file, and then perform stash to operate on the new file .
(2)git stash list
return cached list
(3) git stash pop
Pop the latest content in the stack and apply it to the current branch, and delete the records in the heap.
Pop deletes the cache in the stack
. If the popped content conflicts, git will interrupt the pop and inform you that you need to conflict Solution
You can also specify the records in the stack by adding the name in git stash list after git stash pop
git stash pop stash@{0}
(4) git stash apply
Similar to pop, but it will not delete this cache in the stack , suitable for caching applications in multiple branches,
and can also be specified
git stash apply stash@{0}
(5)git stash drop/git stash clear
delete cache
git stash drop [名] // 删除单个缓存 举例git stash drop stash@{0}
git stash clear // 全清
(7)git stash show
git stash show [name] // show the difference with the current branch
git stash show stash
(8) git stash branch
Specify the latest cache to create a branch
21. Change the remote warehouse address of the current local warehouse
git remote set-url origin <远程仓库地址>