Author: Wu Peiji Reference: http://www.cnblogs.com/wupeiqi/

Version Control When it comes to version control, you probably have a picture of your college graduation thesis on your computer.

The first draft of the dissertation is the first version of the dissertation. The first draft is the first version of the dissertation. The second version is the first version of the dissertation Do not change the version. DocCopy the code

This is the original version control approach, but it has significant drawbacks:

Multiple files. To save all versions, you need to save one file for each version.

Collaborative operation. When multiple people collaborate, files need to be packed and sent…

Easy to lose, deleted means lost forever… (Optional web disk)

In order to solve the above version control problems, a number of version control tools came into being: VSS, CVS, SVN, Git, etc., among which Git is the absolute hegemon.

Note: A general version control tool consists of two parts

Client (local) : Write content and version record locally

Server (web disk) : Store both content and version records remotely (optional)

GIT GIT is an open source distributed version control software designed to handle project version management from the very small to the very large efficiently and quickly. Git was originally designed and developed by Linus Torvalds to manage Linux kernel development. Git is free/free software distributed under the terms of the GNU General Public License version 2. For installation, see http://git-scm.com/

GitHub is a Git-based remote file hosting platform (along with GitCafe, BitBucket, and GitLab, among others).

Git itself can fully achieve version control, but all its content and version records can only be stored in the local, if you want to save the file content and version records in remote, you need to use GitHub. Usage Scenarios:

None GitHub: Maintain the duration file in the local. Git folder

GitHub: Maintain your diachronic files in your local.git folder, but also host your diachronic files in a remote repository

Other:

Centralized: The remote server stores all versions, and the user client has a certain version distributed: the remote server stores all versions, and the user client has all versionsCopy the code

History of small P’s use of Git Initial small P is a promising young programmer, determined to do important things since I was a child, a little P in the middle of the night on the Internet to find the teacher learning video, starring took an hour to find the resources want, little thinking of and promising youth spend a lot of time looking for a day like the teacher’s work, I feel great opportunity came, decisively choose entrepreneurship, Create a platform to provide all the resources of ** teachers!!

At the beginning of the business, P developed alone for a month, and the first version was finally online:

Review the development process, which only small P know the bitterness. Line one day, after the completion of small P suddenly see their development directory, oh my god this extension is too messy, join the day program problem rollback to the last version of the time, he can’t get a sure version, and I did Lao tze this system is to hundreds of thousands of people in the future to maintain the development, the saved version of the form by the original file is Low to explode.

MacBook-Pro-4:pondo zhang$ pwd                   Go to the program directory
/Users/zhang/PycharmProjects/pondo
MacBook-Pro-4:pondo zhang$ git init                Git initialization
Initialized empty Git repository in /Users/zhang/PycharmProjects/pondo/.git/
Copy the code

Git file is the most important folder in git, because the related files and versions are saved in this folder. With this folder, you do not need to worry about my many files to record the version. You can use git command to save all versions in the.git file. Two commands create a version:

MacBook-Pro-4:pondo zhang$ git status                     Git git status
On branch master
Initial commit
 
Untracked files:
 (use "git add <file>..." to include in what will be committed)
 
   .idea/
   app01/
   db.sqlite3
   manage.py
   pondo/
   readme
   templates/
 
nothing added to commit but untracked files present (use "git add" to track)
MacBook-Pro-4:pondo zhang$ git add .                       Add all files in the current directory to the repository
MacBook-Pro-4:pondo zhang$ git commit -m 'First submission'        Commit to the repository and fill out the version description for later rollback.[master (root-commit) df47fe4] 33 files changed, 879 insertions(+) create mode 100644 .idea/dictionaries/zhang.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml ...Copy the code

Note: When you run the git commit command, you may be prompted to configure the user and mailbox to record which user committed the current version

git config --local user.name 'zhangqiye'

git config --local user.email '[email protected]'
Copy the code

Git divides managed files into two regions and four states.

Workspace: the directory where the current development program is located is called the workspace. That is, the work and development are all in this directory. The status of files in this area will be detected automatically by Git.

MacBook-Pro-4:pondo zhang$ ls                # Check the source directory
app01        db.sqlite3    manage.py    pondo        readme        static        templates
MacBook-Pro-4:pondo zhang$ git status        Git current status
On branch master
nothing to commit, working tree clean
MacBook-Pro-4:pondo zhang$ touch a.py        Create a new file
MacBook-Pro-4:pondo zhang$ ls
a.py        app01        db.sqlite3    manage.py    pondo        readme        static        templates
MacBook-Pro-4:pondo zhang$ git status        Workspace A. py has changed
On branch master
Untracked files:
 (use "git add <file>..." to include in what will be committed)
   a.py
nothing added to commit but untracked files present (use "git add" to track)
Copy the code

Repository: The workspace detects that some files have changed, which means that the program has been modified since the last version. After the modification is completed, the program can be submitted as the next version, then execute the [git add.] to commit all files to the staging area. Git commit -m ‘another version’ : commit to a branch of the repository. Adds all changes since the last version to the staging state

MacBook-Pro-4:pondo zhang$ git status                   The color of the file is green, indicating the temporary status in the repository
On branch master
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)
   new file:   a.py
MacBook-Pro-4:pondo zhang$ git commit -m 'Submit again'     Commit to a branch of the repository[master f139d5d] commit file changed, 0 insertions(+), 0 deletions(-) create mode 100644 a.py MacBook-Pro-4:pondo zhang$ gitlog                      Update version commit (rollback based on version commit value)commit f139d5d0a648af06d8a1ecadd90faf572afc388a Author: zhangqiye <[email protected]> Date: Fri Aug 11 10:02:14 2017 +0800 Commit DF47FE49fc1 f14d9cdd1534baa96f46ec71a9934 Author: zhang < [email protected] > Date: Fri Aug 11 08:49:49 2017 + 0800 for the first time to submitCopy the code

At present, four commands of Git have been used, which can replace local multiple files to save versions:

Git init, which indicates that the current folder is about to be version-controlled.

Git status: Displays the current git status, such as which files have been modified and which files have not been committed to the repository.

Git add filename to add the specified file to the staging state of the repository.

Git commit -m ‘commit info’ commits the staging files to a branch of the repository.

Git log to view the commit record, that is, the historical version record

After the survey, small P was angry with himself, so 6 things why not early found, from the small P version management to say goodbye to the complex folder, quickly get up.

MacBook-Pro-4:pondo zhang$ ls
app01       db.sqlite3  manage.py   pondo       static      templates
MacBook-Pro-4:pondo zhang$ git init
Initialized empty Git repository in /Users/zhang/PycharmProjects/pondo/.git/
MacBook-Pro-4:pondo zhang$ git config --local user.name 'zhang'
MacBook-Pro-4:pondo zhang$ git config --local user.email '[email protected]'
MacBook-Pro-4:pondo zhang$ git add .
MacBook-Pro-4:pondo zhang$ git commit -m 'First migration of project to Git control version'[master (root-commit) 6c439d2] mysql > update 870 insertions(+) create mode 100644 .idea/dictionaries/zhang.xml create mode 100644 .idea/encodings.xml ...Copy the code

Just in time, you need to develop an African zone feature, no longer need to copy the file again, directly start to work in the workspace, 30 minutes to complete the development test, another version is done!!

MacBook-Pro-4:pondo zhang$ git status            # Africa feature development, only changes to app01/views.py
On branch master
Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)
   modified:   app01/views.py
no changes added to commit (use "git add" and/or "git commit -a")
MacBook-Pro-4:pondo zhang$ git add .
MacBook-Pro-4:pondo zhang$ git commit -m 'Africa Online'[Master 0972F4b] 1 file Changed, 3 insertions(+), 1 deletion(-) macMacL-pro-4: Pondo Zhang $Copy the code

A month after the African zone online, users have received complaints, the original pure and fresh and refined small P where to go? How did it get so intense? Want to go back….

Small P has always been adhering to the principle of serving the people, the people do not want to see that must be modified. Decision: Roll back to the previous version.

So the question is, right?

A month has passed, the location of the code modification has long forgotten, how to modify, the total can not be developed again. Git rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback

Rollback to the specified version:

MacBook-Pro-4:pondo zhang$ git logcommit 0972f4bb43104baee15aeec2dd62bd0a307ec837 Author: zhang <[email protected]> Date: Fri Aug 11 10:54:42 2017 + 0800 African zone online commit 6 c439d2fd0d943f36f3ee84e158ff86b052961d2 Author: Wu Peiqi <[email protected]> Date: Fri Aug 11 10:42:09 2017 +0800 Project first ported to Git control version macMac-Pro-4: Pondo Zhang $Git reset --hard 6 c439d2fd0d943f36f3ee84e158ff86b052961d2 HEAD is now at six c439d2 program into the Git version control for the first timeAfter executing the command, all files in the workspace will now be undeveloped before the Africa feature is created
Copy the code

Now that the rollback is complete, P is wondering what if one day he wants to go back to the version with the Africa feature. You can’t use git log to check your records and then rollback them.

MacBook-Pro-4:pondo zhang$ git reflog 6c439d2 HEAD@{2}: reset: Moving to 6 c439d2fd0d943f36f3ee84e158ff86b052961d2 0972 f4b HEAD @ {3} : commit: African zone online 6 c439d2 HEAD @ {4} : commit (initial) : Pondo Zhang $Git reset --hard 0972F4b HEAD is now available at 0972F4bCopy the code

Recently, the live streaming industry is becoming increasingly hot, small P also hope their platform to join the live streaming function, has been estimated to be completed in 2 months, small P began to work day and night…

One month has passed, and the development task is proceeding in an orderly manner as expected, and the live broadcast function has been half completed. At this time, there is a Bug in the online operation platform that needs urgent repair. What should I do? What to do? What to do??

Small P came up with several solutions:

Is developing code immovable, copy a line running code to modify ——> no, and step back to copy.

The development of a month’s code to delete, modify the Bug, and then start again ——> no, must be silly force so dry, I am not stupid force.

I have heard about git stash and can find it at ——>

Git branch can be found at ——

Scheme 1: Stash

Stash is used to obtain all files changed in the workspace and store them temporarily in “somewhere” to restore the workspace to the state before the current version is operated. Stash also brings files temporarily stored “somewhere” back to the workspace again.

acBook-Pro-4:pondo zhang$ vim app01/views.py             # In the middle of developing live streaming
MacBook-Pro-4:pondo zhang$ git status
On branch master
Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)

   modified:   app01/views.py

no changes added to commit (use "git add" and/or "git commit -a")

MacBook-Pro-4:pondo zhang$ git stash                     # Store the half-developed live feature "somewhere" temporarilySaved working directory and index state WIP on master: Pondo Zhang $git status Pondo Zhang $git status Pondo Zhang $git status Pondo Zhang $git status Pondo Zhang $git status# Workspace goes back to the current version without doing anything
On branch master
nothing to commit, working tree clean

MacBook-Pro-4:pondo zhang$ vim pondo/settings.py         # Urgent bug fixes
MacBook-Pro-4:pondo zhang$ git status
On branch master
Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)

   modified:   pondo/settings.py
no changes added to commit (use "git add" and/or "git commit -a")

MacBook-Pro-4:pondo zhang$ git add .                     # Add the code to fix the bug to the staging state
MacBook-Pro-4:pondo zhang$ git commit -m 'Urgent bug fix'     # Submit bug-fixing code to branch[Master 1300d33] 解 决 bug 1 File Changed, 1 Insertion (+) MacBook Pro 4: Pondo Zhang $git Stash Pop# Take the half-finished live feature from "somewhere" and take the workspace again to continue development
On branch master
Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)
   modified:   app01/views.py
no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash @ {0} (059 d78ca8fa204f9559bd3ce0ae76235969b4301) special: When you execute the git stash pop command, you may encounter conflicts because there is overlap between code that is urgently bugged and code that is stored "somewhere" with stash.Copy the code

Stash related common commands:

Git Stash stores all changes to the current workspace “somewhere”, restoring the workspace to its unmodified state in the current version

Git Stash list Looks at all the records stored “somewhere”

Git Stash clear

Git Stash pop brings the first record back to the workspace from “somewhere” (potentially conflicting)

Git Stash apply number, which brings the specified numbered record back to the workspace from “somewhere” (possibly conflicting)

Git Stash drop number: deletes the record with the specified number

Solution 2: Branch

Branch learning: A branch is called a branch, and by default there is only one branch named Master. The general process for developing new features is as follows: New features are developed on branch dev and then merged into the Master branch after development.

MacBook-Pro-4:pondo zhang$ git branch dev                 Create a new branch, that is, copy the current branch code to the new branch
MacBook-Pro-4:pondo zhang$ git checkout dev               Switch to the dev branch
MacBook-Pro-4:pondo zhang$ vim app01/views.py             # Development features
MacBook-Pro-4:pondo zhang$ git status                     App01 /views.py was modified in the dev branch
On branch dev
Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)

   modified:   app01/views.py

no changes added to commit (use "git add" and/or "git commit -a")
MacBook-Pro-4:pondo zhang$ git add .                      Add the modified file to the staging area of the repository
MacBook-Pro-4:pondo zhang$ git commit -m 'New features developed'    Commit the contents of the staging area to the current branch: dev branch
[dev 32b40cd2 insertions(+) macBook-pro-4: Pondo Zhang $git checkout masterSwitch back to the master branch
Switched to branch 'master'
MacBook-Pro-4:pondo zhang$ git merge dev                   # merge dev content into masterUpdating 0972f4b.. 32b40cd
Fast-forward
app01/views.py | 2 ++
file changed, 2 insertions(+)
Copy the code

Note: Git merge can also cause conflicts with the same stash solution: find the conflicting files, manually modify the conflicts and commit them.

Branch Common commands:

Git branch Branch name Creates a branch

Git Checkout branch name Switch branch

Git branch -m Creates the branch name and switches to the specified branch

Git branch views all branches

Git branch -d Branch name Deletes a branch

Git merge Branch name merges the specified branch into the current branch

During the period of rapid development of Git, small P never forgets its original intention and always provides resources for the vast number of promising young people, which makes the page view of the website continue to climb. It has already established the scale and made some money. If it has money, it will build it. Diaosi is diaosi, small P is still inseparable from the habit of writing code, so the task of development is still undertaken by their own, small P began to write code in the international trade during the day, back to Tiantongyuan at night to write code. PS: rich, a new computer in the company, an old computer at home… Shit, how do you make a story interesting? It’s too hard.

P began to think, I love to write code, company write, write at home, if every day back and forth with a USB copy is really troublesome, Git is similar to the cloud disk like things can be synchronized data? The answer is definitely yes. It has to be, or I can’t really make it up.

GitHub, a git-based code hosting platform, allows you to record content and versions remotely as well as on a usb flash drive (similar to a cloud drive). PS: There are many similar GitHub products, such as GitLab, Bitbucket, code cloud and so on.

To implement code hosting based on GitHub, the following steps are required:

Registered making

Create a repository. After the repository is created, a URL will refer to the repository, such as:

After P learns how to use Git and GitHub, he can host code remotely based on GitHub.

At home, P pushes the code to GitHub after developing some features.

MacBook-Pro-4:pondo zhang$ git remote add origin https://github.com/zhang/pondo.git   Create an alias origin for the address
MacBook-Pro-4:pondo zhang$ git push origin master              # Push local master branch content and version information to GitHub
Username for 'https://github.com':                               Enter the GitHub username
Password for 'https://[email protected]':                       Enter the GitHub password
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 270 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 localobject. To https://github.com/zhang/pondo.git 634aac4.. 274f1e4 master -> master MacBook-Pro-4:pondo zhang$ git push origin dev# Push local dev branch content and version information to GitHub
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 localobject. To https://github.com/zhang/pondo.git 274f1e4.. 50e2169 dev -> devCopy the code

In the company, when a new computer is used for the first time, you need to get the code from GitHub and continue to develop it.

MacBook-Pro-4:github zhang$ git clone https://github.com/zhang/pondo.git    # Get the project from GitHub
Cloning into 'pondo'. remote: Counting objects: 31, done. remote: Compressing objects: 100% (26/26), done. remote: Total 31 (delta 2), reused 30 (delta 1), pack-reused 0 Unpacking objects: 100% (31/31), done. MacBook-Pro-4:github zhang$cd pondo/
MacBook-Pro-4:pondo zhang$ git Branch                                          Only the master branch is retrieved by default
* master
MacBook-Pro-4:pondo zhang$ git branch dev origin/dev                           Create dev branch and synchronize with remote dev branch
Branch dev set up to track remote branch dev from origin.
MacBook-Pro-4:pondo zhang$ git checkout dev                                    Switch to the dev branch
Switched to branch 'dev'
MacBook-Pro-4:pondo zhang$ vim app01/views.py                                  # Keep developing new features
MacBook-Pro-4:pondo zhang$ git add .                                           Add files to the temporary state of the repository
MacBook-Pro-4:pondo zhang$ git commit -m 'Corporate Development Function 1'                         Submit new functionality to the repository branch[dev 9281447] 1 File Changed, 1 insertion(+), 1 deletion(-) MacBook pro-4: Pondo Zhang $git push origin dev# commit dev branch content to dev branch of remote GitHub hosting repository
Username for 'https://github.com': zhang
Password for 'https://[email protected]':
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 427 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 localobjects. To https://github.com/zhang/pondo.git 50e2169.. 9281447 dev -> devCopy the code

At home, some functions have been developed in the company during the day and submitted to GitHub, but the code of the computer at home is still the version of last night, so we need to pull the latest code from GitHub and continue development.

MacBook-Pro-4:pondo zhang$ git checkout dev                                   Switch to the dev branch
Already on 'dev'
MacBook-Pro-4:pondo zhang$ git pull origin dev                                Get the latest dev branch content from the remote GitHub repository and merge it locallyremote: Counting objects: 4, done. remote: Compressing objects: 100% (2/2), done. remote: Total 4 (delta 2), reused 4 (delta 2), pack-reused 0 Unpacking objects: 100% (4/4), done. From https://github.com/zhang/pondo * branch dev -> FETCH_HEAD 50e2169.. 9281447 dev -> origin/dev Updating 50e2169.. 9281447 Fast-forward app01/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) MacBook-Pro-4:pondo zhang$ vim app01/views.py# Keep developing new features
MacBook-Pro-4:pondo zhang$ git add .                                         Add files to the temporary state of the repository
MacBook-Pro-4:pondo zhang$ git commit -m 'Home Development features 1'                        Submit new functionality to the repository branch
Copy the code

In the company, as we have developed some functions at home last night, we need to pull the functions developed last night from GitHub and continue to develop them.

MacBook-Pro-4:pondo wupeiqi$ git checkout dev                                   Switch to the dev branch
MacBook-Pro-4:pondo wupeiqi$ git fetch origin dev                               Get the latest dev branch from GitHub repository to the repository branchremote: Counting objects: 3, done. remote: Compressing objects: 100% (1/1), done. remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/WuPeiqi/pondo * branch dev -> FETCH_HEAD 150d891.. 65b6604 dev -> origin/dev MacBook-Pro-4:pondo wupeiqi$ git merge origin/devMerge the branch contents of the repository into the workspaceUpdating 150d891.. 65b6604 Fast-forward readme | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py# Keep developing new features
MacBook-Pro-4:pondo wupeiqi$ git add .                                          Add files to the temporary state of the repository
MacBook-Pro-4:pondo wupeiqi$ git commit -m 'xxxxxxxxxxx'                        Submit new functionality to the repository branch
Copy the code

In the long run, Git and GitHub can be used together to avoid data loss and multi-site development problems caused by computer corruption. Git pull origin = “git fetch origin” + “git merge origin/” Then you need to resolve the conflict manually before moving on.

The start-up maturity of small P with Git

Small P’s company is developing better and better, but the company’s single product is a serious shortcoming. After study and investigation, small P decided to recruit 3 Python programs to develop another product “About P” platform to enrich the company’s business line and provide a complete set of services for users.

Small P’s Slogan: Look want, want to contract. Don’t ask me what I want, I don’t know hahahahahahahaha

The “about P” platform requires three people to work together. There is still a slight difference between multi-person and single-person development on GitHub. There are two ways to work together:

Partner, after adding another user to the repository partner, that user has the ability to submit code to the current repository.

Organization, create an organization, and then you can create multiple projects under the organization, and members of the group can submit code to all projects in the group. PS: You can also assign collaborators to a project

The co-development command is similar to the previous steps, so instead of rewriting the code, a file is used to describe the entire process of co-development by three people.

Create a program

User A creates an application and submits it to GitHub

User B clones the project

User C clones the project

Development capabilities

User A develops function 1

User B develops function 2

User C develops function 3

submit

User A submits function 1 and pushes (user A submits first with fast hand speed.)

User B submits function 2, which cannot be pushed because there is already new code submitted by others on GitHub. Solution: Get the latest code from GitHub, merge it locally, and submit your own feature 2.

User C submits function 3, which cannot push and cannot submit, because there is already new code submitted by others on GitHub. Solution: Get the latest code from GitHub, merge it locally, and submit your own feature 3.

Get the latest code

User A obtains the latest code

User B gets the latest code

User C gets the latest code

In the solution position highlighted in red above, there are three ways to do this. All three can complete the merge and commit the new functionality, but the logging will be different. For example, the merge will occur in the first two version records, while the third one keeps the version records clean and tidy.

Git pull origin master git fetch origin master git merge origin/master git push Origin master user A: touch 4.py git add. Git commit -mFunction of '4'Git add. Git commit -m'function 5'
   git push origin master # error because someone has submitted new code on GitHub
   git pull origin master
   git push origin master

Copy the code

A: touch 4.py git add. Git commit -mFunction of '4'Git add. Git commit -m'function 5'
   git push origin master # error because someone has submitted new code on GitHub
   git fetch origin master
   git rebase origin/master
   git push origin master
Copy the code

Finally finally small P in the listed company to achieve financial freedom, but as a technology good or from the essence of prick silk, so every day is around making, see what others have good project, you can give him to choose bug pack to force, but others may not give small P to co-operation or something, that how to change others to contribute code? That’s fork…

Find the project you want to work on, fork it, and it pops up in your repository