git learning record - git in the warehouse, file status, modify, and commit operation

  Note: The main record of this article read  Pro git  notes of the first two. Part of a direct translation of the corresponding content.

  Recently started using git version control tool. The main way of learning through reading Pro git git and hands-on practice on site, the system used is Ubuntu16.04LTS, as well as Windows 8.1, in this record.

 

Introduction to Git

  Three states Git file management

  committed: committed state data indicating the file has been saved to local database.

  modified: Modify the state, it indicates that the file has been modified, but has not yet been submitted (saved).

  staged: the temporary state, indicating that marked the file is modified, it will submit all labeled when the next save changes.

 

  Git operate in three fields

  Git directory: Git directory, a local git repository of data and information, that warehouse (repository), save all versions of the project location and related information. User during clone (clone) is that the operation of the operation git directory (repository) is;

  Working Directory: working directory, it is a collection of files corresponding to a version of the project, from the corresponding extract from the git directory for the user to operate and modify data and information;

  Staging area: staging area, you need to save the time of submission for the next recording file list file information;

  Corresponding to the above three file status can be seen, if the file already exists in the git directory (repository), then the file has been submitted to the state . If the file has been modified and recorded to the staging area, the file is temporarily stored state . If the file has been modified but not just marked as staged, (only saved when the state must first enter the temporary submission) was modified state, and does not save the relevant information at the time of the next commit.

 

  Different status of the file

  In the working directory (working directory), there are two states of documents, tracked and untracked. tracked files are present on the submission of those documents, that is documented already existed in Git, these files may in turn be modified, it may submit the same time and the last state; Untracked file is the working directory of all other the document, submitted as the last new files in the directory. When the first cloned into the warehouse directory that all files are tracked and files are not modified.

  The current working directory state transition diagram may be present in the file (from the git pro):

  

  You can git status command to obtain the status of files in the current directory, which shows the results of a major state described by the following signs:

    Files Untracked : flag state of the file is untracked, Git does not take the initiative to convert the file Untracked state is staged state, requires the user through the conversion git add command is displayed, to avoid submissions do not want to upload;

    BE committed to Changes  : flag state of the file is staged, when submitted, it will be recorded above-mentioned documents;

    Not staged for the commit Changes : flag that has been modified but not in staged state tracked file, use git add command to convert it to a staged state;

  Use the command git add filename specified by filename, file or directory is added staged state (from a file or from a modified tracked Untracked file). Use the command git commit to commit operation.

  Note : Submissions are to be committed in the Changes file status, the contents of the documents submitted for the use of the contents of the file when the git add command . That will not be submitted to modify the same file at the secondary use git add command, unless git again add the modified file join staged state.

  Reflected in the git status command, then when using the command git add ac again ac file has been modified, these documents will appear in the Changes at the same time to be committed and Changes not staged for commit description (ie the presence of the same name entry), but the specific the contents of the file are different, and will only be submitted in Changes to be committed status when submitting an entry, that is not modified after submission. Users can git status -s / - short to give a brief description of the file status, see here .

 

Git basic operations

  Pull Git directory (Warehouse)

  Get a Git repository, there are two main ways:

  (1) a new local directory into a Git repository. Enter the directory, enter the command git init, Git creates a .git directory in the current directory, which contains all the necessary information warehouse. The command just completed the initialization process, and no submission, therefore the warehouse can be considered null.

  (2) clone (clone) a Git repository from elsewhere. Use the command git clone will target repository of all files stored on the server (including all versions and modification history) to the local clone, unlike other version control software checkout (get only the version of the project) command different. 

  clone git <url>             // get content url points to warehouses

  When a user uses HTTPS clone git : // when github.com/project_name/project_name command to get a git repository, git will perform the following procedures

    . A local directory to establish a project_name, and which initializes a .git directory;

    b. Get all the files in the warehouse, and the latest version of unzip files placed to create project_name working folder for the user to operate.

  The user can add a destination folder name as a parameter, such as git clone https://github.com/xxx/xxx dir_name, the git is placed under the warehouse to the file directory dir_name behind the instruction. In addition to the https protocol, git also supports the git: // and user @ server: path format.

 

  Use basic commands

  Command to a file in the current directory version control operation by a simple git.

  git status - to view the file status

  View the status of individual files in the current directory via git status. Git status operation instruction will be given by various states of the document may be used.

    Status git         // view the current status of each file folder

  Git state of the repository at a main document (see the corresponding description above):

  Untracked files: git track record has not been a corresponding file when a directory is initialized by git init, to initialize an empty git directory, all files in the directory are not git track record in the state;

  changes not staged for commit: git has been limited to the trace file (tracked) is. If any of these modifications occurred, but it has not been required by the addition of git add submitted state (staged for), then in this state;

  changes to be committed: only files have been git track record (tracked) is. When these files have modifications, and has been joined by git add needs to be submitted to the state, in this state. Modification shall be submitted through the git commit modify the file in the state;

  Practice when a new test directory, which contains only one hello.cpp file, the init after initialization, i.e., generates an empty git repository, git status results as shown below by Git, can be seen at this time in Untracked hello.cpp status.

  

 

  git add - Add files to be modified to state

  Status git add to add files to be submitted (changes to be committed). git add has two main functions: a track record will not git file (untracked) "Join" record git repository; b. in the modified file under changes not staged for commit state added to the need to submit. state (staged). These two features are file will become changes to be committed state.

    the Add git file-list         // add the file-list to the file specified changes to be committed state
     git the Add.                 // add all files to the state changes to be committed in the current directory 

  Users can git add command files in the current directory under the untracked state "Add" to git repository. In the author's practice, that is, through git add hello.cpp will join hello.cpp changes to be committed state, can also be used git add. Command goes all the files in the current directory changes to be committed state.

 

  git commit - commit the changes to the warehouse

  Commit to submit all documents in the state staged by command git. Note that the above command will submit the file using git add command marked staged state .

  By command git Git will commit the time an edit screen, the user must edit the commit message (Message commit), for identifying the submission by the default display editor. Where the line beginning with # denotes a comment, the user edits the end of the exit, Git creates a new submission (ie generate a new snapshot) to use the information submitted. The default display includes the results (comments) Last git status command displays, adding parameters when using the -v will submit the results git diff command is also included in the display content, the user can be modified by uncommenting or edit new content submit information (the result of running under Ubuntu system).

  You can also use git commit -m "commit message" command to specify directly submit information, the submission process can be used in a more compact case.

   

  git rm - delete tracking of documents

  In Git, delete a file comprises a file tracked state has been canceled, then later submit, through the completion of the operation command git rm. More specifically, reference can be described herein .

  git rm ac ac file will be deleted, and delete the "Edit" marked staged state. When using the git commit command, ac file will disappear from the working directory, and no longer tracked state. This ensures that the file will not be treated as untracked files for processing operation after again.

  Note: Conventional delete operations such as rm, will make the deleted files in Changes not staged for commit state, and git rm deletion makes the deleted files in a Changed to be commited state. For the latter, after the user submits, are no longer recorded after Git deleted files, while the former will not be submitted due, will remain in this state.

  Has been modified and added to the state staged files, delete the file using the -f parameter you need to avoid deleting files and other misuse Git has not been recorded by the git rm.

  Use parameter git rm --cached xxx can make the file is no longer being recorded Git, but continues to exist in the working directory, similar to the role and .gitignore file.

  

  git log - View commit history

  Submit records for locally created or cloned from another warehouse project has multiple submissions of record, you can view history through the git log command.

  By default, git log will (i.e., last commit first display) according to reverse the order submitted, git log is displayed and the corresponding order of submission SHA-1 hash value, and OF, mailbox, filing date, and submission of information content.

  

  git log have some common parameters may be used to display content needs more precise.

    -p            // Join modify the information submitted in each log entry 
    -n            // show only the most recent n number of entries submitted 
    --stat        // display file is modified each time you submit the information 
    --pretty = Options     // Options may be online, short and the like, can also specify the output format information itself, reference may here 
    --graph              // form of a tree display output character commit record

 

  Revocation and modification operations

  (1) For submission process: If found some documents not submitted properly (not joined staged state) to submit information or omission, may first need to submit additional documents to join staged state, and then use git commit --amend command, this submission will replace the last submission, in the process, you can modify the submitted information;

  (2) for the staged file states: using the command git reset HEAD filename filename file can be restored from the modified staged state to state in general;

  (3) for the modified files: To To restore files that have been modified to unmodified state, can be used git checkout --filename, will be submitted to the corresponding file record from the previous recovery, and to simply discard its modification;

  In fact, to get the current working directory, file status information via git status command will prompt the appropriate action. Users can directly use the git status command to see how to do this.

 

  other

  (1) Status Area skipped staged: -a added to the parameter in the command git commit, Git directly submit all the documents tracked, while eliminating the need to convert it to git add staged state.

  (2) creates a .gitignore file, does not need to be set for display and submissions (ignore processing), specific reference may be written here

 

Git Configuration

  Git git config to the modification of the variables such applications personalization, personalization by the tool carrying the variables may be stored in three different positions, each with a different scope (Ubuntu directory structure Environment) .

  (1) / etc / gitconfig, Git global variable setting. In which the settings apply to all users and Git repository system. --System parameter is set by the git config.

  (2) ~ / .gitconfig or ~ / .config / git / config, the user's personal Git variable setting, which is applied to all operations of the user. --Global set by parameter.

  (3) config file directory Git (hereinafter .config directory), arranged to record some of the variables Git directory.

  Configuration variable values ​​of each layer will be provided on the covering layer of the set value, and therefore the effect of the config file in a specific directory Git can mask the effect of providing settings / etc / gitconfig corresponding to.

  Set personal information

  Find information about individuals, including user names and emai, prior to submission, you must set up a user name and mailbox name . This is associated with an individual user, and therefore parameter --global. 

  git config --global user.name "xxx"
  git config --global user.email email_address

  Set the default editor   

  --global core.editor vim config git         // settings editor in Ubuntu environment for vim

  View Settings

  Configuration parameters can be viewed through the following command Git

  config --list git            // View all current values of the variables have been set, reads from the above three files, and therefore specific variables may appear several times, the last occurrence of the value of the value of its actual use Git              
  git config key_name          // View Git current value of the particular variables, such as git config user.name

 

  Git Help

  Users can get help through the command line, include the following command, we can understand the function of git related commands through the help of information and network resources. 

  git help <action> // Tathagata nice help is available for operation by config Help config git     
  man GIT-name acts such as // get help by git-config man 
  git the Command -h // get the help of simple tips, such as git config -h

 

Reference and Learning Resources:

  Git Handbook

  github-git-cheat-sheet, Github

  

Guess you like

Origin www.cnblogs.com/yhjoker/p/7740203.html