Recommend learning website learngitbranching.js.org/?locale=zh_…
1. Get and create the project
1.1 Git initializes the local repository
git init
Git config --global user.name "zhangsan" git config --global user.email [email protected] # Git config --list # git config --global user.name # Git config --global user.emailCopy the code
1.2 Copy a Git repository
git clone [url]
Git clone [url] git clone [url]Copy the code
2. Basic snapshots
2.1 Adding a File to the Cache
git add
Git status -s?? a.txt # ?? Git status -s # A a.txt = git status -s # A a.txt = git status -s # A a.txt = git status -s # A a.txt Git status -s AM a.txt # AM indicates that the file changed after we added it to the cacheCopy the code
Git Add is quick
Git add * # monitor index file git add -a # monitor index file git add -a # monitor index file git add -ACopy the code
2.2 Viewing the File Cache Status
git status
Git status -s = git status -sCopy the code
2.3 Displaying changes
Git diff [describes changes that have been temporarily committed or changed but not committed]
If there are no other parameters, Git diff displays all changes that have not been cached since the last snapshot was committed in the canonical diff format
Git diff warning: LF will be replaced by CRLF in a.txt. The file will have its original line endings in your working directory diff --git a/a.txt b/a.txt index 40f4dc6.. F678797 100644 -- -- a/a.txt +++ b/a.txt @@ -1,2 +1 @@ fdafds - +ttestCopy the code
Git Diff is quick
Git diff --git a/ a.tb/a.tb new file mode 100644 index 0000000.. 40f4dc6 -- /dev/null +++ b/a.txt @@ -0,0 +1,2 @@ +fdafds + --stat The file will have its original line endings in your working directory a.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Copy the code
2.4 Recording a snapshot of cached content
git commit
Store the cached contents
Git config --global user.name 'guo xiao ye' git config --global user.email '[email protected] [master (root-commit) 9a3f411] first test 1 file changed, Git status On branch master nothing to commit, working tree cleanCopy the code
Git Commit fast
Git commit -a -m 'message' git commit -a -m 'message'Copy the code
2.5 Canceling cached Content
git reset HEAD
Introduction: Perform git Reset HEAD to cancel the previous Git add addition, but do not want to include the cache in the next commit snapshot
Git status - # s not added to the staging area (the M in red) M a.t xt git add git status - s # has been added to the staging area (M at this time for the green) M a.t xt # cancelled temporary git reset HEAD a.t xt Git status -s # is not added to the registerCopy the code
2.6 Removing a File from the Buffer
git rm
Introduction: Remove entries from the buffer, delete files from the cache and your hard drive (working directory)
To keep this file in your working directory, use git rm –cached
3. Branch and merge
3.1 Creating and Switching branches
git branch [branchName]
Git branch dev *master *master = master/master = master/master = master/master Git checkout -b dev # checkout git branch -d div # checkout git branch -rCopy the code
3.2 Merge Branches
git merge
Introduction: Merge branches into your current branch
Git merge dev # 2 indicates the file version corresponding to the current user. 3 indicates the remote version of the merged file) git ls-files-s # Check the version content of the corresponding file git show :1:a. xt # Manually add commit push after the conflict is resolvedCopy the code
3.4 Viewing branch Change Records
git log
Overview: Display change logs
Quick Log Operation
Git log --oneline --graphCopy the code
3.5 Labelling history records
git tag
Adds a label record to the Update History node
Git tag -a 'tagname' -m 'tagname'Copy the code
4. Share and update projects
4.1 Associating a Remote Repository
git remote
Description: Lists the names of remote repositories
Git remote add [alias] [url] # git remote add [alias] [url] # git remote add [alias] git remote rm [alias]Copy the code
4.1 Pull code
git fetch
Introduction: Synchronizing remote warehouse to extract data that is not available locally
git pull
Git fetch + merge = merge branch
# git fetch [alias] to synchronize your repository with the remote repository, extracting all its unique data to the local branch for merging or whatever.Copy the code
Compare git fetch and Git pull
- git fetch
When you update the code using Git fetch, the commitID of the master in the local library is still equal to 1. The commit ID of git is changed to 2. If the commit ID of git is changed to 2, a new version of git will be generated with an ID = 3
The version number associated with the remote repository is updated. The following is the code to merge the two versions locally.
- git pull
Update the local code to the latest version of the code in the remote repository
4.3 Pushing Code to the Remote Repository
git push [alias] [branch]
Git push pushes your [branch] branches into [alias] remote [branch] branches
Git push github masterCopy the code
shortcuts
Git push -u origin master # 2. Git push --all Origin # 3: -all git push --all Origin # 3: -all git push --all Origin # 3 Modify Git push --force OriginCopy the code
5. Withdraw the submission record
5.1 Local modification Is not committed
Git checkout <fileName> git checkout a.txtCopy the code
5.2 Local Modifications Have been committed
Git reset --hard HEAD~1 git reset --hard HEAD~1 git reset --hard HEAD~1Copy the code
5.3 Local Modifications have been uploaded to the Remote Repository
Git Revert HEAD Git Revert HEAD git Revert HEADCopy the code
6. Git abbreviation command
shorthand | The command |
---|---|
g | git |
gst | git status |
gd | git diff |
gdc | git diff –cached |
gl | git pull |
gup | git pull –rebase |
gp | git push |
gc | git commit |
gco | git checkout |
gb | git branch |
glo | git log –oneline –decorate –color |
gss | ogit status -s |
ga | git add |
gm | git merge |
gp | git push |
7. Common instruction record
7.1 Git rebase multiple times
# git rebase < first branch > < second branch > Git rebase master bugFix git rebase bugFix side git rebase side anotherCopy the code
7.2 Git pull code
Git pull = git pull = git pull = git pull = git fetch = git merge = git pull = git pull Git fetch (update local remote branch code) + git rebase (rebase local remote branch code) = git pull --rebase git pull --rebaseCopy the code
parsing
Local branch test is created on git remote master branch [B], and records are generated on both D and E. At the same time, other colleagues have updated the code to the remote branch of the master, generating two node records of C and F. The remote code needs to be updated locally: 2. Git fetch + Git merge update code (git pull) D--------E / \ A - B - C - F - G - test, master 3. Git fetch + git rebase way to update the code (git pull - rebase) A - B - D - E - C '-' F - test, Master (special case) -- Update code collision 1. The merge operation cannot continue with a collision. After manually modifying the conflicting content, add the modification and commit it. 2. The rebase operation will interrupt the rebase and prompt you to resolve the conflict. Once the conflict is resolved, git rebase-continue is executed after modifying add, or Git Rebase-skip ignores the conflict.Copy the code
7.3 Switching Git Branches
#1. Switch branches and establish the specified remote branch associationGit checkout -b < branch name > #2. Set the remote tracking branchGit branch -u < branch name >Copy the code
8. Git commit specification
Commit format
:
Note the space after the colon
type
Used to describe the category of COMMIT, only the following identities are allowed
- New feature (feature)
- Fix: fix the bug
- Docs: documents (documentation)
- Style: format (changes that do not affect code running)
- Refactor: refactoring (that is, code changes that are not new functionality or bugs)
- Test: Adds tests
- Chore: Changes to the build process or ancillary tools
subject
A subject is a short description of the purpose of commit, no more than 50 characters long, and does not end with a period (.).
Chestnut 🌰
Git commit -m 'feat: add a user list page 'git commit -m 'fix: add a user list page' git commit -m 'refactor: add a user list page code refactorCopy the code