preface
This article describes only the common instructions used at work.
To learn how to operate Git systematically, please go to Pro Git Simplified Chinese Version
1. Pull code from scratch
Pull code over HTTP:
git clone http://xxxxxxx/xx.git
2. Configure git global information
Save personally identifiable information globally and avoid re-entering your account and password
Git config --global user.name "Mr. Xx "// Save your personal pull and commit account passwords and avoid typing git config --global credential.helper store twiceCopy the code
PS: The global configuration file is available in theC:\Users\ user name \.gitconfig
See the information below
3. Submit the code after modification
Git add. // Generate a commit node and add remarks git commit -m "remarks" // pull code git push codeCopy the code
Note that “commit” is used before “pull” is used. Otherwise, you may be forced into edit mode
If you accidentally pull code and force it into edit mode, don’t panic!
Just exit edit mode:
- Press esc key (exit edit mode)
- Press two capital Z’s (you can press CapsLock, then two ZZ’s or just
shfit + zz
)
Another way to exit edit mode:
- First,
esc
And thenshift + :
, the cursor is in the bottom column - The input
q
. (if the inputwq
Save the modification.)
4. Pull the specified remote branch
After git clone, only the master branch is available locally, so you need to pull the remote dev branch
Git checkout -b dev origin/dev git checkout -b dev origin/devCopy the code
Remote branch association: With git pull, git push, you do not need to specify a remote branch on the command line.
5. View, Create, switch, merge, delete branches
See the branch
Git branch // View local branches by defaultCopy the code
Switch branch
Git checkout dev // switch to the (already locally existing) dev branchCopy the code
Create and switch branches
Git checkout -b dev // create a new branch and switch to the dev branch at the same timeCopy the code
New branch (rarely used)
Git branch dev // Create a branch named dev locallyCopy the code
Merging branches
Git merge dev // The merge is normal. Git merge --no-ff --squash dev // Git merge --squash dev // There is only one more COMMIT record on the primary branchCopy the code
Delete the branch
Git branch -d test git push origin :testCopy the code
PS: If you want to delete the test branch, you need to do it on a branch other than test
6. Copy a COMMIT change to this branch
Query the ID of the commit node
Git log --pretty=oneline --abbrev-commitCopy the code
Copy a COMMIT change to this branch
Git cherry-pick commit-id // select a commitCopy the code
PS: Generally speaking, commit-id does not require all replication. Copy more than 6-8 bits
Application Scenarios:
- Changes are made on the error temporary branch (Test) and commit. At this point, you need to synchronize this change to the branch you want to modify (dev).
Solution:
Git checkout dev // git cherry-pick XXXX // commit to the dev branch git push //Copy the code
7. The version is rolled back
Git reset --hard XXXX // Rollback to the specified version, XXXX is the unique identifier of the commit record. Git reset --hard HEAD^ // back to the previous commitCopy the code
Typically used to start a new branch based on a historical COMMIT node.
PS: Roll back the version, modify it, and push it. There is no way to submit it directly! Unless plus-f
8, label
View local labels
git tag
Copy the code
tagging
Git tag -a v2.0.0 -m git tag -a v2.0.0 -m Git tag v3.0.0 XXXX git tag v3.0.0 XXXXCopy the code
Push the label to remote
Git push Origin -- Tags // Submit all local tags to remote git push Origin v3.0.0 // Push the specified tag (v3.0.0) to remoteCopy the code
Remove the label
Git tag -d v3.0 git push origin :refs/tags/v3.0.0Copy the code