Git introduction to mastery


Git introduction to mastery

I What is git

Git official website address
Git source code

1.1 introduction to Git

  • Git is an open source distributed version control system for agile and efficient handling of any small or large project.
  • Git is an open source version control software developed by Linus Torvalds to help manage Linux kernel development.
  • Git is different from the commonly used version control tools CVs and subversion. It adopts the way of distributed version library and does not need to be supported by server-side software.

1.2 advantages of GIT

  • Most operations are done locally and do not require networking
  • Fast and flexible
  • Try to add data instead of deleting or modifying it
  • Branch operation is very fast and smooth
  • Fully compatible with Linux commands

II The difference between GIT and SVN

Svn is a centralized version control system, the version library is centralized on the central server, and when working, you use your own computer, so first you need to get the latest version from the central server, and then work. After completing the work, you need to push your work to the central server. The centralized version control system must be connected to the network to work, and has high requirements for network bandwidth.
Git is a distributed version control system, without a central server, everyone’s computer is a complete version library. There is no need to connect to the Internet when working, because the versions are on their own computers. The method of collaboration is as follows: for example, if you change file a on the computer, others also change file a on the computer. At this time, you two can see each other’s changes by pushing your changes to each other.
Git introduction to mastery

III Git download and installation

3.1 download Git

Git pipe network download

Git introduction to mastery
Execute EXE file
Keep clicking next
Git introduction to mastery
Right click on the desktop to display
Git introduction to mastery
Git Bash: Unix and Linux style command line, most used and recommended
Git CMD: Windows style command line
Git GUI: git with graphical interface is not recommended for beginners. Try to be familiar with common commands first

IV Git structure

Workspace: it is the directory you can see in the computer (the written code exists in the workspace).
Staging area: it is called stage or index in English. It is usually stored in the index file (. Git / index) under the “. Git directory”, so we sometimes call the temporary storage area index (documents planned to be submitted)
Version Library: (local library) the workspace has a hidden directory Git, this is not a workspace, but a git version library.
Git introduction to mastery

V GIT and code hosting Center

5.1 code hosting Center

Tasks of code hosting Center:Maintain remote library
LAN environment:

  • Gitlab server

Under the external network environment:

5.2 local library and remote library

Intra team collaboration and cross team collaboration

5.3 cooperation process within the team

Git introduction to mastery

5.4 cross team collaboration

Git introduction to mastery

Vi Git command line

6.1 create local library

Right click to enter git bash here

MKDIR blog create file   
CD blog enter blog
Ll - a view hidden Git file content
Git init initializes a git repository
ll . Git / means to view File list display under Git
cat . Git / config view Git / config content

be careful:. Git directory is the directory and file where the local library is stored. Do not modify or operate it

6.2 setting signature

6.2.1 signature overview

  1. Signature function: used to identify different developers
  2. Project level (warehouse): valid only in the current local library range
  3. System level: the range of users who log in to the current operating system
  4. Level priority: proximity principle: project level priority is higher than system (user) level signature

be careful: the signature level must be set. If it is not set, GIT may lead to command travel. Indistinguishable

6.2.2 signing order

Warehouse (project level)Only valid in the current local scope of the Library:

git config zysheep
 git config [email protected]
 cat . Git / config view setting results

System level: the range of users who log in to the current operating system

git config –global zysheep
 git config –global [email protected]
 Cd ~ home directory
 Ll – a | grep – I git view the file name called Git
 cat . Gitconfig view file contents

In the actual development process, you usually only need to set the system level. If there are special needs, set the project level again.

6.3 common commands

Git status is used to display the status of working directory and staging area
 git add    xxx. Txt writes the workspace to the staging area
 The GIT RM command is used to delete files from the workspace and index.
 Git commit - M 'remarks' submit the staging area to the local library
 Git checkout switch branches or restore work tree files
 Git log displays the submission log information
 Git log – - pretty = online more elegant display (view history)
 Git log --oneline
 Git reflog // used to display the step head that needs to be rolled back

6.4 retrieve after permanent deletion

git rmThe command deletes files from the workspace and index.

  1. Create files in the workspace and submit them to the local library through commands
Vim d1.txt  
 git add d1.txt  
 Git commit – M 'upload to local library D1 txt’ d1. txt
  1. Execute the command to delete:
git rm d1. Txt workspace is deleted and submitted to the local library
Git commit – M 'delete D1 txt’ d1. txt
  1. Retrieve through the log git reflog
$ git reset --hard 15d5002
Ll view it

be careful:As long as git operates in the local library, it will record the version.

6.5 document comparison

gid diffCompare the files in the workspace with the staging area

vim git. Txt modify content
git diff git. Txt is used to display changes between submission and work tree, etc

gid diffCompare the files in the staging area with the local library

git add git. Txt will GIT Txt workspace added to staging area 
git diff HEAD git. Txt head represents the pointer of the latest version of the local library    
Git diff without file name
Git checkout file name return modification

VII Interaction between local and remote Libraries

7.1 initialize local library and establish connection with remote library

  1. git remote -vView all current remote address aliases

Git introduction to mastery

  1. git remote add[alias] [remote address]

Git introduction to mastery
Git introduction to mastery

  1. PushGit push [alias] [branch name]

Git introduction to mastery

The above error occurs because there are no files in the directory, and an empty directory cannot be submitted.

We add a file to the directory and usegit addAdd to staging area,git commitSubmit to local library.

Git introduction to mastery

You may need to set the signature (set the global signature, otherwise set the front of the project level, create the local warehouse later, and then reset it). After the setting is completed, you can set the signature againgit commit, finallygit push

Git introduction to mastery

that is becausegithubIt is caused by the inconsistency between the version of the remote library and the local library on. Let’s follow the prompts firstpull, againpush

git pull origin master
git push origin master

ifgit pullFailed, prompt:fatal: refusing to merge unrelated histories, this is because there are two git libraries that are not related at all. One is a local library and the other is a remote library. Then, the local library is pushed to the remote end. The remote end feels that this local library is irrelevant to itself, so it is told that it cannot be merged. There are two solutions:

  • The first one: pull down the code from the remote library, put the local code to be added into the remote library, and then submit it, which is equivalent to an update;
  • Second: forced pullinggit pull origin master --allow-unrelated-histories

Summary:Create a remote warehouse. After the local warehouse is initialized, usegit pullPull and keep the version of local library consistent with that of remote library.

7.2 establishing connection between clone and remote library

Git clone [remote address]

Cloning does these things for us:

  • Complete download the remote library to the local
  • Create remote library address alias: origin
  • Initialize local library

If the remote library has been found, it is recommended to use cloning to establish a connection with the remote library

7.3 switching remote Libraries

Method 1: modify the remote warehouse address

Git remote - V # view the address of the remote warehouse

Git remote set URL origin URL # change the remote warehouse address, and the URL is the new address.

Method 2: delete the remote warehouse address first, and then add it

Git remote RM origin # delete an existing remote warehouse
Git remote add origin URL # add a new remote warehouse

VIII branch

8.1 branch concept

  1. What is a branch?
    In the process of version control, multiple lines are used to advance multiple tasks at the same time.

Git introduction to mastery

  1. Benefits of branching?
  • At the same time, promote the development of multiple functions in parallel to improve the development efficiency
  • In the development process of each branch, if the development of one branch fails, it will not be responsible for other branches
    What impact.
  • The failed branch can be deleted and restarted

8.2 branch command

  1. Create branch
git branch develop
  1. View local branches:
git branch

Note:The current branch is preceded by a * sign

  1. View remote branches:

Add the – a parameter to view the remote branch, and the remote branch will be indicated in red (if you turn on color support)

git branch -a
  1. Switch branch
git checkout branch_name
  1. Delete local branch
git branch -d branch_name
  1. Delete remote branch
git branch -r -d origin/branch-name  
git push origin :branch-name 
  1. If a new branch is created remotely, it does not exist locally.

Can usegit checkout --track origin/branch_nameAt this time, a new branch named branch will be created locally_ Name, the remote branch with the same name will be automatically tracked_ name。

git checkout --track origin/branch_name
  1. If a new branch is created locally_ Name, but not remotely.

At this timepushandpullThe instruction cannot determine who should be tracked. Generally speaking, we will make it track the remote branch with the same name, so it can be usedgit push --set-upstream origin branch_nameIn this way, you can automatically create a branch remotely_ Name branch, and then the local branch will track the branch. Then use push and pull for this branch to synchronize automatically.

git push --set-upstream origin branch_name
  1. Merge and branch to master
    First switch to the master branch
git  checkout master

If it is developed by multiple people, you need to pull down the code on the remote master

git pull origin master

Then we merge the code of the dev branch into the master

git  merge dev

Then view the status

git status

Examples: merge branches

  1. In branch hot_ Modify git on fix Txt file and submit it to the local library
  2. Branches need to be merged. Let GIT. On the master Txt synchronization

Switch to the branch that accepts the modificationmaster(will)hot_fixSync content tomaster

Git checkout master // switch to the branch that accepts modification

Execute the merge command. The GIT merge command is used to add (merge) two or more development histories.

git merge hot_ fix   //hot_ Fix the name of the merged branch

7.3 consolidated branch (conflict) resolution

Problem: modify the files in the two branches separately (and the same location is changed) to merge.

1. View branch information:

 git branch -v

Git introduction to mastery

2. Modify the contents of a line of file under master. Then submit.

vim git.txt  
git add git.txt
Git commit – M 'master commit' txt

Git introduction to mastery
3. View branch information

git branch -v

Git introduction to mastery
4. In hot_ Modify the contents of a master peer file under fix. Then submit

git checkout hot_fix
vim git.txt
git add git.txt
git commit –m ‘hot_ Fix submit 'GIT txt

Git introduction to mastery
View version information:

Git introduction to mastery


Git merge master // in hot_ Merge codes from fix to master

Automatic merge failed;  fix conflicts and then commit the result. (auto merge failed; fix conflicts and submit results.)

Git introduction to mastery
View file (conflicting):
cat git.txt
Git introduction to mastery
Handling conflicts:

  • It shall be solved by human communication.
  • File editing

Modify file:

vim git. Txt // clear the conflicting symbols. Modify the content)
git add  git.txt
Git commit – M 'conflict fixed' // no file name is required

Git introduction to mastery

Recommended Today

Generate QR code in Python

This paper introduces the basic use of a QR code library that can be associated. Myqr (amzqr) is very convenient to use Myqr (amzqr) library You can generate ordinary QR code, art QR code (black and white or color), and animation QR code (black and white or color). At present, the official website seems to […]