preface
Recently, looking through my youdao cloud notes, I found that I threw a lot of documents at random, all of them in each period of time, scattered like a garbage heap, so I decided to sort out my notes, and consolidate the knowledge point by the way, and check the leaks and fill in the gaps. Plan the front-end knowledge system in the second half of the year, read a few books have not sorted notes. ง ง, _, ()
git
The following is mainly from Pro Git
basis
Initializing the warehouse
1, empty project
git init
git remote add origin <url>
Copy the code
Or clone the remote repository directly
git clone <url>
Copy the code
2. A local repository exists
- No remote warehouse can be added directly
- There is a remote repository reference link to the remote repository
Check the current file status
You can use the git status command to see which files are in what state.
Git status git status -s // Clean modeCopy the code
Tracking new files
Start tracking a file with the git add command. So, to track the README file, run:
git add README
Copy the code
It is more appropriate to read this command as “add content precisely to the next commit” rather than “add a file to the project.”
Submit the updates
Git commit -m < commit description > git commit -a -m < commit description > // Fast commitCopy the code
Recommended format for submission:
- Add-feat: New feature submission (usually used for the first feature code submission)
- Add-style: ADD new code STYLE changes
- Mod-style: modify/tidy the code STYLE STYLE, does not involve logical changes
- Mod-feat: Modify function code (usually used after the first function code submission)
- BUGFIX: Bug fix for testing;
- HOTFIX: For online emergency bug fixes
- Del-style: Remove unwanted code, comments, etc. from the code STYLE
- Del-feat: Remove some useless function block code
Remove the file
Git rm < file name >Copy the code
- If you want to delete previously modified files or files that have been placed in the staging area, you must use the force delete option -f.
Git rm -f < file name >Copy the code
- Only the staging area is cleared without deleting files
Git rm --cached < file name >Copy the code
You can list the name of the file or directory after the git rm command, or you can use the glob mode. For example, delete all files whose extension name is.log from the log/ directory
git rm log/ \ *.log
Copy the code
View the submission record
git log
Copy the code
Git log can be followed by a variety of commands to filter records, more trouble is recommended to use visual tools to view
git log --pretty=format:"%h %s" --graph
Copy the code
Cancel the operation
Has been submitted
git commit -m 'initial commit'
git add forgotten_file
git commit --amend
Copy the code
The current commit replaces the previous commit
Have not been submitted
- Undo the file from the cache
Git reset HEADCopy the code
- Undo changes to file (reset to last commit)
Git checkout -- < file name >Copy the code
Remote warehouse
- Viewing the remote repository
git remote -v
Copy the code
-v displays the abbreviation of the Git store that you want to read and write to the remote repository and its corresponding URL.
- Adding a remote Repository
git remote add <shortname>
Copy the code
- Grab and pull from remote storage
git fetch <remote>
Copy the code
If you clone a repository using the Clone command, the command automatically adds it to the remote repository and defaults to “origin”. So, Git Fetch Origin will fetch all the work of the new push since the clone (or the last fetch). It is important to note that the Git FETCH command only downloads data to your local repository – it does not automatically merge or modify your current work. Git pull in most cases is a Git fetch followed by a Git merge command
- Push to the remote repository
Git push Origin master or Git pushCopy the code
- View a remote repository
git remote show <remote>
Copy the code
- Renaming and removing remote repositories
git remote rename <old> <new>
Copy the code
It will also change the name of all your remote trace branches
git remote remove <remote>
Copy the code
The label
Check the label
git tag
git tag -l "V1.8.5 *"// Search by conditionCopy the code
Create a label
Git tag-a v1.4-m"my version 1.4"Git tag: git tag: git tag: git tag: git tag: git tag: git tag: git tag: git taghash< span style = "box-sizing: border-box; color: RGB (255, 255, 255)Copy the code
Push the label
By default, git push does not pass labels to the remote repository server. After creating the tag you must explicitly push the tag to the shared server. This process is similar to sharing remote branches — you can run git push Origin
to push all git push Origin –tags
Remove the label
git tag -d <tagname>
Copy the code
Git push
:refs/tags/
git push origin –delete
Git alias
Git doesn’t automatically infer what you want when you type some commands. If you don’t want to enter the full Git command every time, you can easily set up an alias for each command using a Git config file. Here are some examples you can try:
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status```
### .gitignoreGitignore node_modules // Ignore all files under node_modules. Temp. Cache distCopy the code
The format specification of.gitignore is as follows:
-
All lines that are blank or start with # are ignored by Git.
-
You can use the standard GLOb pattern matching, which is applied recursively throughout the workspace.
-
Matching patterns can start with (/) to prevent recursion.
-
A matching pattern can end with (/) to specify a directory.
-
To ignore files or directories outside the specified schema, precede the schema with an exclamation point (!). Invert.
The GLOb pattern refers to the simplified regular expression used by the shell. The asterisk (*) matches zero or more arbitrary characters; [ABC] matches any character listed in square brackets (this example matches either an A, a B, or a C); Question mark (?) Matches only one arbitrary character; If two characters are separated by a dash in square brackets, it means that all characters within the range of these two characters can be matched (for example, [0-9] means that all numbers from 0 to 9 are matched). Use two asterisks () to match any intermediate directory. For example, a//z can match a/z, a/b/z, or a/b/c/z.
# ignore all.a files *.a # but track all lib.a files, even if you ignored.a files in the first! Lib. a # ignore the TODO file in the current directory, not subdir/TODO /TODO # ignore any folder called build/ # ignore doc/notes.txt, # Ignore the.pdf files in the doc/ directory and all its subdirectories: doc/**/*.pdfCopy the code
Tip:
GitHub has a very detailed list of.gitignore files for dozens of projects and languages. You can check them out at github.com/github/giti… Find it.
branch
Git does not keep changes or differences in files, but rather a series of snapshots at different times.
Create a branch
git branch <branch>
Copy the code
Create a pointer to the current commit object with HEAD pointing to the current branch
The branch switch
Git checkout -b <branch> git checkout -b <branch> git checkout -b <branch Pull the specified branch from the remote Git repository to the local branch that does not exist locallyCopy the code
If the branch you’re trying to check out (a) doesn’t exist and (b) just happens to have a remote branch with a matching name, Git creates a trace branch for you
Merging branches
git merge <branch>
Copy the code
It’s just a simple pointer move when there’s no conflict
The commit in the Master branch is not a direct ancestor of the commit in the ISS53 branch, and Git has to do some extra work. When this happens, Git does a simple three-way merge using snapshots of the ends of the two branches (C4 and C5) and their common ancestor (C2)
In the case of a conflict, resolve the conflict and commit the changes
Delete the branch
Git branch -d <branch> git branch -d <branchCopy the code
View all branches
git branch
Copy the code
Git branch – no – merged | merged view has merged or unincorporated branch, behind may meet branch name to filter.
Prune remote branches
When some branch remote has been deleted and still exists locally
git remote prune origin
Copy the code
Push the branch
Git push <remote> <branch>:<other_ranch> // push the local branch to a remote branch with a different nameCopy the code
Tracking remote branch
- Modify the upstream branch being tracked
git branch -u origin/serverfix
Copy the code
Deleting a remote branch
git push origin --delete <branch>
Copy the code
rebase
git rebase master
Copy the code
Its principle is to first find the recent common ancestor C2 of the two branches (i.e., the target base branch master of the current branch experiment and the base branch master of the base change operation), then compare the previous commits of the current branch relative to the ancestor, extract the corresponding modifications and store them as temporary files. The current branch is then pointed to the target base C3, from which the changes previously saved as temporary files are applied sequentially. Commit ID = commit ID = commit ID
Rebase risk
If the commits exist outside of your repository and someone else might be developing based on them, do not base them.
The essence of a rebase operation is to throw away some existing commits and create new commits that are the same but actually different.
remedial
Undo the changes to the file
git checkout -- <file>
Copy the code
Git checkout. Discard all changes
The rollback
git logGit reset --hard version numberCopy the code
workflow
Centralized workflow
There is only one main branch, and members update, push, and resolve conflicts on the same branch
2. Function branch workflow
Pull Requests are key
Each function is developed on a separate branch, which makes a merge request and is then merged into the main branch.
3. Gitflow Workflow
The Gitflow workflow facilitates release iterations by assigning separate branches for feature development, release preparation, and maintenance. The strict branching model also provides some much-needed structure for large projects.
Branch composition:
- Master: the main branch (used for publishing)
- Develop: Feature integration branch (used to merge feature branches)
- Function branch
Release branch
The Develop branch cuts a branch out as the publish branch, and then merges it into the Master branch and marks it. As long as the branch is created and pushed to the central repository, the release is functionally frozen. Any new features not in the Develop branch are pushed to the next release cycle.)
The maintenance branch
Cut a fix branch from master and merge it with Master and Develop.
Refer to the link
Q&A
matches more than one
Dst refspec hr-portal-app_v2.0 matches more than one. Failed to push some refs
Cause: The tag name is the same as the branch name
Solution: Delete one of them
git push origin :refs/tags/dev_test
Copy the code
refusing to merge
refusing to merge unrelated histories
Local and repository are separate libraries
git pull origin master --allow-unrelated-histories
Copy the code
The related resources
Pro Git