About some of the common commands in Git

Source: Internet
Author: User
Tags sha1 hash

Learn more about GIT's checkout commands

The checkout command (git checkout) is one of the most commonly used commands in Git, and it is also a dangerous command.

Because this command overrides the workspace. The check Out command is used as follows:

Usage one: git checkout [-Q] [<commit>] [--] <path> ....

Usage two: git checkout [<branch>]

Usage three: Git checkout [-M] [[-b|--orphan] <new_branch>] [<start_point>>]

The first use of <commit> is optional, and if omitted, it is equivalent to checking out from staging Area (index).

This is very different from the previous reset command: The default value of Reset is head, and the default value checked out is staging area.

The reset is therefore typically used to reset the staging area (unless the--hard parameter is used, the workspace is not reset)

The checkout command mainly covers the workspace (if <commit> does not omit it, the corresponding file in staging area will be replaced).

The first usage:

Let's take a shadow first:

In Git, a "photo" command is called tag, and a more professional term is called a "milestone" (tag, or tag).

The "Photo" command is as follows:

$ git tag-m "say bye to previous" Old_practice

Just know that a milestone is nothing more than a reference. "Take a photo" of the status of the current repository by recording the commit ID (or creating a tag object)

$ ls. git/refs/tags/old_practice

After you take a photo, you can execute the git describe command to display the latest commit as an easy-to-remember name.

The most recent milestone from the submission is selected as the "base version number", followed by the identity distance "base version"

and an abbreviation for the SHA1 hash value of the commit. Because the latest submission was hit by a "milestone".

So the name of the milestone is displayed directly. This technique is often used in the following example code.

Delete files: (This time the workspace has only one test1.txt file is already commit end, add new things, in modify state)

Using the Delete command is more challenging in situations where both the staging area and the workspace contain file modifications.

There are many ways to use the Delete command, some of which are ingenious, and others require more input.

In order to introduce the different deletion methods separately. Also use the git stash command that was previously used to save progress.

Save Progress:

In the recovery progress. Be careful not to use Git stash pop, but instead use git stash apply, because the progress of this save will be used multiple times.

Local deletions are not really deleted.

$ls is to see which files are in the workspace.

After $rm the Test1.txt command executes, the Test1.txt file in the workspace that is in the modified modified state is deleted.

However, after executing $ git ls-files, you can still see the file.

From the state of the file, the file was deleted locally and has not been added to the staging area (submit Task).

That is: Delete directly in the workspace, with no effect on staging area and the repository.

As can be seen from the state of Git, local deletion should be done with git RM command in staging area if you want to react to files that are not deleted.

Git checkout---<FILE>, which allows files to be reproduced in the workspace.

If you use the checkout command at this time, the test1.txt that have just been "deleted" will be restored to the workspace.

However, this file is not modified before the state. I have $git status in the picture above.

Execute git rm command to delete files

$ git rm test1.txt
This removes the action to join the staging area, when the commit action is executed, the file is deleted from the real meaning.

$ git commit-m "delete files (using:git rm)"

Do not worry, however, that the file was only deleted in the latest submission of the repository and is still in the history submission. You can view a list of the historical versions of a file by following the commands below.

$ git ls-files--with-tree=head^

You can also view the contents of deleted files that are still in the historical version.

$git cat-file-p Head^:test1.txt

command git add-u Quick Tag Delete

When you execute the git RM command earlier, you write down all the file names you want to delete, which is very long. Can you simplify it?

Git add is actually available. That is, use the-u parameter to invoke the git add command. The meaning is to mark files with local changes (including modifications and deletions) to staging area.

To reproduce the scene, use the Reset command to discard the latest commit, and then use the progress to revert to the previous state.

The entire process of the file:

$git Stash Apply-q Restore the saved progress (parameter-Q causes the command to enter quiet mode)

The execution of the above command restores the modified state to the test1.txt.

The git add-u command allows you to record changes (modifications, deletions) of local files (tracked by the repository) to staging area.

About some of the common commands in Git

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.