Introduction and Installation
Git is an open source distributed version control system developed by Linus Torvalds to help manage Linux kernel development.
Git is different from SVN
Git is distributed, SVN is not: this is the core difference between Git and other non-distributed version control systems, such as SVN, CVS, etc.
Git stores content as metadata, whereas SVN stores content as files: all resource control systems hide meta information about files in folders like.svn,.cvs, etc.
Git branches are different from SVN branches: a branch is nothing special on SVN, just another directory in the repository.
Git does not have a global version number, whereas SVN does: this is by far the biggest feature Git lacks compared to SVN.
Git’s content integrity is superior to SVN’s: Git’s content storage uses the SHA-1 hash algorithm. This ensures the integrity of the code content and reduces damage to the repository in the event of disk failures and network problems.
Install on Windows platform
Download the installation package at gitforWindows.org/
Website is slow, you can use the domestic mirror: npm.taobao.org/mirrors/git…
Once installed, you are ready to use the command-line Git tool (which already comes with an SSH client), as well as a graphical Git project management tool.
Go to “Git” -> “Git Bash” in the Start menu. This will bring up a Git command window where you can perform Git operations.
Install on Mac platform
The installation package download address: sourceforge.net/projects/gi…
Work flow and principle
Git’s general operation flow is as follows:
There are four key points:
Workspace: the place where the project files are stored on the local computer. The code we usually develop and write is also carried out in this area.
Index: a place where files are temporarily stored. You can add files from a workspace to the Index by using the add command.
Git folder contains the master branch that git automatically creates, and the HEAD pointer points to the master branch. You can use the commit command to add files from the staging area to the local repository.
Remote repository (Remote): The project code is copied to the local repository using the Clone command on the Remote Git server. After development, the project code is pushed to the Remote repository by push.
SSH and HTTPS
The main differences between using SSH and HTTPS are as follows:
If you use HTTPS pull code, you need to enter the account password every time you pull and submit the code from the remote repository.
If SSH is used, pull and submit code from remote repository will compare local and platform upload SSH, if the same authentication pass, otherwise failed!
Create SSH on Windows
Type CMD to open the system command prompt.
Ssh-keygen -t rsa -c “[email protected]”,”[email protected]”. Your C:\Users\admin\. SSH path will generate id_rsa and id_rsa.pub.
Open the id_rsa.pub file with Notepad, copy the contents of the file, and add them to Github’s SSH key management page.
Common commands
Git commands are used as follows:
Configuration information
Querying Configuration Information
Git config –list;
Git config –local –list;
Git config –global –list;
Git config –system –list;
Setting configuration Information
Git config –global user.name “your name”;
Git config –global user.email “[email protected]”;
Other Configuration Information
Git config –global color. UI auto;
Git config –global core.editor vi;
Workspace operation
The new warehouse
Create a new local repository: git init;
submit
Commit all files in your workspace to staging: git add.
Git add
… ;
Commit all files in a folder in the workspace to staging: git add [dir];
undo
Git rm
… ;
Git rm –cached
… ;
Git reset HEAD
… ;
Git checkout —
; Git checkout –.
Update file
Git mv [file-original] [file-renamed]…
Git file names are case insensitive by default
Staging area operation
Commit to local repository
Git commit -m “commit-info”;
Commit the workspace and staging files to the local repository: git commit-am “commit-info”;
Overwrite the last commit and commit the staging files to the local repository: git commit — amend-m “commit-info”;
Local warehouse operation
Git pull
:
Git pull –rebase
:
;
Git fetch [remote-name];
Fetching all branch updates from remote repository: git fetch –all;
Git push [remote-name] [branch-name] git push [remote-name] Git push
:
;
Remote warehouse operation
Git clone
;
Git remote: displays the url of the remote repository associated with the local repository.
Git remote add [remote-name] [url];
Git remote show origin;
Git remote rm [remote-name];
Branch operation
Git branch
Git checkout
;
Git checkout -b
Git branch -d
Git branch -d
Git merge
Display all branches of the local repository: Git branch;
Git branch -r displays all branches of the remote repository: git branch -r;
Git branch -a displays all branches of local and remote repositories: git branch -a;
Check which branches are merged to the current branch: git branch –merged;
Git branch –no-merged;
Git merge
/
;
Git branch -m
diff
Git diff
; git diff
;
Compare staging files with local repositories: git diff –cached;
Git diff
–cached;
Other operating
Query the status of all files in the current workspace: git status;
Git log –oneline -3;
Git tag
Git tag;
Git tag -l “v1.4.2.*”;
Create a lightweight tag: Git tag v1.5;
Git tag -a v1.4.2 -m “commit-info”; git tag -a v1.4.2 -m “commit-info”
Run the git show command to check the version information of the corresponding tag and display the submitted object of the tag: git show v1.4.2;
Git push Origin v1.4.2;
Git push origin –tags;
Git Stash
Hide the current change so you can switch branches: git stash;
Look at all the current stash lists: git Stash list;
Apply the latest stash: git stash apply. If you want to apply another stash: git stash apply stash{0}; If you use the pop command, you can not only reapply the stash, but also immediately remove stash from the stack: git stash pop;
.gitignore
The.gitignore file no longer manages specified file changes.
Grammar specification
A blank line does not match any file, so it can be used as a separator for readability;
Lines starting with # are comments;
The slash/is used as a directory separator;
* Matches anything except the skew bar;
? Matches any character except the slanted bar;
** Two consecutive asterisks in a pattern that matches the full pathname have special meaning:
- leading
**
Followed by a slash to match in all directories; - Of the following
/ * *
Matches everything in it; - Oblique bar followed by two consecutive asterisks, followed by a slash to match zero or more directories;
- Other consecutive asterisks are treated as regular asterisks and will be matched according to the previous rules;
! Indicates that matched files or directories are not ignored.
Example configuration file
#Ignore any files named A in all directories
a.*
#Ignore all files whose suffix is TXT in the directory
*.txt
#Do not ignore c.txt files in all directories! c.txt
#Ignore all files under file, including files under file in other directories
#For example, files under A /file are ignored
file/
#Only all files in the current file directory are ignored, excluding files in other directories
#For example, files under A /file are not ignored
/file
#Ignores all files in the file directory at any location
file
#Ignores all files in the file directory at any location as file
**/file
#Ignore any files in the file directory directly under the C directory
**/c/file
#With the file /
file/**
#Ignore all files in a/file, a/c/file, a/c/d/file
a/**/file
Copy the code
When the.gitignore configuration conflicts, the later configuration overrides the previous one
.gitignore solution does not take effect
git rm -r --cached .
git add .
git commit -m "update .gitignore"
Copy the code
The resources
- Git common operations summary
- Git workflow under the Byte development facility
- The company will not use Git to pull projects! Fired the next day? Day! So what are you waiting for? Why don’t you come in