Use of git

1. The difference between git and svn

svn is a centralized version control system

git is a distributed version control system

 

Centralized:

 

 

It can be seen that svn centralized version control stores all versions in the svn server

And must be connected to the svn server to perform version rollback, update and other operations.

If one day the svn server is broken, then all the version codes will be lost, and you can't get the code and roll back the version and other operations. God, this will be a sad story!

 

How to turn sadness into happiness, let's see how git works!

 

distributed:

 

It can be seen that with git, each computer has a complete version number and log information.

And when there is no Internet, git can still work, just submit the code to the local, and wait for the Internet to submit to the remote git warehouse

It doesn't matter if the git server breaks down one day, because the local repository keeps the full version.

 

Build a github server (see after watching linux server): http://blog.w0824.com/2018/07/09/Centos%E6%90%AD%E5%BB%BAGIT%E6%9C%8D%E5% 8A% A1% E5% 99% A8 /

Free third-party platforms: github, code cloud, https://coding.net/

 

Git inventor: is the Linux founder Linus

Several commands for Linux:

ls: View the files in the directory (list)

clear: clear the screen

pwd: view the current working directory

2. Install the git tool

Pay attention to the number of bits in your system.

 

Choose the installation path when installing, and then all the way to next. After installation, the right mouse button will have the following two options, indicating that the git tool installation is complete.

 

 

3. Get the git repository

There are two ways to obtain a git repository,

One is to execute the git init command in the local directory to initialize a warehouse.

The second is to pull a warehouse from a remote server. Such as pulling from github, or pulling from a git server built by yourself.

 

Here focuses on explaining how to create a warehouse on github in the second way, the first way is too simple.

(1) Create a warehouse on github

Creating a warehouse is relatively simple, just refer to the following picture:

1. Click the plus sign + and select New repository to create a new warehouse

 

2. Enter the information to create the warehouse

 

Once created, it looks like this:

 

4. Clone the remote warehouse code to the local

Clone the remote warehouse code to the specified directory:

Command: git clone warehouse address [directory]

Note: Not writing the directory name will create a directory with the same name as the github repository in the current directory

For example, the following instruction represents checking out the warehouse code to the current directory (./)

git clone warehouse address ./

 

The warehouse address is located at:

 

There are two protocols for warehouse addresses: https and ssh. Using the ssh protocol later can save you the trouble of entering the password every time you push the code.

 

Complete command: git clone  https://github.com/ww24kobe/test_project.git   ./, after success, it will add a .git hidden folder in the directory.

5. git workflow

A git repository contains the following three parts:

 

Workspace: This is the directory that we can see on our computer.

Temporary storage area: English name is stage, or index. Generally stored in the index file of the ".git directory".

Local repository: There is a hidden directory .git in the workspace. This is not a workspace, but a git repository. And automatically create a master branch and a HEAD pointer to the branch where it is currently located

 

as follows:

 

 

 

Many git commands in the back are used in the above three.

6. Instructions commonly used by git in development

l Global settings: You must first set the submitted user name and email address. If you do not set it, you cannot submit the code. You can find the person in charge of the code later

git config --global user.name name # what's the name

git config --global user.email mailbox # how to contact you

 Remove --global is only valid in the current project

git config user.name name # what's the name

git config user.email mailbox # how to contact you

l View configuration information

 git config --list to see how to use the command, such as git commit --help

l Create a git repository in the specified directory

git init: After execution, it will generate a .git hidden folder in the current directory

If you need to push the local warehouse code to the remote warehouse later, you need to set the remote warehouse address.

git remote add origin url # Set the local remote warehouse address

l Clone the remote warehouse code to the specified directory:

git clone url [directory]

Note: Not writing the directory name will create a directory with the same name as the github repository in the current directory

For example, the following instruction represents checking out the specified url warehouse code to the current directory (./)

git clone url  ./

l Add all files in the current directory to the temporary storage area:  

git add . 

l Check the status of the temporary storage area:

git status

l Submit documents:    

git commit -m 'Remark information'

l Files that have been tracked by git can be abbreviated:

git commit -am 'Remark information'

l View the information of the submission notes (view the submission record)

git log or git reflog or git log --oneline

Cooler display: git log --oneline --graph

--Graph graphical display, more intuitive.

 

More powerful display: set an alias for the log command,

git config --global alias.lg “log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative”

 

l Code version rollback:

 

git reset --hard HEAD Back to the current version

git reset --hard HEAD ^ Back to the current version

git reset --hard HEAD ^^ Back to the current version of the previous two versions

git reset --hard af4542g (use git log to get the top 7 bits of the log, you can go back to the specified version)

l Delete files

git rm  files

l Withdraw modification

git checkout files

l Push the code to the remote server:

Syntax: git push -u remote name local branch name: remote branch name

git push -u origin master (the first push plus -u, both called master can be omitted, just write a master)

l Modify the local remote warehouse address:

git remote -v View the local remote warehouse address

git remote add origin url # Set the local remote warehouse address

git remote rm origin # remove the local remote warehouse address

l Get content from a remote server:

git push -u remote name local branch name: remote branch name

git pull remote name remote branch name: local branch name

 

git pull orgin master pull remote warehouse code and merge

git fetch orgin master pull remote warehouse code will not be merged, you need to execute git merge origin / merge to merge

 

Remote code forced to merge local code:

git pull origin master  --allow-unrelated-histories

l Warehouse address

git remote -v #View the local remote warehouse path

git remote rm origin #Remove the local remote warehouse address

git remote add origin [email protected]: username / repository name.git #set local remote repository address

 

l Check who wrote each line of code in the file, especially if the error code is found, there is no way to run.

git blame files

7. Create private and public keys for the ssh protocol

If the warehouse address uses the https protocol, each submission will require the user name and password of the remote warehouse github,

If we use the ssh protocol as the warehouse address, and configure the private key and public key, every time you submit will avoid the trouble of entering the user name and password.

 

Public key: understood as a lock, uploaded to github and stored.

Private key: understood as the key to the lock, stored on the local computer.

 

In other words, only the corresponding key of the lock can submit code.

 

Create ssh private key and public key, enter: 1 -t rsa -C (uppercase C oh) 'email address', and then press Enter all the way, after success, the following two files will be added in the current user's directory.

id.rsa: private key file

id_rsa.pub: public key file

 

Copy the public key content of id_rsa.pub to github, the steps are as follows:

 

After adding it, it looks like this:

 

Finally, modify the remote warehouse address to ssh protocol:

git remote -v #View the local remote warehouse path

git remote rm origin #Remove the local remote warehouse address

git remote add origin [email protected]: username / repository name.git #set local remote repository address

 

 

 

 

Summary of two ways to create a warehouse

l Method 1: Go to github to create a warehouse remotely.

Note: When creating, pay attention to uncheck the checkbox of the readme in front

 

 

l Initialize a warehouse locally by git init

 

 

 

 

 

8. Create a label (version number)

l View all tags

git day 

 

l Create a label with version number 1.0, -m Remarks

git tag v1.0  -m ‘version 1.0’ 

 

l Push all the local tags to the remote warehouse, and others will see these tags after cloning the warehouse or pulling data for synchronization.

git push origin master --tags

 

 

 

9, git branch

master branch: This branch is default in every repository, mainly used to release the official version of the code.

dev branch: The usual code development is carried out on this branch.

 

The master branch and the dev development branch work together:

Generally, after the development branch dev is developed, the code of this branch is merged into the master branch, and finally the code under the master branch is pushed to the remote server (that is, the remote warehouse github)

 

Branch related instructions:

l View all branches of the warehouse:

git branch

l Create a dev branch:

git branch dev

l Switch branch (switch to master branch):

git checkout master

l Merge branch dev into master master branch

First switch to the branch to be merged, and then merge the dev branch to the current branch

git checkout  master   

git merge --no-f dev -m 'Merge information' 

Note: Merging through the option --no-f is also considered as one submission

l Delete the branch dev:

git branch -d dev

l If the branch has not been merged, you can force delete:

git branch -D dev

l Submit the branch dev to the remote

git push origin dev

l Delete the remote dev branch

git push --delete dev

The management strategy document about the git branch:

 

 

 

 

 

10. Build a git server

 

Address of the reference book: https://www.jianshu.com/p/e79ea05d9b61

 

You can watch it after learning linux.

 

11、fork

You can fork (understand to copy) someone else's project under your username

 

 

12、issue

Give comments and suggestions to the project, including project bugs, make the project better together, and learn together.

 

13、full request

When you find that there is a bug in the project you fork, you can modify it and submit it to your repository. You can initiate a pull request. The original author of the project will see this pull request. If there is no problem, the request will be merged and merged. Into the project.

14、contributor

Add helpers to your project development to complete this project together. Click Settings of your own project

 

 

Added successfully, you can view it through the distributor.

 

Guess you like

Origin www.cnblogs.com/oujianjun/p/12733934.html