Article directory
Review the use of git
Recently, the company's old projects have to be transferred from svn to git. Most of the git commands are forgotten. Here is a review and summary.
Base
View local git version
git --version
View local git configuration information
git config --list
Set configuration information
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Submit documents
If there is no operation such as branch merging, the approximate steps to submit a file using git:
1. Check status
git status
2. Add the modification to the temporary storage area
Add the modification that needs to be submitted. git add .
It is all added, this is not recommended to use directly
git add 文件目录 / git add .
3. Submit to the local warehouse
git commit -m "描述文本"
4. Update the local warehouse code to the latest
git pull
5. Push the local modification to the remote warehouse
git push
Note: Synchronize the local warehouse before submitting; if there is a conflict after the update, modify the conflict first.
Check
Check out the last few submissions
git log
git reflog
git log --pretty=oneline
View the detailed submission history of a file
git log -p README.md
go back
git reset --hard commitId
Note that the local modification add and commit are both in the local warehouse before use, otherwise the local modification will be gone once you roll back and come back.
git revert
Special attention: If you are not familiar with it, try not to use git revert and reset. You must think about what you want to do before using it. Do you want to undo it? delete? Or just check back.
Example usage scenarios:
If you already have A -> B -> C and want to go back to B:
Method 1: reset to B, lose C:
A -> B
If C is submitted wrongly and needs to be deleted, you can use the reset command to roll back and then push to the remote.
Method 2: revert submits a reverse rollback and becomes B:
A -> B -> C -> B
C is still there, but the two Bs are duplicates
If C is a modification, and now you want to change it back, and you may change it to C in the future, then you should revert
If you accidentally submit the password or something to the remote, you need to delete the modification. If this modification is the first one, just reset it and push it again. If it is not the first one, doing this will replace the post All changes are lost? It would be even better if there is a pointer directly to a certain commit id, and then modify it and submit it to the current commit. I don't know if there is such a function.
Undo changes
git checkout -- readme.txt
There are two cases, one is that it has not been added to the temporary storage area, and the local modification will be gone after canceling; the other is that if it has been added to the temporary storage area, if it is undone, it will return to the state after being added to the temporary storage area.
Note: "--" here, if you don't add "--", it becomes a command to switch branches.
or usegit restore
git restore readme.txt
The workspace has been modified, but it has not been added to the temporary storage area. If you want to undo the modification in the workspace, you can use git restore.
If the modification of the work area has been added to the temporary storage area, the modification of both the temporary storage area and the work area needs to be undone
git restore --staged readme.txt
git restore readme.txt
restore the workspace from the staging area,
git restore --worktree readme.txt
Restoring the staging area from the master
git restore --staged readme.txt
the branch
create
git branch dev
//Create a dev branch
git checkout dev
//Switch to dev branch
git push origin dev
//Push local dev branch to remote
or
git checkout -b dev
//Create a dev branch and switch to the dev branch
git push origin dev
//Push the local dev branch to the remote
git checkout -b dev
It is git branch dev
equivalent git checkout dev
to pushing to the remote, and the remote will have a branch of origin/dev.
If the remote already has a dev branch, create a local dev branch based on the remote dev branch:
git checkout -b dev origin/dev
git branch -d dev
// delete local branch
git branch -D
// force delete branch
to switch
git checkout 分支名 / git switch 分支名
It is recommended to use the switch command, which is the latest command
tag
git tag v1.0
hit local tag
git push origin v1.0
Push the local tag to the remote
git push origin --tags
Push all local tags to remote
git tag -d v1.0
Delete the local tag, and delete the remote after deletion, you also need to push origin
other commands
Display the current directory
pwd + enter
Create file/folder
touch readme.txt
view file content
cat filename
Understanding of the HEAD pointer
The HEAD pointer points to the current version branch, so switching between different versions actually changes the pointing of the HEAD pointer, so switching between different versions will be very fast.