Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use git

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use git, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Region

Before we do that, let's introduce the three areas of git

Work area (working directory)

Temporary storage area (stage index)

Local Historical District (history)

The transformation between them can be understood succinctly and easily through a picture.

Clone

Let's start with the clone command, which is known to anyone who has used git. The git colne command pulls the remote warehouse to the local location. But when we want to pull to the specified folder, you may directly mkdir, in fact, do not need to do so, a command can deal with the git clone remote repository file name, it is as simple as that.

Rm

We may encounter such a situation in our work, using git add. Directly add all the modified files of the workspace to the temporary storage area, but later found that there is a file not to be added, at this point we can use the following command to return the file to the workspace.

Git rm-cached

Stash

In such a case, when you are in development, there is an online emergency bug that needs to be fixed, and the development function is not completed and you do not want to submit, so you can use git stash to store all the files in the workspace. At this time, you can safely cut branches to repair bug, and then execute git stash pop to take out the previously stored records. Of course, there are also some other related commands, such as: git stash list to view stored records, git stash drop to discard stored records.

Tag

Maybe in the process of development, we need to label git tag tagName and push the corresponding tags to the remote warehouse. At this point, we can use the following command to push.

Git push-tags tagName

Amend

When you commit, you find that a file is not added to the last commit, or some files have been modified. You don't want to add new commit information at this time, you just want to add it to the last commit. At this point, you can use

Git commit-amend

Add the files from the staging area to it, and you can also modify the commit information at this time.

Reset

Reset can also achieve the effect of the previous rm, you can use the following command to replace the previous git rm-- cached command

Git reset HEAD

But reset is more versatile, and it can be pulled back to any commit node for operation combined with soft parameters.

Git reset-soft index

After executing this command, you go back to index, the workspace remains unchanged, and the staging area goes back to the index at that time. There is also a hard parameter.

Git reset-hard index

As opposed to soft, its effect is that the workspace is different from the staging area, and it clears the two areas.

Rebase

For rebase means redirection, if your current branch and remote branch commit information is different, it will remind you that you can not do push at this time, you must first pull the remote commit information to the local before you can submit. You can use the rebase command in this case. The following is currently in the develop branch

The rebase command should be executed first at this point

Git fetch git rebase origin/master

After execution, * * then push to the remote master

Git push origin master

In the end, the situation of each branch is the effect of the above picture. If you find the command difficult to remember, you can also use a command to accomplish the above effect.

Git pull-rebase origin master

This is a simple use of rebase and a common command. An optional parameter for rebase, onto, is described below.

-- onto

Usage scenario: during the development process, we will create different branches to develop different functions. When you create a new branch B on branch A to develop functions and also submit some commit, you will find that there is an error commit on the original A branch. If you want to rebase to master, you cannot attach this wrong commit. At this point, it's time for onto to show his magic.

Currently in branch B, to get the above results, simply execute the following command

Git rebase-onto master B

This can be applied not only to different branches, but also to the same branch. Therefore, in view of the above situation, you can only operate on Branch B, and the equivalent command is as follows:

Git rebase-onto B

-- interactive

When we want to change the name of the commit information, if the commit to be modified is *, we can use the

Git commit-amend

If it is not *, we will use the-interactive optional parameter of rebase, which can be abbreviated to-I.

Git rebase-I

The commit hash code after the parameter is the previous commit that needs to be modified. After execution, a similar message appears as follows:

Pick 137cf0a First coommit pick 163dc38 Second commit # Rebase f9aee6e..163dc38 onto f9aee6e (2 command (s)) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out

According to the prompt, we can have 6 optional actions. I believe the hint has made it clear that in our case where we want to modify First coommit, we need to use r.

R 137cf0a First commit pick 163dc38 Second commit

After execution, you will skip to the interface of the First coomit and modify it.

First commit # Please enter the commit message for your changes. Lines starting # with'# 'will be ignored, and an empty message aborts the commit. # # Date: Thu Jan 26 23:07:10 2017 + 0800 # # rebase in progress; onto f9aee6e # You are currently editing a commit while rebasing branch 'master' on 'f9aee6eholders. # # Changes to be committed: # new file: file1

As for other operation items, if you are interested, you can try it yourself. For example, the s operation can be used to merge commit.

Branch

I'm sure branch is familiar with it, and what I'm talking about here is another situation he might use. The scenario goes like this: if you are creating a new branch, you do not want to create a branch from the current commit inodes.

To achieve this effect, you only need to add an additional parameter later when you create the branch, which is the hash code of the commit node you need to call.

Git branch new_branch

Push

Here we mention push's-- set-upstream. Its effect is to set the upstream branch. When we push a local branch that does not exist remotely, if it is not on the pushed branch, we usually use the following command to push.

Git checkout push_branch git push origin push_branch

Here is a simple way to use this parameter without switching branches, you can directly use the following command to push.

Git push-set-upstream origin push_branch

Cherry-pick

The scenario of this command is: when your branch is useless, you want to delete it, but you still want to push one of the commit to the remote master.

Switch the branch to master and execute the following command:

Git cherry-pick

Merge

What we are familiar with is using merge to merge branches. Every time we use merge, we automatically merge the secondary branch into a commit and push it to the main branch, so if I don't want it to be automatically pushed to the main branch (maybe I still need to modify it), I can use the-- squash operation.

Git merge-squash dev_branch

After executing the above command, we can see the status of an uncommitted file in the staging area.

Reflog

When we cut branches too often, we may forget which branch some branches are cut from. At this point, you can check it with the following command:

Git reflog894a16d HEAD@ {0}: commit: commit another todo 6876e5b HEAD@ {1}: checkout: moving from solve_world_hunger to kill_the_batman 324336a HEAD@ {2}: commit: commit todo 6876e5b HEAD@ {3}: checkout: moving from blowup_sun_for_ransom to solve_world_hunger 6876e5b HEAD@ {4}: checkout: moving from kill_the_batman to blowup_sun_for_ransom 6876e5b HEAD@ {5}: checkout: moving from cure_common_cold to kill_the_batman 6876e5b HEAD@ {6}: commit (initial): initial commit

So we can see the operation history used. So if we use the git reset command to accidentally delete what we need. You can use this to find the hash code of the delete operation, and then restore it with the following command.

Git checkout above is all the content of this article "how to use git". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report