Study git Before , We need to understand a concept first
version control !
version control (Revision control) It is used to manage our files in the development process 、 Revision history of contents such as catalogues or projects , Easy to view change history , Backup to restore previous versions of software engineering technology .
Realize cross regional and multi person collaborative development
Track and record the history of one or more files
Organize and protect your source code and documentation
Statistical workload
Concurrent development 、 Improve development efficiency
Track and record the whole software development process
Lighten the burden on developers , Save time , And reduce human error
In short, it is the technology used to manage multi person collaborative development projects .
There is no version control or the version control itself lacks correct process management , Many problems will be introduced in the process of software development , Such as the consistency of software code 、 Redundancy of software content 、 The materiality of software process 、 Concurrency in software development 、 Security of software source code , And the integration of software .
Whether it's work or study , Or take your own notes , Have experienced such a stage ! We urgently need a version control tool !
For multiplayer development, you must use version control !
What we learn , It must be the most popular now !
The mainstream version controllers are as follows :
Git
SVN(Subversion)
CVS(Concurrent Versions System)
VSS(Micorosoft Visual SourceSafe)
TFS(Team Foundation Server)
Visual Studio Online
There are so many version control products (Perforce、Rational ClearCase、RCS(GNU Revision Control System)、Serena Dimention、SVK、BitKeeper、Monotone、Bazaar、Mercurial、SourceGear Vault), Now the most influential and widely used is Git And SVN
1、 Local version control
Record every update of the file , You can take a snapshot of each version , Or record patch files , Suitable for personal use , Such as RCS.
2、 Centralized version control SVN
All version data is saved on the server , Co developers can synchronize updates or upload their own modifications from the server
All version data exists on the server , The local version of the user only has the version synchronized before , If you don't connect to the Internet , Users can't see the historical version , Can't switch version validation issues , Or working in different branches . and , All data is stored on a single server , There is a great risk that this server will be damaged , You lose all your data , Of course, it can be backed up regularly . On behalf of the product :SVN、CVS、VSS
3、 Distributed version control Git
Everyone has all the code ! Safe hidden trouble !
All version information warehouses are synchronized to each local user , This allows you to view all version history locally , Can submit offline locally , Only when connected push To the corresponding server or other users . Because every user has all the version data , All data can be recovered as long as there is no problem with one user's device , But this increases the local storage footprint .
No server damage or network problems , Cause a situation that doesn't work !
SVN It's a centralized version control system , The version library is centralized on the central server , And at work , They use their own computers , So first get the latest version from the central server , And then work , After finishing the work , You need to push your work to the central server . The centralized version control system must be networked to work , The demand for network bandwidth is high .
Git Distributed version control system , No central server , Everyone's computer is a complete version library , You don't need to be connected to the Internet when you work , Because the versions are all on your own computer . The collaborative approach is like this : For example, I changed the files on my computer A, Other people also changed the files on the computer A, At this time , You two just need to push each other's changes , You can see each other's changes .Git You can directly see which code and files have been updated !
Git It is the most advanced distributed version control system in the world .
Like many great things in life ,Git Born in a time of great strife and innovation .
Linux The kernel open source project has a large number of participants . Most of Linux Kernel maintenance is all about the hassle of submitting patches and archiving (1991-2002 Year ). To 2002 year , The whole project team started to use a proprietary distributed version control system BitKeeper To manage and maintain the code .
Linux There are a lot of big guys in the community ! Crack research BitKeeper !
here we are 2005 year , Development BitKeeper The same as the commercial company Linux The partnership with the kernel open source community is over , They took it back Linux The kernel community is free to use BitKeeper The power of . This forces Linux The open source community ( especially Linux The architect of the Linus Torvalds) Based on the use of BitKeeper The lesson of time , Develop your own version system .(2 Around the week !) That is to say, later Git!
Git It is the most advanced distributed version control system in the world .
Git It's free 、 Open source , first Git It's to help Linux Kernel developed , To replace BitKeeper!
Linux and Git My father Linus · Towards (Linus Benedic Torvalds)1969、 Finland
open [git Official website ] https://git-scm.com/, download git The version of the corresponding operating system .
If everything downloads slowly, you can find the image !
Download is too slow on the official website , We can use Taobao image to download :http://npm.taobao.org/mirrors/git-for-windows/
Download the corresponding version to install !
install : Without brains, the next step is ! After installation, you can use !
After successful installation, there will be... In the start menu Git term , Under the menu are 3 Procedures : Right click any folder to see the corresponding program !
Git Bash:Unix And Linux Style command line , Most used , The most recommended
Git CMD:Windows Style command line
Git GUI: Graphic interface Git, It is not recommended for beginners to use , Try to be familiar with common commands first
Always use these basic commands !
1)、cd : Change the directory .
2)、cd . . Go back to the previous Directory , direct cd Go to the default directory
3)、pwd : Display the current directory path .
4)、ls(ll): List all the files in the current directory , It's just ll( Two ll) The list is more detailed .
5)、touch : Create a new file Such as touch index.js A new one will be created in the current directory index.js file .
6)、rm: Delete a file , rm index.js It will index.js File deletion .
7)、mkdir: Create a new directory , Just create a new folder .
8)、rm -r : Delete a folder , rm -r src Delete src Catalog
rm -rf / Do not Linux Middle attempt ! Delete all files from your computer !
9)、mv Moving files , mv index.html src index.html It's the file we're moving , src It's the destination folder , Of course , Write it like this , Make sure that the file and the destination folder are in the same directory .
10)、reset Reinitialize the terminal / Clear the screen .
11)、clear Clear the screen .
12)、history View command history .
13)、help help .
14)、exit sign out .
15)、# Notation
All configuration files , In fact, they are saved locally !
Check the configuration git config -l
View different levels of configuration files :
# Check the system config
git config --system --list
# View the current user (global) To configure
git config --global --list
Git Related configuration files :
1)、Git\etc\gitconfig :Git Install under directory gitconfig --system The system level
2)、C:\Users\Administrator\ .gitconfig Only for the configuration of the currently logged in user --global overall situation
You can edit the configuration file directly here , After setting through the command, it will respond here .
When you install Git The first thing to do is to set your user name and e-mail Address . This is very important , Because every time Git This information is used for all submissions . It's embedded forever in your submission :
git config --global user.name "XXXXX" # name
git config --global user.email XXXXXX@qq.com # mailbox
Just do this once , If you pass on –global Options , because Git This information will always be used to handle everything you do in the system . If you want to use a different name or e-mail Address , You can run the command in the project without –global Options . All in all –global Configure for global , Not added as a specific configuration for a project .
Git There are three working areas here : working directory (Working Directory)、 Temporary storage area (Stage/Index)、 The repository (Repository or Git Directory). If you add remote git Warehouse (Remote Directory) It can be divided into four working areas . The transformation relationship between these four areas is as follows :
Workspace: work area , It's where you usually store project code
Index / Stage: Temporary storage area , For temporary storage of your changes , In fact, it's just a document , Save the information to be submitted to the file list
Repository: Warehouse area ( Or local warehouse ), It's where data is stored safely , Here's the data you submitted to all versions . among HEAD Point to the latest version put into the warehouse
Remote: Remote warehouse , The server that hosts the code , It can be simply considered that a computer in your project team is used for remote data exchange
The three local areas should be git Warehouse HEAD Pointed to version :
Directory: Use Git Manage a directory , It's a warehouse , Including our workspace and Git Management space .
WorkSpace: Need to pass through Git Directories and files for version control , These directories and files make up the workspace .
.git: Deposit Git Directory of management information , Automatically create when initializing the warehouse .
Index/Stage: Temporary storage area , Or call the update area to be submitted , In submit to enter repo Before , We can put all the updates in the staging area .
Local Repo: Local repository , A local version library ;HEAD Will be just the current development branch (branch).
Stash: hide , It's a working state storage stack , Used to hold / recovery WorkSpace The temporary state of .
git This is the general workflow of :
1、 Add... To the working directory 、 Modify file
2、 Put the files that need version management in the staging area ;
3、 Submit files from staging area to git Warehouse .
therefore ,git There are three states of managed files : The modified (modified), Temporary storage (staged), Has been submitted (committed)
working directory (WorkSpace) It's usually what you want Git Help you manage folders , It can be the directory of your project , It can also be an empty directory , It is not recommended to have Chinese .
Just remember the figure below for daily use 6 An order :
There are two ways to create a local warehouse : One is to create a new warehouse , The other is to clone the remote warehouse .
1、 Create a new warehouse , Need to use GIT Root execution of managed projects :
# Create a new one in the current directory Git The code base
$ git init
2、 After execution, you can see , Only one more in the project directory .git Catalog , All the information about the version is in this directory .
1、 Another way is to clone a remote directory , Because the warehouse on the remote server is completely mirrored to the local !
# Clone a project and its entire code history ( Version information )
$ git clone [url] # https://gitee.com/kuangstudy/openclass.git
2、 Go to gitee perhaps github Clone a test on !
Version control is the version control of files , To modify the file 、 Submit and other operations , First of all, you need to know what state the file is currently in , Otherwise, you may submit documents that you don't want to submit now , Or the documents to be submitted are not submitted .
Untracked: No trace , This file is in the folder , But didn't join in git library , Do not participate in version control . adopt git add The status changes to Staged.
Unmodify: The documents have been put in storage , not changed , That is, the content of the file snapshot in the version library is exactly the same as that in the folder . There are two places for this type of document , If it's modified , And into Modified. If you use git rm Move out of the repository , Then become Untracked file
Modified: The document has been modified , Just modify , There is no other operation . There are two places for this document , adopt git add Can enter temporary storage staged state , Use git checkout Then discard the modified , Back to unmodify state , This git checkout That is, take the file out of the library , Overwrite current changes !
Staged: Staging status . perform git commit Then synchronize the changes to the library , At this time, the files in the library and the local files become consistent , File for Unmodify state . perform git reset HEAD filename Cancel staging , The file status is Modified
It says the file has 4 States , You can view the status of the file through the following command :
# View specified file status
git status [filename]
# View all file status
git status
# git add . Add all files to staging area
# git commit -m " The message content " Submit the contents of the staging area to the local repository -m Submission of information
Sometimes we don't want to include certain files in version control , Like database files , The temporary file , Design documents, etc
Create... In the home directory ".gitignore" file , This file has the following rules :
Ignore empty lines or pound marks in files (#) The beginning of the line will be ignored .
have access to Linux wildcard . for example : asterisk (*) Represents any number of characters , question mark (?) Represents a character , square brackets ([abc]) Represents the range of optional characters , Curly braces ({string1,string2,…}) Represents an optional string, etc .
If there is an exclamation mark at the front of the name (!), An exception rule , Will not be ignored .
If the name is preceded by a path separator (/), Indicates the file to be ignored in this directory , Files in subdirectories are not ignored .
If the last side of the name is a path separator (/), Indicates that the subdirectory of this name under this directory is to be ignored , Not documents ( The default file or directory is ignored ).
# For comment
*.txt # Ignore all .txt Final document , In this way, upload will not be selected !
!lib.txt # but lib.txt With the exception of
/temp # Just ignore... In the root of the project TODO file , Not including other directories temp
build/ # Ignore build/ All the files in the directory
doc/*.txt # Will ignore doc/notes.txt But does not include doc/server/arch.txt
In fact, this can be used as an important information for everyone to find a job in the future !
1、 Sign up for the login code cloud , Improve personal information
2、 Set native binding SSH Public key , Realize password free login !( Password free login , This step is very important , Code cloud is a remote warehouse , We usually work in the local warehouse !)
# Get into C:\Users\Administrator\.ssh Catalog
# Generate public key
ssh-keygen
3、 Put the public key information public key Add it to the code cloud account !
4、 Use code cloud to create your own warehouse !
license : Whether open source can be reproduced at will , Open source but not commercially available , Cannot be reproduced ,… Limit !
Clone locally !
1、 New projects , binding git.
observe idea Changes in
2、 Modify file , Use IDEA operation git.
Add to the staging area
commit Submit
push To remote warehouse
3、 Submit test
These are individual operations !
The way of learning is the most important ! Learn to learn ! I spend more time in class teaching everyone to learn an idea and thought ( Learning style )
There is Tao without skill 、 Skill is still available . No way with skill 、 Stop at surgery !
Real teaching , Teach them to fish !
Branches in GIT It's relatively difficult , The branch is the parallel universe in science fiction movies , If two parallel universes don't interfere with each other , That has no effect on you now . however , At some point in time , The two parallel universes merge , We need to deal with some problems !
git Common instructions in branches :
# List all local branches
git branch
# List all remote branches
git branch -r
# Create a new branch , But still in the current branch
git branch [branch-name]
# Create a new branch , And switch to the branch
git checkout -b [branch]
# Merge the specified branch to the current branch
$ git merge [branch]
# Delete the branch
$ git branch -d [branch-name]
# Delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
IDEA In the operation
If the same file is modified when merging branches, it will cause conflicts : The solution is that we can modify the conflict file and submit it again ! Choose whether you want to keep his code or your code !
master The main branch should be very stable , To release a new version , Generally, it is not allowed to work on it , Generally speaking, work in the new dev Work on branches , After work , For example, it will be released , Or say dev After the branch code is stable, it can be merged into the main branch master come up .
Homework practice : Find a little partner , Build a remote warehouse together , To practice Git!
Don't put the Git It's hard to think , Practice more at work and you will naturally !
Meet the madman and say
Welcome to join me for wechat exchange and discussion ( Please note csdn Add )