The author | su xiaoxiao

Edit | Wang Jiu one

Net source | for class

Introduction: Because the tutorial is detailed, so the text is a little long, new hand to see the side operation effect is beyond your expectations. GitHub has had a bit of a makeover, but no big deal. Please pay attention to the wechat public account “Web Project Gathering Place”

What is Git?Git is the most advanced distributed version control system in the world. Working Principle/process:Workspace: Workspace Index/Stage: temporary storage Repository: warehouse area (or local warehouse) Remote: Remote warehouse

What are the main differences between SVN and Git?

The SVN is a centralized version control system. The version repository is centrally located on the central server. When you work, you use your own computer, so you need to get the latest version from the central server first, and then work. The centralized version control system must be connected to the Internet to work. If it is ok on the LAN, the bandwidth is big enough and the speed is fast enough, if it is slow on the Internet, it will be confused.

Git is a distributed version control system, so there is no central server. Everyone’s computer is a complete version library, so there is no need to connect to the Internet to work, because the version is on their own computer. Since everyone’s computer has a complete library, how can multiple people collaborate? For example, if you change file A on your computer and someone else changes file A on your computer, you can see each other’s changes only by pushing your changes to each other.

How to install Git on Windows?

Msysgit is a Version of Git for Windows.You need to download one from the Internet and install it by default. After the installation is complete, go to the Start menu and find “Git –> Git Bash” as follows:A similar command window will pop up, indicating that Git has been installed successfully. As follows:

After the installation is complete, you need to enter the following command line:

Because Git is a distributed version control system, you need to fill in the username and mailbox as an identification.

Note: the git config –global parameter indicates that all git repositories on your machine will use this configuration, although you can also specify a different username and mailbox for each repository.

Four: How to operate?

One: create the repository.

What is a repository? A repository is a directory in which all files are managed by Git. Git can track every change or deletion of a file so that it can be traced at any time in history or “restored” at any point in the future.

So create a version of the repository is very simple, as follows: I am D – > WWW directory to create a testgit repository.

The PWD command is used to display the current directory.

To make this directory a git managed repository, run the git init command as follows:

At this time your current testgit directory will have a. Git directory, this directory is git to track the management version, nothing do not hand upheaval to change the files in this directory, otherwise, the Git repository will be destroyed. As follows:

Let’s take a look at the following demo:

I created a new notepad file readme. TXT in testgit directory: 11111111

Step 1: Use the git add readme.txt command to add to the staging area. As follows:If, as above, there is no hint, it has been added successfully.

Step 2: Tell Git to commit the file to the repository with the git commit command.Git status = “git status” git status = “git status” git status = “git status”‘git status’ =’ 22222222 ‘; ‘git status’ =’ 22222222 ‘;The above command tells us that the readme.txt file has been modified, but the changes have not been committed.

Add the file to the repository.

Must first clear, all of the version control system, can track the change of a text file, such as TXT file, page, all the procedures of code, etc., and such is Git, changes in the version control system can tell you every time, but the pictures and video of these binary file, although can also can be managed by a version control system, but not the change of the trace file, You can only string together each change to the binary, so you know that the image changed from 1KB to 2KB, but version control doesn’t know exactly what the change was.

Next, I want to see what changes have been made in the readme.txt file. You can run the following command:

Git diff readme.txtAs you can see above, the readme.txt file changed from one line of 11111111 to two lines and added one line of 22222222.

Now that you know what changes have been made to the readme.txt file, you can commit to the repository in the same 2 steps as committing the file (git add and Git Commit).

As follows:2. Version rollback:So now that we’ve learned how to modify files, I’ll go ahead and add another line to the readme.txt file

The content is 33333333333333. Run the following command:

Now that I have made three changes to the readme.txt file, I want to check the history. How do I do that? We can now use the git log command as shown below:The git log command displays the latest to furthest display logs. We can see the last three commits, and the latest one is added to 333333. Git log-pretty =oneline git log-pretty =oneline git log-pretty =onelineNow I want to use a version rollback operation. I want to roll back the current version to the previous version. What command do I use? Git reset –hard HEAD^ So to go back to the previous version, just change HEAD^ to HEAD^^ and so on. Git reset –hard HEAD~100: git reset –hard HEAD~100 The contents of the readme.txt before the rollback are as follows:To roll back to the previous version, run the following command:

To view the contents of readme.txt, run the cat readme.txt command

As you can see, the content has been rolled back to the previous version. Git log = git log = git log

We see the addition of 333333 content that we didn’t see, but now I want to go back to the latest version, such as: how do I restore the 333333 content? To roll back the version number, run the following command:

Git reset –hard version number, but now the problem is what if I have already closed the command line once or I don’t know the version number of 333 content? How to know the version number to add 3333 content? To obtain the version number, run the following command: git reflog

According to the above display, we can know that the version number of adding 3333 is 6fCFC89. We can now order

Git reset –hard 6fcfc89 restores. Here is a demonstration:

You can see that it’s the latest version.

Three: Understand the difference between workspace and staging area? Workspace: the directory you see on your computer, such as files in testgit (except the.git hidden directory repository). Or later need to create a new directory files and so on belong to the workspace category. Repository: Your workspace has a hidden directory. Git, this is not part of your workspace, this is your Repository. The repository contains many things, the most important of which is the stage. Git automatically creates the first branch, the master, and a pointer to the master, the HEAD.

As mentioned earlier, there are two steps to committing a file to the repository using Git:

The first step is to use Git Add to add the file, in effect, to the staging area.

Step 2: Commit your changes using Git Commit, which essentially commits everything in the staging area to the current branch.

Let’s continue to use the demo to demonstrate:

‘git status’ =’ git status’; ‘git status’ =’ git status’;

Git add = git status = git add = git status = git add = git status = git add = git status

Then you can commit to the branch once using git commit, as follows:

Four: Git undo modify and delete file operations.1: undo the modification: for example, I now add a line in the readme. TXT file 555555555555, we first through the command to see the following:Before I submitted, I found that adding 55555555555 was wrong, so I have to restore the previous version immediately. Now I can make changes in the following ways:

First: If I know what to delete, I manually delete the files I need, then add them to the staging area, and commit them.

Second: I can go straight back to the previous version the way I did before. Use git reset –hard HEAD^

But now I don’t want to use either of the above methods, I want to use the undo command directly how to do it? Git status = git status = git status As follows:

Git checkout — file: Git checkout — readme.txt: Git checkout — readme.txt

Git checkout –readme.txt = git checkout –readme.txt = git checkout –readme.txt = git checkout –readme.txt = git checkout –readme.txt = git checkout –readme.txt = git checkout –readme.txt

1. After the automatic modification of readme.txt, it has not been put into the staging area, and the use of undo changes will return to the same state as the repository. 2. The other is that the readme.txt is already in the staging area, and then the modification is made. Undo the modification and return to the state after the staging area was added. For the second case, I want to continue the demo. Now, if I add a line to readme.txt 66666666666, I add git add to the staging area, then add 7777777, I want to undo the command to get it back to the staging area. As follows:

Note: the — in the git checkout — readme.txt command is important, if there is no — then the command changes to create branch.

Two: Delete files. Suppose I add a file b.tb to the testgit directory of the repository and commit it. As follows:

If you want to delete the file from the repository, run the rm b. TB command. If you want to delete the file from the repository, run the commit command.

What if I want to restore the file in the repository before committing?

You can use the git checkout — b.txt command as follows:

Take a look at our testgit directory and add three files. As follows:

Five: remote warehouse. Before you know it, sign up for a Github account. Since transfers between your local Git repository and your Github repository are encrypted via SSH, you need a little setup: Step 1: Create an SSH Key. In the user’s home directory, check whether there is an. SSH directory. If there is, check whether there are id_rsa and id_rsa.pub files in this directory.

Ssh-keygen -t rsa -c “[email protected]

Id_rsa is the private key and cannot be leaked, while id_rsa.pub is the public key and can be confidently told to anyone.

Step 2: Log in to Github, open “Settings” SSH Keys page, then click “Add SSH Key”, fill in any title, paste the contents of the id_rsa.pub file in the Key text box.

Click Add Key and you should see the added Key.

How do I add a remote library? Here’s the situation: having created a Git repository locally, we want to create a Git repository on Github, and we want to synchronize the two repositories remotely so that the Github repository can be used as a backup and others can collaborate through the repository.

To start, log on to Github and find “Create a new Repo” in the upper right corner to create a new repository. As follows:

Enter Testgit in the Repository name and keep everything else default. Click the “Create Repository” button to successfully Create a new Git Repository:

Currently, the TestGit repository on GitHub is empty. GitHub tells us that we can clone a new repository from the repository, or associate an existing local repository with it, and then push the content from the local repository to GitHub.

Copy the code

Now, follow GitHub’s prompts and run the following command in your local testgit repository:

git remote add origin https://github.com/tugenhua0707/testgit.gitCopy the code

All are as follows:

Git push = git push = git push = git push = git push = git push

Git will not only push the contents of the local master branch to the new remote master branch, but also associate the local master branch with the remote master branch. This simplifies the command for future push or pull. After the push is successful, you can immediately see that the content of the remote repository is the same as that of the local repository on the Github page. Enter the github user name and password as follows:

From now on, whenever a commit is made locally, you can run the following command:

git push origin master

Now you have a truly distributed repository by pushing the latest changes to the local Master branch to Github.

  1. How do I clone from a remote library?

    Above we saw how to associate a remote library with a local library first and a remote library later.

    Now we’re thinking, if there’s something new in the remote library, and I want to clone it locally, how do I clone it?

    First, log on to Github and create a new repository called TestGit2. As follows:

Here we see:

Now that the remote library is ready, the next step is to clone a local library using the git clone command. As follows:

Testgit2 = testGit2 = testGit2 = testGit2 = testGit2

Create and merge branches. In version backfill, you already know that every commit, Git strings them together into a timeline, and that timeline is a branch. As of now, there is only one timeline, and in Git this branch is called the master branch. HEAD technically doesn’t point to commit, it points to master, and master points to commit, so HEAD points to the current branch.

First, let’s create the dev branch, and then switch to the dev branch. Do as follows:

Add -b to the git checkout command to create and switch. It is equivalent to the following two commands

git branch dev

git checkout dev

Git branch Look at branches. All branches are listed, and the current branch is preceded by an asterisk. Then let’s continue the demo on the dev branch. For example, let’s add another line 7777777777777 to readme.txt

Let’s look at the readme.txt content and add 77777777 as follows:

Now that the dev branch is complete, let’s switch to the master branch and continue to view readme.txt as follows:

Git merge dev = master; merge dev = master;

The git merge command is used to merge a branch into the current branch. After merging, you can view the readme. TXT file.

Note the fast-forward information above, Git tells us that the merge is in “fast-forward mode”, i.e. the merge is directed directly to the current commit of dev, so the merge is very Fast.

After the merge is complete, we can delete the dev branch as follows:

The create and merge branch command is summarized as follows:

Check out the branch: git branch

Create branch: git branch name

Git checkout name

Git checkout -b name

Enter git merge name to merge a branch

To delete a branch, run git branch -d name

How to resolve conflicts? Create a new branch, for example, fenzhi1, add a line 8888888 to readme.txt, and commit, as shown below:Similarly, we now switch to the master branch and add 99999999 to the last line as follows:

Now we need to merge FenZHI1 on the master branch as follows:

Git use <<<<<<<, ======= and >>>>>>> to mark the contents of different branches, where <<<HEAD refers to the contents modified on the main branch, >>>>>fenzhi1 refers to the contents modified on fenzhi1, we can modify the following after saving:

If I want to see the branch merge, I need to use the git log command. The command line is shown as follows:

3. Branch management policies. Git uses the “Fast forward” mode when merging branches. In this mode, when deleting a branch, the information about the branch is lost. Now disable the “Fast forward” mode with the -no-ff parameter. First, let’s do a demo:

Create a dev branch. Example Modify the contents of readme.txt. To the staging area. Switch back to the master branch. To merge the dev branch, run the git merge — no-ff -m “comment” dev command.

Branching strategy: First of all, the master branch should be stable, that is, it should be used to release new versions, and it should not be allowed to work on it. Normally, work should be done on the new dev branch, and when the new dev branch is ready to release, or the dev branch is stable, it can be merged into the master branch.

In Git, branches are very powerful. Every bug can be fixed by a temporary branch. After the repair is complete, merge the branches and delete the temporary branch.

For example, if I receive a 404 bug in development, we can create a 404 branch to fix it, however, work on the current dev branch is not committed yet. For example:

It’s not that I don’t want to submit, but in the middle of the work, we can’t submit. For example, my branch bug took 2 days to complete, but my issue-404 bug took 5 hours to complete. What to do? Fortunately, Git also offers stash functionality that allows you to “stash” your current work site so you can work on it later when the site is restored. As follows:

So now I can fix the bug by creating an issue-404 branch.

First we need to make sure that we fix the bug on that branch. For example, I now fix the bug on the master branch. Now I will create a temporary branch on the master branch.

Once the fix is complete, switch to the Master branch, complete the merge, and finally remove the issue-404 branch. Here is a demonstration:

Now we’re back to work on the dev branch.

The work area is clean, so where do we go for the work site? You can check this out using the git stash list command. As follows:

The work site is still there, Git has stash contents somewhere, but there are two ways to restore them:

1. Restore git stash apply. After the restore, the stash contents are not deleted. 2. Another way is to use git Stash Pop and remove the stash contents as you recover. Demonstrate the following

Eight: collaboration. When you clone from a remote repository, Git automatically matches the local master branch to the remote master branch, and the default name of the remote repository is Origin.

Git remote -v: git remote -v: git remote -v: git remote -v

1. Push branch:

Git push origin master: Git push origin master: Git push origin master: Git push origin master

For example, my current github readme.txt code looks like this:

The local readme.txt code is as follows:

Now I want to push the locally updated readme.txt code to the remote library using the following command:

Github readme.txt:

You can see that the push is successful. Now if we want to push to another branch, such as the dev branch, we will use the same git push origin dev command

So in general, which branches are pushed?

The master branch is the primary branch, so it must be synchronized with the remote branch at all times. Some bug fixing branches do not need to be pushed to remote. They can be merged into the main branch and then pushed to the master branch. Two: Grab the branch:

When multiple people work together, everyone pushes their changes to the Master branch. Now we can simulate another colleague, either on another computer (be sure to add the SSH key to Github) or on another directory clone on the same computer by creating a new directory named TestGit2

But first I need to push the dev branch to remote as well, as follows

Then go to testgit2 and clone the remote library to the local directory as follows:

Now the generated directory looks like this:If you want to create a branch of origin, you need to create a branch of origin. If you want to create a branch of origin, you need to create a branch of origin.

Git checkout — b dev origin/dev

Now you can work on the dev branch and push it to the remote library.

As follows:

I’ve already pushed the commit to the origin/dev branch, and I’ve changed the same file to the same place in my directory file and tried to push it to the remote library as follows:

We can see from the above: push failed, because my friend’s latest commit was in conflict with what I tried to push. The solution is also very simple. As indicated above, we should first use git pull to catch the latest commit from origin/dev, then merge it locally, resolve the conflict, and then push it.

Git pull fails because there is no link between the local dev branch and the remote origin/dev branch.

This time, git pull is successful, but merge conflicts need to be resolved manually, and the way to resolve conflicts is exactly the same as in branch management. After that, submit and push: we can look at the readme.txt content first.

Now it has been solved manually, I need to submit it again and push it to the remote library. As follows:

Therefore: The mode of working collaboratively looks like this:

First, you can try to push your own changes with git push Origin branch-name. If the push fails, you need to use git pull to try to merge the remote branch before your local update. If there are conflicts with the merge, they need to be resolved and committed locally. Git push origin branch-name

Reference:

www.cnblogs.com/tugenhua0707/p/4050072.html

The author | su xiaoxiao

Net source | for class

The original | click here to read the original text

Recommended reading

1. Recommend several original public accounts

2. Use ThreadLocal gracefully

3. Linux Development Plan (6)

4. Wechat Scan code login practice (with code)