Git advanced tutorial

Source: Internet
Author: User

Git advanced tutorial

Today, I accidentally saw it was said that it was a Git getting started tutorial. After reading it, I learned a lot of new things and summarized and recorded what I learned. Otherwise, I always felt that something was missing.

The following figure illustrates the concept of a work directory, a version library, a stage, and a branch.

The following explanation is not completely rigorous. Please understand the following four concepts in combination.
Working dicrectory: the parent directory containing the. git directory is generally the workspace, which is our project directory. All the newly created files are in the work zone. At this time, they are not added to the hold zone to be explained later.

Version Library (Repository): The. git directory is the version library. All files related to version management are in this directory.

Stage: After you modify a file that has been added to version control, rungit addThe file is in the hold zone.

Branch: git will create a master branch by default and executegit commitThen, the files in the hold zone are included in the branch.

The following is a readme. rst file that has been added to the version library. After modifying the content, check the status.

E:\Users\liuzhijun\workspace\blog>git status# On branch master# Your branch is ahead of 'origin/master' by 4 commits.#   (use "git push" to publish your local commits)## Changes not staged for commit:#   (use "git add <file>..." to update what will be committed)#   (use "git checkout -- <file>..." to discard changes in working d##       modified:   README.rst#no changes added to commit (use "git add" and/or "git commit -a")

Git prompts that README. rst has been modified, but it is not a file in the hold zone (not staged), waiting for commit. Then I will tell you how to perform the operation,checkoutThis modification is revoked. Note that--If this character is not included, checkout has another meaning.

Return to specified version

Return to the specified version and use the commandgit reset --hard <version>HEAD always points to the current version. HEAD ^ indicates the previous version. If you want to return to the previous version, you can use:

git reset --hard HEAD^^  

What if I want to return to a specified version? Availablegit logView the commit version:

commit 33b351ae746edaf3fd5a56a0318235096b6ed1ceAuthor: liuzhijun <lzjun567@gmail.com>Date:   Sat Mar 15 11:43:56 2014 +0800    commit many filescommit 86eefaaea5251fa5707ecd02009c893c098ab6cdAuthor: liuzhijun <lzjun567@gmai..com>Date:   Thu Mar 14 03:20:27 2013 +0800    add author myself

The string after commit is the version number. Generally, you only need to select the first few digits. Git automatically searches.

git reset --hard 86eefa

The command above is returned to the specified version. If I regret it again, what should I do if I want to restore it to the latest version? As long as you still remember the latest version number, simply execute the above command, but who will remember this number? Another method is to usegit reflogView, this command records each operation.

E:\Users\liuzhijun\workspace\blog>git refloga11c917 HEAD@{0}: reset: moving to a11c        HEAD@{1}: reset: moving to HEAD^        HEAD@{2}: reset: moving to a11c917430050a94549e48d205ef01cacc82c1cf        HEAD@{3}: reset: moving to HEAD^

The above allc... is my last modification.

Change)

The modification here is not a verb, but a noun. As long as the file changes, it indicates modifications, including modifications to the file content, a newly created file, or a deleted file. Git tracking (track) is modification, not the file itself.

Checkout)

Undo: After a file is modified, the modification has not been added to the hold zone (git add has not been executed). If the file has been added to the hold zone, however, if you do not want to revoke the service after making any modifications to the Branch, the revocation here is the state of withdrawing the service to the hold zone. For example, add "add some to file" to the file ":

E:\Users\liuzhijun\workspace\blog>git status# On branch master# Your branch is ahead of 'origin/master' by 4 commits.#   (use "git push" to publish your local commits)## Changes not staged for commit:#   (use "git add <file>..." to update what will be committed)#   (use "git checkout -- <file>..." to discard changes in working directory)##       modified:   README.rst

Then add it to the hold zone:

git add README.rst

Add "add some again to file". After undo, you will find that the content added for the first time is retained, and the content added for the second time is revoked.

E:\Users\liuzhijun\workspace\blog>git checkout -- README.rstE:\Users\liuzhijun\workspace\blog>git status# On branch master# Your branch is ahead of 'origin/master' by 4 commits.#   (use "git push" to publish your local commits)## Changes to be committed:#   (use "git reset HEAD <file>..." to unstage)##       modified:   README.rst
Branch Management

It may take several weeks to develop a new function. You can create a branch and develop it on the branch without affecting the function of the main branch.
Create Branch:

git branch devgit checkout dev

Or merge it into a command:

git checkout -b dev

After the dev branch switch is created, the HEAD pointer is transferred from the original master to the dev branch,git branchYou can view the branches on which the cluster is located.

E:\Users\liuzhijun\workspace\blog>git branch* dev  master

The asterisk represents the current branch.

Switch Branch:

git checkout master

Merge Branch:
After switching the branch, the modifications made on the dev branch are invisible to the master branch. If the dev branch function is developed, you can consider merging the branches. after merging, you can delete the dev branch, this is because the dev branch does not make much sense to us.

git merge dev

Merge the branches to execute the dev branch on the master, and then delete the branches.

git branch -d dev

Git encourages you to use the branch. Therefore, you must be familiar with the use of the branch.

Conflict

If different people modify the same part of the same file, a conflict will occur after submission, or, if the same file is modified on different branches, conflicts also occur, you must manually modify the conflict and then submit it to resolve the conflict.

Create the branch dev and add the content "add new branch dev". After the commit operation, switch to the master branch and add the content "may be here is conflict" in the same line. After the commit operation, merge the content.

Git checkout-B devgit add README. rstgit commit-m "add new branch" git checkout mastergit add README. rstgit commit-m "add new line" git merge dev # Error Auto-merging README. rstCONFLICT (content): Merge conflict in README. rstAutomatic merge failed; fix conflicts and then commit the result.

The following error occurs in README. rst:

<<<<<<< HEADmay be here is conflict=======add new branch dev >>>>>>> dev

<To =======\ indicates the content of the current branch, >>>> indicates the content in dev. Manually modify the content before submitting. The master is the latest file. Of course, dev is still in the last commit state. At this point, you may want to maintain the same status as the master on the dev branch, so you can:

git checkout devgit rebase master

It is equivalent to quickly pointing the dev branch to the master.

Branch Policy

Branch management should be performed as follows during development.
Main branch: the code library should have only one master branch. The code of the master branch is stable and only used for official release.

Development Branch: The daily development work should be completed on the Development Branch dev. When the dev Branch functions are improved at a certain time, you can consider merge to the master branch.

Self branch: each person builds his/her own branch on the dev branch.
By default, git uses the "fast forward" mode to merge and direct the master branch to the dev branch. After the branch is deleted, the branch information is also lost.

Include parameters when merging--no-ffYou can disable the fast-forward merge mode. In this way, a new node can be generated on the master, which means the Branch Information reserved by the master, which we hope to adopt in this merge mode.

git merge --no-ff dev

Ubuntu perfectly installs and builds a Git Server

GitHub Tutorials:

GitHub tutorials

Git tag management details

Git branch management

Git remote repository details

Git local Repository (Repository) Details

Git server setup and Client installation

Git Overview

Share practical GitHub tutorials

How to Build and use Git servers in Ubuntu

Git details: click here
Git: click here

This article permanently updates the link address:

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.