Git simple tutorial: basic operations

Source: Internet
Author: User

Tag: git

Initialize and submit files
  • Create the initialization directory mkdir learngit
  • Initialize a repository
    Git init

  • Create a file in learngit and submit it to the local repository.
    git add readme.txt
    git commit -m "wrote a readme file"

  • You can also add one file at a time through *. The following one adds two TXT files starting with readme at a time.


File Modification and submission
  • Modify the readme.txt file and add the following content to the file.
    Git is a distributed version control system.
    Git is free software.
  • View git status


  • Git diff readme.txt

  • Submit the file git add readme.txt
  • The status indicates that readme.txt will be submitted.

  • Git commit-m'test'
  • Git status, indicating that no file is to be submitted. The working directory is clean.


Version rollback
  • View logs through git log

  • Use git log-- Pretty = oneline

  • Use git reset -- hard 609c9 to jump to the specified version. At this time, the head Pointer Points to this version. version information before 609c9 has been removed from git log or gitk.
    You can also use git reset -- hard head ^ to jump to the previous version. The first two versions of head ^ are supported ~ N to jump to the first n versions

  • If you want to return to latest version after the jump, If you disable the command line, the commit ID of this version will disappear. To find the corresponding commit ID,
    You can use git reflog to view operation records. If you want to return to the latest version, run git reset -- hard da5e02f.

Temporary Storage Area

When we add files to the GIT repository, we perform the following two steps:

The first step is to usegit addAdding a file is actually adding the file modification to the stage );

Step 2: Usegit commitSubmitting changes is actually to submit all the content in the temporary storage area to the current branch.

Manage changes

When modifying an object, if the operation order is the first modification->git add-> Second modification->git commit

Because git manages modificationsgit addAfter the command, The first modification in the workspace is put into the temporary storage area and ready to be submitted. However, the second modification in the workspace is not put into the temporary storage area,git commitOnly responsible for submitting changes to the temporary storage area, that is, the first modification is submitted, and the second modification is not submitted.

So how can I submit the second modification? You can continuegit addAgaingit commitOr do not rush to submit the first modification.git addThe second modification, and thengit commit, It is equivalent to merging the two modifications and submitting them one by one:

First modification->git add-> Second modification->git add->git commit

Undo Modification
  • Scenario 1: When you disrupt the content of a file in the workspace and want to discard the modification, run the following command:git checkout -- file.
    The following example shows how to restore a file by executing the GIT checkout -- readme.txt command after a file is modified.

  • Scenario 2: When you not only disrupt the content of a file in the workspace, but also add the file to the temporary storage area, you want to discard the modification in two steps. The first step is to use the commandGit reset head file undo the content in the stageAnd then return to scenario 1. Step 2: perform operations according to scenario 1.

  • Scenario 3: If you have submitted an inappropriate modification to the version library and want to cancel this submission, you can use the reset command, provided that it is not pushed to the remote database.
Delete an object
  • Delete the RM readme.txt file in the operating system
  • Use git checkout -- readme.txt to restore the file
  • If you are sure to delete the file, run git RM readme.txt and git commit-M 'Confirm delete a file'

Git simple tutorial: basic operations

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.