Git Use detailed introduction

Source: Internet
Author: User
Tags diff mixed rewind rollback tag name using git git clone
1. Git concept
1.1. Git library is made up of three parts
The Git repository is the. Git directory that contains the contents of the document we submitted, and Git can track the documents it manages based on the content of the document index, thereby enabling versioning of the document. The. Git directory is located in the working directory.
1) Working directory: User's local directory;
2) index (index): Creates a snapshot of all files (including subdirectories) under the working directory, and stores them in a temporary storage area, which Git calls the area an index.
3) Warehouse: The index is submitted to the warehouse through a commit, each time the submission means that the version is being updated.

1.2. Initialization considerations when using Git
1.2.1. Git initialization configuration
1) Configure the name of the person who uses the Git warehouse
git config--global user.name "Your name comes Here"
2. Configure people to use git warehouse email
git config--global user.email you@yourdomain.example.com
1.2.2. Git document ignoring mechanism
Some files in the working directory do not want to be managed by git, such as intermediate files generated at compile time, and so on. GIT provides a document-ignoring mechanism to write to the. gitignore file in the same directory that does not want to accept git management in the working directory.
For example: There is a zh directory under the working directory, and if you do not want to add it to git management, execute:
echo "en" > gitignore
git Add.
A lot of details about the Gitignore file can be read in its Manual of use: Man Gitignore
Comparison of 1.3 git and repo
A git operation typically corresponds to a warehouse, whereas a repo operation typically corresponds to a project, where a project is made up of several warehouses.
For example, you use repo when you operate an entire Recket project, and you use Git when you manipulate one of the warehouses. Perform a git operation under the directory containing the hidden directory. Git.

2. Git Help
git help get git basic command
(If you want to know how a particular command is used, such as using Git help clone to get the use of Git clone)

3. Git local Operation basic command
3.1. Git Init
or use Git init-db.
Create an empty git library. Generates a subdirectory of. Git in the current directory. Later, all file change information will be saved to this directory, and unlike CVS, a CVS directory will be created in each directory and subdirectory.
There is a config file under the. git directory where you can modify the configuration information.
3.2. Git add
Adding a change or new file in the current working directory to Git's index and adding it to Git's index indicates that it has been recorded in version history, which is a step that needs to be performed before submission.
You can add recursively, that is, if you follow a directory as a parameter, all subdirectories and files in the entire directory are recursively added. For example:
git add dir1 (add dir1 This directory, all files in the directory are added)
Git Add F1 f2 (add f1,f2 file)
git Add. (Add all files and subdirectories under the current directory)
3.3. Git RM
Deletes a file from the current working directory and from the index.
Can be recursively deleted, that is, if followed by a directory as a parameter, it will recursively delete all subdirectories and files in the entire directory. For example:
Git rm–r * (go into a directory, execute this statement, delete all files and subdirectories under that directory)
git rm F1 (delete file f1, containing this file record in local directory and index)
git rm--ached F1 (deletes file F1, does not delete the local directory file, deletes only the file record in index, removes the file that has git add to the cache, so the commit does not submit this file, apply to add a lot of files at once, However, I would like to exclude some of the individual documents.
3.4. Git Commit
Commits changes to the current working directory.
Call the git commit command directly, prompting you to fill in the comment. Fill in the submit comment at the command line as follows: Git commit-m "Initial commit of Gittutor reposistory". Note that, unlike CVS, Git's submission comment must not be empty, or a failure will be committed.
Git commit also has a-a parameter that can be forcibly committed without the changes that are not made through the Git add identity, but this is not recommended.
Every time you commit, Git creates a unique commit code for the global code, and the user can recover the code at any one time by submitting the git reset command.
git commit–-amend–m "message" (constantly modifying committed content on a commit ID)
3.5. Git Status
View the status of the version library. You can learn which files have changed, which files have not been added to the Git library, and so on. It is recommended that you confirm the library status by using this command before each commit.
The most common mistake is to modify a file without calling git add to notify git that the file has changed and call a commit directly, causing the file to not be committed. If the developer thinks that the file has been submitted, they continue to modify or even delete the file, and the modified content is not managed by version. This error can be detected each time you use Git status to view it before submitting it. Therefore, if you invoke the git status command, be sure to pay extra attention to files that are prompted as "Changed but not updated:". These files are files that have changed compared to the last commit but have not been identified by git Add.
3.6. Git Log
View the history log, containing each version change. Each version change corresponds to a commit ID.
Git log-1
-1 means to show only one commit, and if you want to show 5, then-5. If not specified, git log will be displayed from the commit.
Git log--stat–summary (show detailed variations for each version)
In the project log information, the first line of each log (that is, that string of characters) is named for the version update submission, which we can interpret as the project version number. The project version number should be unique and automatically generated by Git by default to indicate an update to the project. If we use the project version number as an argument to the Git-show command, we can view the update details for that project version. For example:
1 Git Log

2 Git Show

In fact, the above command is not really a version number customization, but the creation of a tag object, which is more useful when the project version is released externally.
3.7. Git Merge
Merge the code that is downloaded from the server and the local code. or a branching merge.
For example: currently on the Master branch, if you want to merge branch dev onto master, git merge dev
Note: git merge nov/eclair_eocket (is to merge the Eclair_eocket branch of the server git library onto a local branch)
Git rebase nov/eclair_eocket (is to map the Eclair_eocket branch of the server git library to a local temporary branch, and then merge the changes on the local branch into this temporary branch, then initialize the local branch with this temporary branch)
3.8. Git diff
Compare the local code with the code in index, or compare the code in index with the code in the local warehouse.
1 Git diff
Compare the working directory and the code in index.
2 Git diff--Cached
Compare the code in index and local warehouses.
3.9 Git Checkout
3.9.1. Switch to Branch
1 Create a new branch and switch to the branch
Git checkout–b New branch Name
2 Switch to an already established local branch Local_branch
Git Checkout Local_branch
(After using Cat. Git/head, show Refs:refs/heads/local_branch)
3 Switch to a branch on the server Remote_branch
Git Checkout Remote_branch
(Remote branch Remote_branch can be listed through Git branch–r)
4) switch to a commit ID
Git Checkout commit_id
(After using Cat. Git/head, show commit_id)
5) switch to a tag
Git Checkout Tag
(After using Cat. Git/head, show tag)
Note: In addition to 1 and 2, the remaining three are simply switched to a temporary (no branch) state (this detached), where you can see cat on the (no branch) with Git branch. Git/head See pointing The corresponding commit ID. This (no branch) is only temporary, not a truly established branch. If you execute 2 at this point, this (no branch) disappears automatically, and if you do 1, create a new branch of branch and Hook this (no branch) to the new branch, at which point Cat. git/refs/heads/new_branch You can see that you have pointed to that commit ID just now.
3.9.2. Initializes a new branch with an existing branch
Execute the following command to create a new branch new_branch and hang it on the new branch while switching to an established local branch or a remote branch or a commit ID or a tag.
1 switch to an established local branch Local_branch and use this branch to initialize a new branch new_branch.
git checkout–b new_branch local_branch
2 Switch to a remote branch Remote_branch and initialize a new branch new_branch with this branch.
Git checkout–b New_branch Remote_branch
3 Switch to a commit ID and create a new branch New_branch
Git checkout–b New_branch commit_id
4 switch to a tag and create a new branch New_branch
Git checkout–b new_branch Tag
3.9.3. Restore Code
For example, "Git checkout app/model/user.rb" will update the user.rb file from the last submitted version, and the contents of the uncommitted working directory will all be overwritten.

3.10. Git-ls-files
View the files in the current Git library.
3.11 Git MV
Renames a file, directory, or link.
For example: Git mv helloworld.c helloworld1.c (rename file helloworld.c to helloworld1.c)
3.12 Git Branch
3.12.1. General statement
The cost of creating a branch in the Git version library is almost Nil., so you don't have to skimp on creating a few more branches. When Git init is executed for the first time, a branch named "Master" is created. Other branches are created by hand.
Some common branching strategies are listed below:
Create a personal work branch of your own that avoids too much disruption to master Branch master and facilitates communication and collaboration with others;
Create an experimental branch when doing high-risk work;
When merging other people's work, it is best to create a temporary branch for merging, and then "fetch" your own branch after the merge is complete.
The branch of the increase, delete, check and other operations.
Note: The branch information is generally in the. git/refs/directory, where the heads directory is the local branch, remotes for the corresponding server branch, tags for tags.
3.12.2. View Branches
Git branch lists all the branches in the local git library. In a listed branch, if the branch name has a * before it, this branch is the current branch.
Git branch–r lists all the branches of the server git library.
(You can continue to use the command "git checkout-b local branch name Server branch name" to get code files for a branch on the server).
3.12.3. See which branch is currently on
Cat. Git/head
3.12.4. Create a branch
1 Git Branch branch name
Although a branch is created, the current work branch is not switched to the newly created branch, so the command "git checkout branch name" is also required to switch,
2 git checout–b branch name
Not only did you create a branch, you also switched the current work branch to that branch.
3.12.5. Switch to a branch: Git checkout branch name
Switch to Main branch: Git checkout Master
3.12.6. To delete a branch
git branch–d branch Name
Note: After deletion, all changes that occur in the branch cannot be recovered. Force deletion of this branch.
3.12.7. Compare the differences between files on two branches
git diff Master branch name (compare the difference between the main branch and the other branch)
3.12.8. View branch History
Git-show-branch (view submission comments and information for the current branch)
Git-show-branch-all (view submission notes and information for all branches) for example:
* [Dev] D2
! [Master] m2
--
* [Dev] D2
* [dev^] D1
* [Dev~2] D0
*+ [Master] m2
In the example above, the two lines above "--" represent two branch Dev and master, and the last log submitted on the dev branch is "D2", and the last log submitted on the master branch is "m2". The lines below "--" represent the history of branching evolution, where Dev represents the last commit on the Dev branch, dev^ represents the penultimate submission on the Dev branch. Dev~2 represents the penultimate submission that occurred on the Dev branch.
3.12.9. View action records for the current branch
Git whatchanged
3.12.10. Merging branches
Law I:
git merge "notes" the source branch of the merged target branch
If there is a conflict with the merge, Git will be prompted.
For example: Git checkout master (switch to master branch)
Git merge head dev~2 (Merge Master branch and Dev~2 branch) or: git merge master dev~2
Law II:
The source branch of the merged target branch of git pull merge
For example: Git checkout master (switch to master branch)
Git pull. Dev~2 (merge current branch and Dev~2 branch)
3.13 Git rebase
is typically used when merging the latest server content into local. For example: In version C when the content from the server to the local, modify local content, at this point to the local modified content to submit to the server, but found that the version of the server has become G, this time you need to execute Git rebase, Merges the latest version on the server to the local. For example:
Explained in the following two diagrams will be more clear, the rebase command after the implementation of the actual is to move the branch from C to G, so that the branch also has a function from C to G.

3.14. Git Reset
The reversal and recovery of the library has an important role to play in addition to the reset of some discarded research and development code. For example, we have a code base from remote clone and are ready to commit back to remote after local development. But the local code base is developed with a functional commit, a commit for backup purposes, and so on. In summary, there are a lot of useless logs in the commit log, and we do not want to commit the log to the library when it is committed back to the remote. Therefore, you need to use git reset.
The concept of git reset is more complex. Its command form: git reset [--mixed |--soft |--hard] [<COMMIT-ISH>]
Options for commands:
--mixed This is the default option. such as git reset [--mixed] dev^ (dev^ definition can see 2.6.5). It only resets the branch state to dev1^, but does not change the contents of any working file. That is, all file changes from dev1^ to Dev1 are preserved, but all the commit logs between dev1^ and Dev1 are cleared, and the changed file contents are not identified by git add, and if you are going to commit again, you need to make a git of the changed files. Add Thus, after committing, a very clean submission record is obtained. (The contents of index and warehouse are rolled back)
--soft is equivalent to doing Git reset–mixed, and then to change the file to do git Add. If you use this option, you can commit directly. (The contents of the warehouse are rolled back)
--hard This command will cause all information to be rolled back, including the contents of the file. It is generally used only when the discarded code is reset. After execution, the contents of the file can not be restored back. (The contents of the working directory, index, and warehouse are rolled back)
For example:
Switch to the used branch;
git reset head^ rollback first record
git reset head~2 Rewind second record
If you want to return the files in the working directory, use git reset--hard head^ rollback first record
Git reset--hard head~2 Rewind second record
You can also use the following methods:
The current working directory is completely rolled back to the specified version number, assuming the following figure, we have a-g five submitted versions, where C's version number is BBAF6FB5060B4875B18FF9FF637CE118256D6F20, we executed ' git reset Bbaf6fb5060b4875b18ff9ff637ce118256d6f20 ' So the result is only the A-C three submitted versions

3.15 Git revert
Restore a modification to a version, such as Git revert commit_id (where commit_id is a uniquely represented string generated when the commit code)
For example: (3.6) git revert dfb02e6e4f2f7b573337763e5c0013802e392818 (to do this, restore the last commit action)
3.16. Git Config
This command allows you to add and change the various settings for git, such as "git Config branch.master.remote origin" to set the remote version Library of Master to an alias called the Origin version library.
3.17 Git Show
Displays the different types of objects.
3.18. Git Tag
Create, list, delete, or validate a Label object (signed with GPG).
You can label a specific version so that you don't need to memorize a complex version number hash string, for example, you can use "Git tag revert_version bbaf6fb5060b4875b18ff9ff637ce118256d6f20" To mark the version you restored, you can use the revert_version tag name instead of the hash value when you want to view the version later.

4. Git Server Operations Command (interacting with the server

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.