Git for detailed use, git for use

Source: Internet
Author: User
Tags how to use git

Git for detailed use, git for use
1. Git Concept
1.1 The Git library consists of three parts
Git repository is that. the git directory stores the submitted document index content. Git can track the content of the documents it manages based on the document index content to achieve document version control .. The git directory is located in the working directory.
1) working directory: local user directory;
2) Index: generate a snapshot of all files (including subdirectories) in the working directory and store it in a temporary storage area. Git calls this area an Index.
3) Repository: Submit the index to the repository using the commit command. Each commit means that the version is updated.
 
1.2. Initialization items when using Git
1.2.1. Git Initialization Configuration
1) Name of the person Who configured to use git Repository
Git config -- global user. name "Your Name Comes Here"
2) configure the git repository staff email
Git config -- global user. email you@yourdomain.example.com
1.2.2. Git document ignore Mechanism
Some files in the working directory do not want to be managed by Git, such as intermediate files generated during program compilation. Git provides a document ignore mechanism to write the document information that you do not want to accept Git management in the working directory to the. gitignore file in the same directory.
For example, the working directory has an zh directory. If you do not want to add it to Git management, run:
Echo "zh" & gt;. gitignore
Git add.
For details about the gitignore file, read its user manual: man gitignore.
1.3. Comparison between Git and Repo
Git operations generally correspond to a repository, while Repo operations generally correspond to a project, that is, a project may consist of several repositories.
For example, you can use the Repo when operating the entire Recket project, and use Git when operating a repository. Execute the git operation under the directory that contains the hidden directory. git.

2. Git help
Git help to get git basic commands
(If you want to know how to use a specific command, for example, use Git help clone to get how to use git clone)

3. Git basic commands for local operations
3.1. Git init
Or use git init-db.
Create an empty Git library. Generate a. git subdirectory in the current directory. In the future, all file change information will be saved to this directory. Unlike CVS, a CVS directory will be created under each directory and subdirectory.
There is a config file in the. git directory that can modify the configuration information.
3.2. Git add
Add the changes or new files in the current working directory to the Git index. Adding the changed files to the Git index indicates that the files are recorded in the version history, this is also a step that needs to be performed before submission.
It can be recursively added, that is, if a directory is followed by a parameter, all subdirectories and files in the entire directory will be recursively added. For example:
Git add dir1 (add the dir1 directory, and all files under the directory are added)
Git add f1 f2 (add f1, f2 files)
Git add. (add all files and subdirectories in the current directory)
3.3. Git rm
Delete files from the current working directory and index.
It can be recursively deleted, that is, if a directory is followed as a parameter, all subdirectories and files in the entire directory will be recursively deleted. For example:
Git rm-r * (enter a directory and execute this statement to delete all files and subdirectories in the directory)
Git rm f1 (delete file f1, including this file record in the local directory and index)
Git rm -- ached f1 (delete file f1, do not delete local directory files, only delete file records in index; remove files that have been added by git to cache, in this way, this file will not be submitted during the commit operation. This applies to the case where many files are added at once, but you want to exclude several files .)
3.4. Git commit
Submit the changes to the current working directory.
Directly call the git commit command and you will be prompted to enter a comment. Enter the submit comment in the command line as follows: git commit-m "Initial commit of gittutor reposistory ". Note: Unlike CVS, the git commit comments must not be empty; otherwise, the commit fails.
Git commit also has a-a parameter that can be forcibly submitted together with changes that are not identified by git add. However, this method is not recommended.
For each commit, git creates a unique commit ID code for the global code. You can use the git reset command to restore the code at any commit.
Git commit -- amend-m "message" (The submitted content is constantly modified on a commit id)
3.5. Git status
View the status of the version library. You can find out which files have changed and which files have not been added to the git library. We recommend that you use this command to check the database status before each commit operation.
The most common mistake is to modify a file and call the commit operation without calling git add to notify the git database that the file has changed. As a result, the file is not actually submitted. If the developer thinks that the file has been submitted and continues to modify or even delete the file, the modified content is not managed by version. If you use git status to check the status before each commit, you can find this error. Therefore, if you call the git status Command, pay special attention to the files that prompt "Changed but not updated. These files are all changed compared with the previous commit, but they are not identified by git add.
3.6. Git log
View historical logs, including each version change. Each version change corresponds to a commit id.
Git log-1
-1 indicates that only one commit is displayed. If you want to display five commit,-5 is displayed. If this parameter is not specified, the git log will be displayed from the commit.
Git log -- stat-summary (displays detailed changes to each version)
In the project log information, the first line (that is, the string of characters) of each log is the name for version update submission. We can name it as the project version number. The project version number should be unique. By default, it is automatically generated by Git to indicate an update of the project. If you use the project version number as a parameter of the git-show command, you can view the update details of the project version. For example:
1) Git log
 
2) Git show
 
In fact, the above commands are not really customized for version numbers, but create a tag object, which is useful for external release of Project versions.
3.7. Git merge
Merge the downloaded code on the server with the local code. Or merge branches.
For example, if you want to merge the dev on the master node, git merge dev
Note: git merge nov/eclair_eocket (merge eclair_eocket branches of the git repository on the server to the local branch)
Git rebase nov/eclair_eocket (map the eclair_eocket branch of the git repository on the server to a local temporary branch, and then merge the changes on the local branch to this temporary branch, then use this temporary branch to initialize the local branch)
3.8. Git diff
Compare the local code with the code in the index, or compare the code in the index with the code in the Local repository.
1) Git diff
Compare the code in the working directory and Index.
2) Git diff--cached
Compare the code in index and local repository.
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 a created local branch local_branch
Git checkout local_branch
(After cat. git/HEAD is used, refs: refs/heads/local_branch is displayed)
3) switch to the remote_branch branch on the server.
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 cat. git/HEAD is used, commit_id is displayed)
5) switch to a tag
Git checkout tag
(The tag is displayed after cat. git/HEAD is used)
Note: except for 1) and 2), the other three are switched to a temporary (no branch) status (this head is detached ), at this time, you can see that the git branch is on (no branch), cat. git/HEAD points to the corresponding commit id. This (no branch) only exists temporarily and is not a truly established branch. If 2 is executed at this time, the (no branch) will automatically disappear; if 1 is executed), a new branch will be created and the (no branch) mounted to the new branch, cat. git/refs/heads/new_branch, you can see that it has pointed to the commit id just now.
3.9.2. Use an existing branch to initialize a new branch.
Run the following command to create a new branch new_branch and attach it to a created local branch, a remote branch, a commit id, or a tag.
1) switch to a created 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 use this branch to initialize a new branch new_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. the rb file is updated from the previous submitted version, and all the content in the unsubmitted working directory will be overwritten.

3.10. Git-ls-files
Check the files in the current git library.
3.11. Git mv
Rename a file, directory, or link.
Example: Git mv helloworld. c helloworld1.c (rename the file helloworld. c to helloworld1.c)
3.12. Git branch
3.12.1. Summary
The cost of creating branches in the git version library is almost zero, so you do not have to create several more branches. When git init is executed for the first time, the system creates a branch named "master. Other branches are created manually.
The following lists some common branch policies:
Create a Personal Work Branch to avoid too much interference to the master of the master branch and facilitate communication and collaboration with others;
Create an experimental branch for high-risk work;
When merging others' work, it is best to create a temporary branch for merging, and then "fetch" to your own branch after merging.
Add, delete, and query branches.
Note: The Branch information is generally in the. git/refs/directory, where the heads directory is a local branch, remotes is the branch on the corresponding server, and tags is the label.
3.12.2. View branches
Git branch lists all branches in the local git repository. In the listed branch, if * exists before the branch name, the Branch is the current branch.
Git branch-r lists all the branches of the git repository on the server.
(You can continue to use the command "git checkout-B local branch name server branch name" to obtain the code file of a branch on the server ).
3.12.3. view the current Branch
Cat. git/HEAD
3.12.4. Create a branch
1) git branch name
Although a branch is created, the current branch is not switched to the new branch. Therefore, you need to run the "git checkout branch name" command to switch,
2) git checout-B branch name
Not only is a branch created, but it also switches the current branch to the branch.
3.12.5. Switch to a branch: git checkout branch name
Switch to the master branch: git checkout master
3.12.6. Delete Branch
Git branch-D branch name
Note: after deletion, all changes in the branch cannot be recovered. Force Delete this branch.
3.12.7. Compare the differences between files on the two branches
Git diff master branch name (compare the differences between the master Branch and the other branch)
3.12.8. View branch history
Git-show-branch (view comments and information about the current branch)
Git-show-branch-all (view comments and information about all branches) for example:
* [Dev] d2
! [Master] m2
--
* [Dev] d2
* [Dev ^] d1
* [Dev ~ 2] d0
* + [Master] m2
In the above example, the two rows above "--" indicate that there are two branches dev and master, and the last commit log on the dev branch is "d2 ", the last submitted log on the master branch is m2 ". "--" Indicates the evolution history of the branch. dev indicates the last commit that occurred on the dev branch, dev ^ indicates the second-to-last commit on the dev branch. Dev ~ 2 indicates the last and third commit on the dev branch.
3.12.9. view the operation records of the current Branch
Git whatchanged
3.12.10. merge branches
Method 1:
Source branch of the target branch merged by git merge "comment"
If there is a conflict in the merge, git will prompt.
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
Method 2:
Source branch of the target branch merged by git pull
Example: git checkout master (switch to master Branch)
Git pull. dev ~ 2 (merge the current branch and dev ~ 2 Branch)
3.13. Git rebase
It is generally used when the latest server content is merged to the local device. For example, the local content is obtained from the server in Version C, and the local content is modified, in this case, you want to submit the locally modified content to the server. However, if the version on the server has changed to G, you need to execute Git rebase first, merge the latest version on the server to your local computer. For example:
The two figures below will be clear. After the rebase command is executed, the pivot point is actually moved from C to G, so that the Branch has the function from C to G.
 
3.14. Git reset
In addition to Resetting discarded R & D code, database reversal and restoration also play an important role. For example, we cloned a code library from a remote server and submitted it back to the remote server after local development. However, the local code library has a functional commit and a commit for backup purposes during development. In short, there are a lot of useless logs in commit logs. We do not want to submit these logs to the database when they are submitted back to the remote. Therefore, git reset is required.
The concept of git reset is complicated. Command Format: git reset [-- mixed | -- soft | -- hard] [<commit-ish>]
Command Options:
-- Mixed: This is the default option. For example, git reset [-- mixed] dev ^ (for the definition of dev ^, see 2.6.5 ). It only resets the branch status to dev1 ^, but does not change the content of any working file. That is, all file changes from dev1 ^ To dev1 are retained, but all commit logs between dev1 ^ and dev1 are cleared, and, the changed file content is not identified by git add. If you want to re-commit the file, you also need to perform git add on the changed file. In this way, a very clean submission record is obtained after the commit operation. (Content in index and repository is rolled back)
-- Soft is equivalent to git reset-mixed, and then git add for the changed file. If this option is used, you can directly commit it. (The contents in the repository are rolled back)
-- Hard this command will cause the rollback of all information, including the file content. It is generally used only when the discarded code is reset. After execution, the file content cannot be restored. (The contents in the working directory, index, and warehouse are rolled back)
For example:
Switch to the used Branch;
Git reset HEAD ^ roll back the first record
Git reset HEAD ~ 2. roll back the second record
If you want to roll back the files in the working directory, use git reset--hard HEAD ^ to roll back the first record.
Git reset--hard HEAD ~ 2. roll back the second record
You can also use the following method:
Roll back the current working directory to the specified version. Suppose, for example, we have the version submitted five times by the A-G, where the C version number is bbaf6fb5060b4875b18ff9ff637ce118256d6f20, we executed the 'git reset bbaf6fb5060b4875b18ff9ff637ce118256d6f20 ', then the result is only three submitted versions of the A-C.
 
3.15. Git revert
Restore A version modification. For example, git revert commit_id (where commit_id is a unique string generated when the commit code is used)
Example: (IN 3.6) git revert dfb02e6e4f2f7b573337763e5c0013802e392818 (perform this operation to restore the previous commit Operation)
3.16. Git config
This command can be used to add and modify Git settings. For example, "git config branch. master. remote origin" sets the remote repository of the master as an alias called the origin repository.
3.17. Git show
Different types of objects are displayed.
3.18. Git tag
Create, List, delete, or verify a tag object (signed by GPG ).
You can tag a specific version so that you do not need to remember the complex version hash string, for example, you can use "git tag revert_version failed" to mark the version you restored. If you want to view the version later, you can use the revert_version tag name instead of the hash value.

4. Git server operation commands (interacting with the server)
4.1. Git clone
Extract the code from the server repository to a locally Created directory (interacting with the server)
After obtaining the remote git library through git clone, the developer information in. git/config will not be cloned together. You still need to add developer information to the. git/config file of the local database. In addition, developers also need to add their own. gitignore file.
The remote git library obtained through git clone only contains the current working branch of the remote git library. If you want to obtain other branch information, you need to use "git branch-r" to view it. If you need to obtain other branch code remotely, you can run the command "git checkout-B local branch name remote branch name", where the remote branch name is the branch name listed by "git branch-r, it is generally like "origin/branch name. If the local branch name already exists, the "-B" parameter is not required.
For example:
 
4.2. Git pull
Obtain the code from the server repository and merge it with the local code. (Interact with the server and download the latest code from the server, equivalent to: Git fetch + Git merge)
Update the code from other version libraries (either remote or local) to the local. For example: "git pull origin master" is to update the code of the source database to the local master branch.
Git pull can obtain the content of a branch from any git library. The usage is as follows:
Git pull username @ ipaddr: Remote repository name remote branch name local branch name. This command obtains a local branch of the local git library from the remote branch name of the remote git library. If the local branch name is not specified, the default pull is to the local branch.
Note that git pull can also be used to merge branches. Same as git merge. Therefore, if your local branch already has content, git pull will merge these files. If there is a conflict, an alert will be reported.
For example:

 
4.3. Git push
Update the local commit code to the remote version library. For example, "git push origin" updates the local code to the remote version library named orgin.
Git push and git pull are the opposite. They submit the content of a local branch to a remote branch. Usage: git pushusername @ ipaddr: Remote repository name local branch name remote branch name. This command pushes a local branch of the local git Library to the remote branch name of the remote git library.
Note that git push does not automatically Merge files. Therefore, if a conflict occurs during git push, it will be forcibly overwritten by the content of the subsequent push file, and there is no prompt. This is very dangerous during cooperative development.
For example:
 
4.4. Git fetch
Download the code from the server repository. (Interact with the server and download the latest code from the server)
It is equivalent to remotely obtaining the latest version to the local device. It does not automatically merge and is safer than Git pull.
Use this method to obtain updates on the server.
For example, if you use git checkout nov/eclair_rocket (nov/eclair_rocket is the branch name on the server), the code downloaded from the server when you last used the git fetch Command is obtained; if you first use git fetch and then use git checkout nov/eclair_rocket, the latest update information is obtained from the server and the latest code is downloaded from the server.

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.