Remember before

Git is most commonly used code management tool, in the actual work we will encounter some problems on the git, will make us at a loss, so the summary, the following are common scenarios and the problems in the work, if you want to learn about other aspects of git exotic curiosity-a solution looking can refer to other articles, such as have not welcome pointed out

use

To use Git, you must be in a repository. There are two ways to create a repository

Git clone: git clone: git clone: git clone: git clone: git clone: git clone: git clone: git clone: git clone: git clone: git clone: git clone: git clone: git clone

  • Workspace: Workspace is the area of your cloned code directory where you add and modify code
  • The staging area: the staging area is in your repository. When you use Git Add, Git will put your workspace changes into the staging area
  • Local branch: When you use Git commit, this command commits code from the staging area to a local branch

The first two are understandable, but let’s look at the third, the local branch

Git Clone -b develop github.com/yizhengfeng…

Clone a specified branch of a remote

The local branch

Remote branches are also cloned along with the repository

The red is the remote branch, so what is the green master, which is the local branch that tracks the active branch in the remote repository, and is created by Git itself

Creating a repository

You can use git clone to create a local repository. You can use git init to create a local repository. This command is called git init. If our Git client is github and you create a private repository, see the photo below

Step 1: Git init initializes the repository

Step 2: Git Add. put all changes in the staging area

Git Remote Add Origin XXXXXX

Git push -u Origin master -u origin master -u origin master -u Origin master

!!!!! If the third step accidentally associated with the wrong, you can use the following command to cancel the association

git remote remove origin

Common commands

After introducing some git concepts above, I will now focus on git common commands and scenarios

Git status: Check the status of the workspace

Git Add: Put your workspace changes in the staging area

Git commit -m “: commit changes in the staging area to a branch of the local library

Git branch: Check the current branch

Git checkout: switch branches

Git log: Check the commit records

Scenario 1

One of the most important commands in Git is to go back when you have changed the code and use git add. Later, you find that there is something wrong with the changed file and you want to change it back. At this time, you can directly modify it once and then go to git add. Once is fine, but if you git add. And git commit -m ”; Git reset –hard HEAD^

HEAD: symbolic reference to the current checked out record, which by default points to the latest commit

HEAD^ : ^ means the last one, and this command means the last commit

HEAD~2: ~ lets you specify a number. This command specifies how many times to roll back

–hard: –hard: –hard. Once you add –hard, you can go back to the last commit, but your workspace will have to be changed completely.

Scenario 2

Another important scenario in Git is code rollback. When a bug suddenly appears, but you don’t know when the bug was committed, you can use Git checkout 123AA

Git checkout is usually used to switch branches, but if you use this command on the current branch and specify the hash value for the commit, git checkout separates the HEAD from the current branch (by default, the HEAD is the same as the current branch, so the current branch always has an *). So the HEAD points to the other commit, and your current branch code, which the HEAD points to, can be rolled back using this feature

Scenario 3

There are two types of code merge: One is Git merge Bug, which means that the current branch merges the Bug branch. The advantage of this merge mode is that the Bug branch and the current branch are independent, which can track the history of the commit well, but it will make the code line more complicated. Look at the following diagram

The second way to merge branches is git Rebase. Rebase is essentially taking a list of commit records from the bug branch, “copying” them, and dropping them one by one on the master branch. The advantage of Rebase is that it creates a more linear commit history. If only Rebase was allowed, the commit history of the code base would be very clear. Git rebase master moves the current branch commit to the master branch

The important thing to notice here is that the master branch is not new, bugFix is, unlike the merge, and you need to update the master

git checkout master

git rebase bugFix

Or git rebase bugFix Master

Scenario 4

For example, we have a release branch and a Develop branch, we submit a function on the Develop branch, but the product temporarily says that it also wants to send it, so what should we do? Can we directly copy the Develop code? Can we have a simpler method

git checkout release

git cherry-pick C4

This command is to take a branch commit, and put a copy of it in the current branch, and C4 is the hash value of a commit, and it’s usually just to bring in the other branch commit, remember only the C4 commit

Scenario 5

Whether it’s rollback or code undo, you need to know the hash value of the commit, so you use git log, sometimes it’s a little complicated because it’s all hash, but if the commit message is bad and you don’t know which one it is, then you can use git tag to type a tag, Use to set the ID of a particular submission, quickly switch to this submission

Git tag -a 3.0.8 -m ‘3.0.8 commit ‘

-a: Adds a label

3.0.8: Label name,

-m: indicates the description of the label

Git Tags View all the tags

Git push Origin pushes a local tag

Git Push Origin –tags push all successful tags to remote locations

Remote operation

Now let’s talk about git remote operations

git fetch

This command pulls the remote branch code from the remote repository to the remote branch of the local library, as shown in the following picture

After the git fetch is executed, the O /master branch is updated (this is the local remote branch), but the local master branch is not updated. To update the local master, you need to perform git merge O /master

git pull

Git pull = git fetch + Git merge o/master

git push

This command pushes local changes to the branch of the remote repository, and it’s worth explaining that git push without any parameters depends on a git configuration called pusw. default. The default value depends on what version of Git you’re using, so it’s a good idea to check this configuration before pushing.

Git push

We push to the local master branch without any parameter because git knows which branch is associated with the remote repository (explained below).

Git push 2

If you don’t know, for example, I create a local branch, modify the code, and directly git push, what will happen

“Git push origin source:target” “git push origin :target” “git push origin :target” “git push origin :target” “git push origin :target” “git push origin :target

Source: indicates the local branch

Target: indicates the remote branch

Since you are now in the test branch, you can simply ignore the source.

Git push origin test = git push origin test = git push origin test = git push origin test = git push origin test (Another way is to create it directly on a Git client such as Github and then disassociate it, as explained below.)

confusion

If I push the git branch directly to the local master branch, it will push the code to the remote master branch. Is it because the name of master O /master is similar

The correlation between master and o/master is determined by the branch’s “Remote Tracking” property. The master is set to track O /master — this means that the master branch is given a push destination and a pull and merge destination. You may be wondering how this property is set on the master branch. You didn’t specify this property with any command. Well, Git automatically sets this property for you when you clone your repository. When you clone, Git creates a remote branch (such as O /master) in the local repository for each branch in the remote repository. Then create a local branch that tracks the active branch in the remote repository. By default, this local branch is named master.

Set your own association

The above association is set by git default, so can we set it ourselves? Yes

git checkout -b develop origin/develop

This command creates a local develop branch and associates it with the develop branch of the remote library. This branch will behave as if it were a remote branch. This is how we pull the remote branch, and it will not merge

git branch -u origin/develop develop

This method also creates a Develop branch and associates the Develop branch with the remote library. This is also a way to pull the remote branch without merging it

git fetch origin foo

Git fetch and git pull have the same parameters as git push, but the value of the origin target is reversed. What does the above code mean

He will pull the remote foo branch code to the local remote branch, if there is no local remote repository branch, he will also create a local remote branch.

Git fetch Origin bar will create a local branch o/bar, but you can switch to the Git checkout bar on top of the bar

When you perform git checkout bar, git will automatically create a local branch of the bar you see

Git fetch Origin bar:bar if you want to see it from the start, you have to put the two parameters together

git pull origin target:source

Git pull is the same as git fetch, but it helps you merge it! Merge! Merge!

git pull origin develop:develop = git fetch origin develop:develop + git merge develop

It creates the develop branch locally and will help you merge the develop branch with the current branch. If you operate on the master branch, it will help you merge the develop branch with the master branch

Pay attention to

git pull origin develop = git fetch origin develop + git merge o/develop

When the command is an argument, it creates an o/develop but it also merges, so if you use the git pull command, it will help you merge. Git fetch origin develop