This article is from a small white use git, haha, also recommended to everyone white, we progress together! Big gods don’t feel too simple oh, small white people are trying to run towards you, one two three four, one two three four….. Anyway, back to the point, I wrote this article because: I recently found that my master of Git is really too shallow, just like a dragonfly. I also take this opportunity to look at Git from a small point of view, and make a preliminary exploratory understanding of Git. The most important thing is to meet our usual development, if there is any mistake in the article, I hope you will leave a message to inform me! Thank 🙏.

This article first from the actual combat point of view to explain, involving the principle of the problem, please wait for xiaobai advanced, then issued, please look forward to oh! 😁

One, understandgit

Git is an open source distributed version control system that can handle project version management from very small to very large projects efficiently and quickly. —- from Baidu Baike

So let’s understand it and analyze it from several aspectsgitWhat conveniences are provided in our work:

1. History of version Control:

Usb disk -> Paid (CVS) -> Open Source (SVN) -> Distributed (Git)Copy the code

2. Advantages:

A. Convenience: unified warehouse management, submission code, quick and convenient pulling code B. Traceability: each submission is recorded and the historical version can be traced c. Merge conflicts: conflicts can be merged to keep the content of developers unified d. High security: online main warehouse + local warehouse, high securityCopy the code

3. svngitThe difference between

SVN: Centralized: only one central server with the latest version Git: distributed: Each client is a server and can have many latest versionsCopy the code

Here are two sketches to help you understand:


  1. The first issvn, relatively fewer operation paths, easier to understand:

  1. Next up is oursgitThat follows the picturesvnThere are more operable paths:

4. About the project hosting platform

There are many storage libraries in the market, such as: Code Cloud, Github, GitLab, etc

Github: belongs to the open source code library, it is the most popular open source project hosting platform at present. It provides both public and private repositories, but if you use private repositories, you need to pay. Gitlab: you can create a private free warehouse on it, which is mostly used by enterprisesCopy the code

Create a new project in the warehouse

Create a project on Github.

Github website address

The registration/login step is skipped here. Please register yourself.


1. Once you’ve logged in, click next to your profile picture in the upper right corner+Number, select the last itemNew projectCreate a new project.

2. Next, fill in the information about the project.

3. The system automatically generates oneREADME.mdFile, this file is mainly to store the description of the project information, download mode, running information, file directory and so on.

Does it feel very simple, in fact, not hard oh! 😄


Three, finish the simplestgitoperation

Now that we have our own repository, we’ll try to simulate a series of simple actions for Clone line code, local development, and commit.

1. Check the Git version

Viewing the Version Number

git --version
Copy the code

2. Git help

Here is git view command help on how to download a project, save, commit, merge branches, switch branches, view history, etc.

git help
Copy the code

Git clone

This command is used to pull the contents of a remote repository locally from a remote location and is a common command used to learn about project operations.

Git cloneCopy the code

Example: for example, clone the iView version library, (in this to my old company’s product quietly hit an advertisement! 😄 ha ha)

Select the folder location you think is appropriate for it, and then execute the following command:

git clone https://github.com/iview/iview.git
Copy the code

This command generates a folder on the local host with the same name as the repository on the remote host. If you want to specify a different directory name, you can use the directory name as the second parameter to the git clone command, as follows:

Git clone < repository name >Copy the code

Download complete.

4. Git status: Check the git status

Make it a good habit to check the status of your current code by executing git status every time you commit to save code or start git operations.

Of course, after executing the command, several states need to be explained respectively:

  1. The current working status is no modification and no submission is required

On branch master Your branch is up to date with 'origin/master'. Nothing to commit, working tree cleanCopy the code
  1. The current workbook has changed

Add 'XXXX' (commit) On branch master Your branch is up to date with 'origin/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: No changes added to commit (use "git add" and/or "git commit -a")Copy the code
  1. After performing temporary storage

On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." // (unstage) modified: SRC /view/index.uxCopy the code
  1. When adding a new file

On branch master Your branch is up to date with 'origin/master'. Untracked files: (Use "git add <file>..." to include in what will be committed) src/view/test.txt nothing added to commit but untracked files present (use "git add" to track)Copy the code
  1. When the new file is done staging

On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." To unstage) new file: SRC /view/test.txtCopy the code

Git add: Add to staging

We also used this command in the last lecture, so what does this command mean? For example, when we go to the supermarket, we usually push a shopping cart, put the items we need to buy into the cart, and then pay together at the checkout time. This command is like adding to the shopping cart. After the requirements are developed, the code to be submitted is submitted to the staging area before it can be submitted upwards.

I’ve been talking about staging, but how do I perform staging?

Just follow this down to add temporary storage:

There are two forms:

  1. The first is to temporarily store a specified file,addBack address, directly fromgit statusJust copy the red content of the
git add src/view/test.txt
Copy the code
  1. The second option is to temporarily store all local files, adding all files under the current directory (recursive subdirectory) to the temporary store.

Add followed by a.(dot)

git add . 
Copy the code

6. Git commit: commit to local repository

After we commit our local changes to staging, we then commit our staging to the local repository

Git commit -mCopy the code

1 file changed,1 insertion(+) // 1 files modified 1 files added create mode 100644 SRC /view/test.txtCopy the code

We have committed the file to our local repository, and then run git status again to check the status. We have a commit that can be pushed to a remote repository:

Git push origin: commit to remote repository

In the above process, we have committed the code to the local repository, and now we need to push our local repository to the remote repository.

git push origin 
Copy the code

Let’s take a lookgithubThe warehouse already has us justpushThe record of

Force local changes to override the remote repository version if this command is notgitOld hands, try not to use! Don’t use it! Don’t use it!

git push origin -f 
Copy the code

At this point we seem to have covered a simple submission (submission to remote), but we are all talking about the process of one person operating on a project, and we will share the process of multi-person cooperation in the future.

Iv. Multi-person cooperation operation process

Of course, we can not just a person in the development of their own, more of the time is the collaboration of many people, that most of the git operation mistakes are based on the operation of many people, we first explain the more commonly used commands, and finally the overall process.

1. Git remote: Operations on remote repositories

In the development process, we will have different numbers of remote warehouses for different situations. In general, there will be two, one is our own remote warehouse and the other is the remote warehouse of the company’s project.

Their remote warehouse Origin: used to store their own temporarily do not need to test and online code; Company remote repository upstream: code to submit in preparation for a test launchCopy the code

View all remote warehouses

git remote -v 
Copy the code

Adding a remote repository

Git remote add origin > git remote add upstream >Copy the code

Here are two warehouses that I have added

Why add a remote repository? My understanding is that, when I have two computers, one in the company, a home, I in the company’s development to a half, also cannot be submitted to the online test, I want to go home and continue to develop, then you can add a remote branch, the company will be submitted to the remote code, home remote can pull synchronization code. Therefore, we generally need to fork the corresponding project to our own name and then add a remote library of our own after the company gives us git permission.

Git checkout

Depending on the environment and requirements, we typically have multiple branches to separate blocks of code

Switch to another branch

Git checkout < branch name > => Git checkout qa // Switch to qa branchCopy the code

Pull the latest code directly from the line and create a new branch. For example, the branch on your line is called production:

Git checkout -b < new branch name > => git checkout -b Feature-xxx // It will automatically switch to the new branch after it is createdCopy the code

3. Git fetch

Synchronizing remote code

1. Run the following command to retrieve all updates on a remote host

Git fetch < remote hostname >Copy the code

eg:

Upstreams: Pull all updates to the corresponding branch, QA -> QA production->production

git fetch upstream 
Copy the code

2. Retrieve updates for a particular branch

Git fetch < remote host name >Copy the code

eg:

Pull to the Production branch of the upstream host

git fetch upstream production
Copy the code

4. Git merge: Merge the specified branch into the current branch

Merges the code for the specified branch into the current branch

Git merge [branch of new code, i.e., specified branch]Copy the code

eg:

Suppose we are now in the QA branch and need to merge the new code developed in the feature-classdetail branch into the Production branch

git checkout production

git merge feature-classDetail
Copy the code

Git merge –abort: Abort the current git mergemergeoperation

When multiple operators cannot avoid modifying the same file, the same piece of code, then code conflicts will occur. When a conflict occurs, our first reaction is to resolve the conflict, of course, but sometimes after the merge operation, we realize that we changed the wrong code, or want to undo the merge, etc. This command is only applicable to cancel a merge that has code conflicts after the merge operation is executed, but cannot cancel a normal merge that does not have conflicts.

Let’s simulate a conflict:

A branch of A:

Branch B:

Now merge branch A into QA branch:

git checkout qa

git fetch upstram && git reset --hard upstream/qa

git merge A

Copy the code

Now let’s merge branch B into the QA branch again and do the same thing as above. Let’s look at the collision page:

At this point we can use our merge abort command:

git merge --abort

Copy the code

After you cancel the merge, the page reverts to the contents of merged branch A

6. Git pull: Synchronize and merge remote code

In fact, the internal implementation of git pull is to merge the remote repository using git fetch, but the prerequisite is to temporarily store the local code first. Otherwise, if the remote repository changes the same file location, it will be overwritten by the remote code. Git fetch +git merge git fetch +git merge

If you have made some changes to your workspace locally, you know that you are going topullDo not use the content in your workspacepullThis command is honest and practicalgit fetchgit mergeTo synchronize and merge.

Git pull < remote repository > < remote branch >:Copy the code
git pull === git fetch + git merge
Copy the code

eg:

Fetch is performed, and then the corresponding remote branch merge is merged into the current branch

git pull 
Copy the code

eg:

Fetch is performed and the corresponding remote repository Origin branch merge is merged into the current branch

git pull origin 
Copy the code

Git reset: discard changes

Resets workspace, discards existing changes, discards staging area and workspace for all file changes

git reset --hard
Copy the code

All local changes are discarded and mapped to a branch of a host

Git reset --hard < host >Copy the code

eg:

Discard all local changes and update the code to the QA branch of upstream

git reset --hard upstream/qa
Copy the code

8. Git Stash: Saves all uncommitted changes to the stack for subsequent restoration of the current workspace contents

This is the command I use a lot. In our work, most of us need to iterate quickly, and there will be an iteration with multiple irrelevant requirements, plus the need to wait for the backend students. At the beginning of an iteration, I tend to develop multiple requirements in multiple branches, so as to ensure that the rollout does not affect each other.

Above 👆 is the background, below 👇 is the usage scenario:

So how do multiple branches store our code? For example, I now have three branches: A, B and C. I first developed A requirement, but the interface is not yet out. At this time, we can save the code of A, and then continue to develop the requirement of B….

A. Basic use

When we only have a small change and just store it temporarily, we can use the following command directly. Of course, it can save not only once, but many times

git stash
Copy the code

B. View the saved list

Above we temporarily stored the content that we did not commit for the time being. Use the following command to view the saved list

git stash list
Copy the code

C. View the changes in the list

Git stash show stash@{X}

git show stash@{0}
Copy the code

D. Apply the application to the current workspace

Now that we know which version we have modified, we need to restore it to the workspace with git apply stash@{X}. X is still the serial number of the list.

git apply stash@{0}
Copy the code

E. Define a name for the remarks

In the above operation, we use the default storage, to restore, also need to check the changes, it is a bit troublesome, so this time we will add a note for each saved version, so that we can directly select the corresponding version when restoring.

Git Stash Save 'e'Copy the code

Now let’s look at our list again:

In the list above, the last one is the latest version we added, using our custom remarks name.

5. Other functions/commands

We use fewer, but more practical commands (or commands that we use when we have a problem)

1. View historical git submission records

Sometimes we want to restore the last commit. When rolling back code, we usually look at the commit history and find the corresponding number to roll back

Git log is used to view historical commit records. Deleted commit records cannot be viewed

git log 
Copy the code

Git reflow allows you to view all operations on all branches (including deleted commit records and reset operations).

git reflog
Copy the code

2. Modify the commit remarks

git commit --amend
Copy the code

Press I to enter the editing mode, press Esc to save the editing mode, and :wq to exit and edit the mode

After editing, you can see that the modification is successful:

To view the historical submission list:

3. Undo the most recentcommitCommit, often used for code rollback

Sometimes, after committing a COMMIT, you realize that you committed the wrong commit and want to undo the commit and revert to the last one, using the following command

Here is the commit record before the commit:

Then change something and commit it. Here’s the updated commit record:

Then execute the following command to undo the latest commit:

git reset --hard HEAD^
Copy the code

After executing, look at our updated commit record:

If you want to revert to a historical commit version instead of the latest one, record the commit number:

git reset --hard d9dc2c6ff6a3f4eef3f11bd83e259fcf19a36f69
Copy the code

Here is the commit record after the rollback:

4. Delete the specified COMMIT

Be sure to recognize the risks and carefully delete

Git rebase -i HEAD~3 git rebase -i HEAD~3Copy the code

Enter I to enter the editing page, change the “pick” before “commit” to “drop”, and exit the editing page with “esc” and “:wq”.

After the modification is successful, the following message is displayed:

Successfully rebased and updated refs/heads/xxxxxx.

Copy the code

The git log does not record the commit.