Git
definition
Currently the world's most advanced distributed version control system 1 designed for processing text files , which was written in C by Linus in 2 weeks and first developed on Linux.
Git tracks and manages 修改
files, not files.
shortcoming
1. The "Undo" button is limited by its limitations. The most obvious thing is that when the file is closed, the past of the file is also lost.
2. File saving is very personal. It cannot display the history of the entire system, only the file's.
Other Version Control Systems
- Centralized type:
-
Open source free: SVN, CVS, VSS
Charge: ClearCase - distributed:
- Git、Mercurial、Bazaar
what is version control
- 1. What does version control record?
-
All version control systems, in fact, can only track
文本文件
the details of changes (such as TXT files, web pages, all program code, etc., such as adding a word "Linux" on line 5, deleting a word "Windows" on line 8 ”.) And pictures, videos, and Word二进制文件
, although they can also be managed by the version control system, they cannot track the specific changes of the files. They can only string together each change of the binary file, that is, only know that the picture has changed from 100KB to 120KB, but what has been changed, the version control system does not know and has no way of knowing. - 2. Encoding of text files
-
Because the text is encoded, for example, Chinese has the commonly used GBK encoding, and Japanese has the Shift_JIS encoding. If there are no historical problems, it is strongly recommended to use the standard UTF-8 encoding. All languages use the same encoding, which has no conflict and is used by all supported by the platform.
Special attention should be paid to children's shoes using Windows:
do not use the Notepad that comes with Windows to edit any text files. The reason is that the team that developed Notepad at Microsoft used a very mentally retarded behavior to save UTF-8 encoded files, they cleverly added the character 0xefbbbf (hex) at the beginning of each file, and you'd come across a lot of weirdness For example, the first line of the web page may display a "?", a program that is obviously correct will report a syntax error as soon as it is compiled, etc., all of which are caused by the mentally retarded behavior of Notepad. It is recommended that you download Notepad++ instead of Notepad, which is not only powerful, but also free! Remember to set the default encoding of Notepad++ to UTF-8 without BOM.
supporting agreement
Git supports a variety of protocols, including https
, but the ssh支持的原生git协议
fastest through.
Workspace and Staging Area
- Workspace (Working Directory)
-
It is the directory containing .git that you can see on your computer, and
Git can perceive the dynamics of the workspace. But not automatically recorded. - Repository
-
The hidden directory .git of the workspace, which is not considered a workspace, is used to record the changes we submitted.
There are a lot of things stored in the repository, including暂存区(stage)
the first branch that Git automatically created for usmaster
, and a pointer to masterHead
.
When adding files to the Git repository, it is done in two steps: 1.
Use . Add to the staging area 2. Use to submit all the modification records in the staging area (not the work area) to the current branch at one time, and automatically clear the staging area.git add
文件的修改
git commit
pointer
- head pointer
-
Always point to a version in the current branch
head^ previous version, head^^ previous two versions...
head~n previous n versions - master pointer
- points to a version of the master branch
- branch name
- pointer to branch
GitHub
definition
GitHub is a software source code hosting service for version control through Git, written by the GitHub company using Ruby on Rails.
Features (in terms of code hosting)
-
1. Remote repository
备份
, which can be accessed by any computer. - Github takes the commit history in Git and stores it on the Internet for any access. After local 1 pushes commits to Github, those commits can be pulled from another new or different computer. Once uploaded to Github, these commits are stored in the remote repository.
-
2、
协同工作
-
1. Extract your responsible part from the main branch, create a local branch
2. Edit and modify the local branch, and then submit (Commit)
3. Push the commit (Push) to Github
4. Create a pull request (Pull Request), explain What changes does the
branch containFeatures
1. Free situation: read-only to others (others can fork arbitrarily)
Charge situation: can be invisible to others.
2. You can pull request to the official repository to contribute code
Git commands
version ( 修改
) management
record version
- git stage (=git add)
-
文件的修改
Add the work area to the staging area The
same file is staged multiple times in succession (without commit), and the later ones will overwrite the previous ones. - git commit -m <file description>
- Commit all the changes in the staging area (not the work area) to the current branch at one time, and automatically clear the staging area.
View version
- git status
- Check whether there are file changes in the workspace and whether there are any changes in the cache that need to be committed to the current branch.
- git diff
- See how the workspace file differs from the version recorded in the cache or current branch.
- git log [–pretty=oneline]
-
See commits before the current version
日志
(ie文件说明
) 2 and version number 3
undo version
- git checkout --<filename>
- Discard the modification of the current workspace and fall back to the staging area state (priority) or the state of the current branch record
- git reset HEAD <filename>
- clear staging area
- git reset –hard < commit id>
- Go back to a version in a specified branch
Delete Files
- rm
- Delete file from workspace (can be recovered with checkout)
- git rm <filename>
- When the file has been deleted in the workspace, delete the current version of the file from the branch (the current version is not recoverable, other versions can be recovered)
- git rm -f <filename>
- Remove the current version of the file from the branch when the file is not removed in the workspace
Temporary duty
- git stash
- Store the current work site, that is, save the content in the stage, and then clear the stage to provide a stage for temporary tasks.
- git stash list
- View content in stash
- git stash apply stash@{number}
- restore content from a specific stash
- git stash drop
- delete content in stash
git stash pop
: delete the restored stash while restoring the content in the stash
remote warehouse
The default name of the remote repository isorigin
Create SSH Key
1. Create an SSH Key locally
2. Create an SSH Key on GitHub and let GitHub get your public key
- reason
- The transfer between the local Git repository and the GitHub repository is encrypted over SSH. As long as GitHub knows your SHH public key, it can confirm that it was pushed by your computer, so as to receive your push, others cannot.
GitHub allows you to add multiple keys (corresponding to multiple computers)
Local warehouse -> remote warehouse (push)
- git remote add origin git@server-name:path/repo-name.git
- Associate a remote repository
Remote repository -> local repository (clone)
- git clone git@servername:path/repo-name.git
- Only from the clone warehouse under your own account, you can push the changes (because the corresponding warehouse is pushed to the clone).
Remote and local branches
The name of the remote branch should be the same as that of the local branch
- git push -u origin master
- Push everything from the master branch for the first time
- git push origin master
- After the local commit, push the latest changes
- git push origin <branch name>
- push local dev branch to remote
- git branch --set-upstream <branch name> origin/<branch name>
- Establish a connection relationship between the local branch and the remote branch
- merge conflict
-
After the local branch b2 is pulled from the remote branch b1, both the remote branch b1 and the local branch b2 have made changes to some files in the branch, such as file a, so that now the remote branch b1 is no longer the b1 when you pulled it down ( It has been changed by others), then it
相当于merge
will happen when b1 and b2 are pushed ( )远程合并冲突
.
At this point, the file in the remote branch b1 needs to be pulled down (相当于merge
), but after the pull down, it will happen due to the above reasons本地合并冲突
.
At this point, resolve the conflict and push again.
View remote library information
- git remote
- git remote -v
- Display the origin address of the grab and push, if you don't have push permission, you can't see the push address
branch management
View branch
- git branch
- See what branches are
- git log –graph
- View the branch merge graph
create branch
git branch <filename>
switch branch
git checkout <filename>
Create + switch branches
git checkout -b <filename>
Merge a branch into the current branch
- git merge <branch name>
- fast forward (fast forward mode), no branch merge instructions
- git merge --no-ff -m "merge description" <branch name>
- Disable fast forward (fast forward mode), you can attach branch merge instructions, and can see the instructions when viewing the branch merge diagram.
- merge conflict
-
Both branch b1 and branch b2 have modified a certain version of file a at the same time, and submitted the corresponding version. When branch b1 merges branch b2, a conflict is prompted. At this time, the difference information of the two branches is attached to the a file in the branch b1, and the a file in the branch b2 remains unchanged.
Solution: Modify the a file in the branch b1 (that is, delete the additional difference information in the a file or make other necessary changes) (of course, you can not modify it, then there will be corresponding attachment information in a). Then the current version of the a file in the b1 branch is committed again. Finally, merge the b1 branch and the b2 branch again. There will be no conflict prompt at this time, and the a file in b2 remains unchanged before the merge.
delete branch
git branch -d <filename>
bug branch
On the branch that needs to debug bugs, create a temporary branch to debug the bug.
feature branch
When new features need to be created, it is best to create a new branch.
If you want to discard a branch that has not been merged, you can
forcefully delete it by git branch -D <branch name>
Tag management
Label: A pointer to a certain version, which cannot be changed (branch can be changed), is an easy-to-remember name (while the commit number (ie, version number) is not easy to remember)
- git tag <tag name>
- Tag the latest version in the forward branch
- git tag <tag name> <version id>
- tag a version
- git day
- View all tags, in alphabetical order
- git show <tagname>
- View Tag Details
- git tag -a <tag name> -m "tag description" <version id>
- Write a description for the label
- git tag -s <tag name> -m "tag description" <version id>
- Sign tags with PGP
- git tag -d <tag name>
- remove tag
- git push origin :refs/tags/<tag name>
- Deletes a remote tag, provided that the tag has been deleted locally.
- git push origin <tagname>
-
Push a tag to the remote.
The created tags are only stored locally and will not be automatically pushed to the remote - git push origin –tags
- Push all unpushed local tags
[1] https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001373962845513aefd77a99f4145f0a2c7a7ca057e7570000
[2] http://blog.jobbole.com/111187/
[3] https://zh.wikipedia.org/wiki/GitHub
[4] https://www.zhihu.com/question/19946553
[5] https://blog.csdn.net/qq_22337877/article/details/73249912