Article From:https://www.cnblogs.com/tangxuliang/p/9216735.html

 First, preface

Git It is an open source distributed version control system for agile and efficient processing of any small or large project. It is different from the commonly used version control tools such as CVS and Subversion. It adopts a distributed version library and does not require server-side software support.

Git The difference from SVN:

  • SVN A centralized version control system,Version library is centralized on the central server, and when working, it is their own computer, so first of all, to get the latest version from the central server, and then work, after the completion of the work, you need to get the work done to the central server. Centralized version control system must be networked to work.The network bandwidth requirement is high.
  • Git Distributed version control system,Without a central server, everyone’s computer is a complete version library. When working, there is no need for networking because the version is on its own computer. The way of collaboration is like this: for example, you have changed the file A on the computer, others have also changed the file A on the computer, at this point, you only need to put the two files on each computer.You can see each other’s changes by pushing your own changes to each other.

Comparison between centralized version control and distributed version control:

Two, installation and configuration

Git Download address: https://git-scm.com/downloads

2.1 Setting the username and mailbox (user identity)

When you install itGit The first thing to do is to set up your user name and Email. The address. It’s very important because every timeGit This information is used for submissions. It is always embedded in your submission:

git config --global user.name "xuliangTang" 
git config --global user.email "1297441127@qq.com"

Attentiongit config  Commanding–global  The parameter, using this parameter, indicates that all Git warehouses on your machine will use this configuration and, of course, specify a different username and Email address for a warehouse.

2.2 Create a version Library

The version library is also named repository, and you can simply understand it as a directory in which all the files in this directory can be managed by Git. Each file is modified, deleted, and Git can be tracked so that it can track history at any time, or at some time in the future.The carving can “restore”.

Throughgit init  Commands can turn the current directory into a warehouse that Git can manage:

At this time Git built the warehouse and told you that you were an empty warehouse (empty Git repository), and there was a.Git directory under the current directory, which was Git to track the management version library, and never manually modify it.The files in this directory will be destroyed if Git files are changed.

You can also use the following commands to create the directory and warehouse together:

# Create a new directory and initialize it to the Git code library.
git init [project-name]

2.3 Cloned remote warehouse

Through the GIT clone command, the repository on the remote server can be completely mirrored to the local:

# Clone a project and its entire code history (version information)
git clone [url]

Three, Git file operation

Version control is the version of the file control, to modify, submit and other files, first of all to know what the status of the file is in the first place, or may submit the documents that do not want to submit, or the documents to be submitted are not submitted. GIT does not care about the specific differences between the two versions of the document, but ratherIf the file is changed, if the file is changed, a snapshot of the new version of the file is generated when the submission is added, and the method of judging whether the whole file is changed is to use the SHA-1 algorithm to calculate the checksum of the file.

3.1 4 states of the file

  • Untracked:The file is not tracked, but is not added to the GIT library, and does not participate in version control. Change from git add to Staged
  • Unmodify:The file has been stored in the library without modification, that is, the file snapshot content in the repository is exactly the same as in the folder. This type of file has two ways to go. If it is modified to Modified, if you use git RM to move out of the repository, it becomes Untracked file.
  • Modified:The file has been modified only by modification, and no other operations have been carried out. This file also has two locations. Through git add, it can enter the temporary staged state, and use git checkout to discard the modification and return to the unmodify state.This git checkout takes the file from the library to overwrite the current modification.
  • Staged:Temporary storage. Executing git commit will synchronize the changes to the library. At that time, the files in the library will become the same as the local files, and the files will be Unmodify. Execute git reset HEAD filename to cancel temporary storage. File status isModified

3.2 View the state of the file

Throughgit status  The command can view the state of the file:

# View the state of the specified file
git status [filename]

# View all the state of the file
git status

README File status is Untracked (not tracking), prompted by git add can be temporarily stored.

3.3 Add files and directories

Working Directory is the directory you can see in your computer.

The version Library (Repository) workspace has a hidden directory.Git, which is not a workspace but a repository of Git. There are many things in the repository, the most important of which is called stage (or index).Temporary storage area,There is also the first branch master that Git automatically created for us, and a pointer to master called HEAD.

Throughgit add  The command can add untracked status files to the temporary storage area:

# Add the specified file to the temporary storage area
git add [file1] [file2] ...

# Add the specified directory to the temporary storage area, including the subdirectory
git add [dir]

# Add all the files in the current directory to the temporary storage area
git add .

3.4 Remove files and directories (revoke add)

Throughgit rm  The command will delete files directly from the temporary storage area without changing the workspace.

# Delete files directly from the temporary storage area without changing the workspace.
git rm --cached <file>

3.5 Look at the difference after the file is modified

Throughgit diff  The command can display differences between files in the workspace and files in the temporary storage area:

git diff [files]

—aRepresents the previously modified file, and +++b represents the modified file.

# Compare the files in the temporary storage area with the files that have been submitted before.
git diff --cached

3.6 Submission

Throughadd  The command simply adds files or directories to the index temporary storage area.commit  The command can submit files to the local repository in the temporary storage area:

# Submitting the temporary storage area to the warehouse area
git commit -m [message]

# Submit the designated file of the temporary storage area to the warehouse area
git commit [file1] [file2] ... -m [message]

# The submission of workspace changed from last commit to the warehouse area, skipping add, and invalid to the new file.
git commit -a

# All diff information is displayed at the time of submission
git commit -v

# Use a new commit instead of the last submissionIf the code does not have any new changes, it is used to rewrite the submission information of the previous commit.
git commit --amend -m [message]

# Redo the previous commit and include new changes to the specified file.
git commit --amend [file1] [file2] ...

The temporary storage area has no README file and is submitted successfully.

Four. Remote warehouse

Git A distributed version control system, the same Git warehouse, can be distributed to different machines, but the development participants must be in the same network and have to have a original version of a project. The usual way is to let a computer act as a server, open 24 hours a day, and everyone elseThe “server” warehouse cloned one to its own computer and pushed the respective submissions to the server’s warehouse and pulled the submissions from the server’s warehouse. It is entirely possible to build a server running Git, but now it is more appropriate to use a free hosting platform, such as Gi.Thub.

At the same time, compared with the traditional code, it is managed to the local or internal network. Once the machine or internal network machine fails, the code may be lost, and using the remote code repository will always have a backup. At the same time, it also eliminates the cumbersome building of local code version control service. Git in the age of cloud computing is powerfulThe branch and clone functions make it easier for developers to collaborate remotely.

4.1 Create SSH Key

The first step

In the user’s main directory, see if there is a.Ssh directory, if so, see if there are two files of id_rsa and id_rsa.pub under this directory, and if there is already, you can jump directly to the next step. If not, the creation is:

ssh-keygen -t rsa -C "1297441127@qq.com"

Then all the way back, if everything goes well, you can find the.Ssh directory in the user’s main directory. There are two files of id_rsa and id_rsa.pub. These two are the secret keys of SSH Key, and id_rsa is a private key.To go, id_rsa.pub is a public key and can tell anyone confidently.

Second steps

Login Github, open the SSH and GPG keys settings page, click New SSH key, fill in any Title, paste the id_rsa.pub file content in the Key text box,

Why does GitHub need SSH Key? Because GitHub needs to recognize that the submission you push is really your push, not someone else’s impersonating, and Git supports the SSH protocol, so GitHub just knows your public key.To confirm that only you can push it.

Of course, GitHub allows you to add more than one Key. Assuming you have a number of computers, you submit it at the company and submit it at home in a moment, and you can push the Key on each computer as long as you add the Key to the GitHub.

4.2 Submitting the source code to a remote warehouse

There are two kinds of addresses to create remote warehouses:

HTTPS(Recommended for lightweight users.

Use encrypted web access channel to read and write storehouse, use user name and password to authenticate.

SSH(Recommend senior users or often push large files to users.)

SSH The full name Secure SHell is a network protocol. As the name implies, it is a very secure shell, which is mainly used for encrypted transmission between computers. Using encrypted channel to read and write storehouse without single upload restrictions, we need to set up the account SSH public key to complete pairing verification.

At present, this test warehouse on GitHub is still empty, and GitHub tells us that a new repository can be cloned from this repository, a local warehouse can be associated with it, and then the contents of the local warehouse are pushed to the GitHub warehouse. presentIn execution:

# Add remote host, host name is origin, address is git@github.com:xuliangTang/test.git
git remote add origin git@github.com:xuliangTang/test.git

After adding, the name of the remote library is origin, which is called by default by Git, or can be changed to something else.

Next, you can push all the contents of the local library to the remote library:

# The local master branch is pushed to the origin host, at the same time, origin is designated as the default host, and then git push can be added without any parameters.uParameter specifies a default host
git push -u origin master

Since the remote library is empty, we first pushed the master branch and added it.-u  Parameters, Git will not only push the remote new master branch of the local master branch content, but also associate the local master branch with the remote master branch, and can simplify the command in the future push or pull.

From now on, as long as local submissions are made, we can pass the order:

git push origin master

Push the latest changes to the local master branch to GitHub.

When you first use Git’s clone or push command to connect to GitHub, you will get a warning:

This is because Git uses a SSH connection, and when the SSH connection is the first to verify the Key of the GitHub server, you need to confirm whether the fingerprint information of the GitHub’s Key is really from the GitHub server.yes  You can return to the car.

Git It will output a warning to tell you that you have added the Key of GitHub to a trust list on the machine.

This warning will only appear once, and there will be no warning in the later operation.

If you are really worried about someone posing as a GitHub server, you can compare the fingerprint information of GitHub’s RSA Key with SSH connection before entering yes.

4.3 Common operation instruction

# Download all changes in the remote warehouse
git fetch [remote]

# Display all remote warehouses
git remote -v

# Display the information of a remote warehouse
git remote show [remote]

# Add a new remote warehouse and name it
git remote add [shortname] [url]

# Retrieves changes in remote repositories and merges with local branches
git pull [remote] [branch]

# Uploading a local specified branch to a remote warehouse
git push [remote] [branch]

# Force the current branch to the remote repository, even if there is conflict.
git push [remote] --force

# Push all branches to remote warehouses
git push [remote] --all

#Simply view the remote -- all the warehouses
git remote  (Only look at the name of the remote warehouse.#View a single warehouse
git remote show [remote-branch-name]

#New remote warehouse
git remote add [branchname]  [url]

#Modify remote warehouse
git remote rename [oldname] [newname]

#Delete remote warehouse
git remote rm [remote-name]

#Getting remote warehouse data
git fetch [remote-name]  # Gets all updates from the warehouse, but does not automatically merge the current branches.
git pull  # Gets all updates from the repository and automatically merges them to the current branch#Upload data, such as git push origin master
git push [remote-name] [branch]
git fetch command

Once the repository of the remote host has been updated (the Git term is called commit), it is necessary to retrieve these updates locally.git fetch  Order:

git fetch <Remote host name >

git fetch Commands are usually used to view other people’s processes, because the code they retrieve does not affect your local development code.

By default, GIT fetch retrieves updates from all branches (Branch). If you want to retrieve updates from a particular branch, you can specify the Branch Name:

git fetch <Remote host name > < branch name >

 

The retrieved updates are used on the local hostRemote host name / branch name  The form reads. For example, the master of the origin host is to be usedorigin/master  Read. As compared to the differences in local warehouses:

git diff master origin/master

 

After retrieving the update of the remote host, it can be used on the basis of it.git checkout  The command creates a new branch:

git checkout -b newBrach origin/master

In addition, it can also be usedgit merge  Command orgit rebase  Command to merge remote branches locally on the current branch:

git merge origin/master
# perhaps
git rebase origin/master
git pull command

 git pull  The function of the command is to retrieve the update of a branch of the remote host and merge it with the local specified branch.

git pull <Remote host name > < remote branch name > < local branch name >

If the remote branch is merged with the current branch, the following part of the colon can be omitted:

git pull origin master

The above command indicates that the origin/master branch is retrieved and merged with the current branch. In essence, this is equivalent to doing git fetch first and then doing git merge.

Five, Git branch

Suppose you are going to develop a new feature, but it takes two weeks to complete, and the first week you write 50% of the code, and if you submit it immediately, the incomplete code library will cause others to be unable to work because the code is not finished. If the code is written all over again, there will be a huge risk of losing the progress every day..

Now that there is a branch, you don’t have to be afraid. You create a branch of your own, you can’t see it, and you continue to work normally on the original branch, and you work on your own branch, you want to submit it and submit it, until the end of the development, and then merge into the original branch, so that it is safe and no shadow.Sound someone else’s work.

5.1 basic operation

1)View the branch

 git branch  The command will list all branches, and a front number will be marked in front of the current branch.-r  Parameters view the remote branch,-a  Parameters look at all the branches:

2)Create and switch branches

 git checkout  Order to join-b  The parameter represents the creation branch and switches:

git checkout -b dev

It is equivalent to the following two orders:

git branch dev
git checkout dev  # If name is -, it is the last branch

When executing git checkout

3)Merge branch

 git merge  The command can be merged with the specified branch to the current branch:

git merge dev
4)Deleting branch

 git branch  Order to join-d  The parameter indicates that the specified branch of the local warehouse is safely deleted.-D The parameter is forced to delete:

git branch -d dev
git branch -D dev

 git push  Order to join–delete  The parameter represents the deletion of the specified branch of the remote warehouse:

git push origin --delete dev
# Equate to
git push origin :dev

 

Reference resources

《One hour to learn Git: http://www.cnblogs.com/best/p/7474442.html

《GitTutorial: https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

Link of this Article: A preliminary study of Git

Leave a Reply

Your email address will not be published. Required fields are marked *