Git version controller is easy to use

Source: Internet
Author: User
Tags git commands

1. Git Overview

The latest version of git is 2.0.2. We recommend that you use command line in Linux. Git -- version to view the current git version.

Compared with version controllers such as SVN and CVS, git does not need to log on to the server, that is, it can also be used offline.

Resources: http://gitref.org/


Git and GitHub:

Git is a version control system. Similar to SVN and CVS. GitHub is a website that provides git services to users. In this way, you don't need to deploy the Git system on your own, just register an account and use the GIT service they provide.

Ii. installation and configuration

Install: sudo apt-Get install git

Configuration: Two Methods

1. Command Line

Git config -- global user. Name = yourname

Git config -- global user. Email = youremail

Git config -- Global color. UI = true

2. Edit ~ /. Gitconfig File

Git config -- list to view configuration content

3. Create a repository (repo for short)

  1. Initialization

Git Init (generate a hidden. Git folder, which can be viewed by LS-)

2. clone a project on the GitHub website

Git clone https://github.com/pythonhackers/pythonhackers.git

(The pythonhackers folder is generated in the current directory)

Iv. Common git commands

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/A6/wKiom1RWMvKg_u8NAAPo5AP95ro347.jpg "Title =" git.png "alt =" wkiom1rwmvkg_u8naapo5ap95ro347.jpg "/> For example: WD is short for working directory, stage & history. Our goal is to synchronize the code. py edited in WD to history. Stage is the middle layer of history and WD.

  1. Git status [-S]

    After the GIT status-s command is executed, the following code is generated:

Example: A test1.py

M test2.py

If column A is displayed, the test1.py version in stage and history is inconsistent. If column M is displayed, the test2.py version of WD and stage is inconsistent.

Execute echo '*~ '>. Gitignore can make git ignore ~ File, that is, some backup files.

  1. Git Add code. py (WD-> stage) undo: git checkout code. py (stage-> WD)

  2. Git commit-M 'update' (stage-> history) undo: git reset code. py (history-> stage)

  3. Git commit-am 'second Update' (WD-> history) undo: git checkout head code. py (history-> WD)

  1. Git diff (view the code difference between stage and WD)

  2. Git diff-staged (view the code difference of History & stage)

  3. GIF diff head (view the code differences of History & WD)

Notice: git diff -- stat can output brief diff information.

Remove files and change file names

Note: git does not recognize the file name, but only the file content. Even if the file is renamed with the same content, it is considered unchanged.

  1. Git RM old. py

  2. Git Rm -- cached code. py (retain code. py of WD and delete code. py in stage)

  3. Git MV old.txt old.doc (if the content is the same, it is still considered unchanged)

It is equivalent to the following command:

Git Rm -- cached old.txt

MV old.txt old.doc

Git add old.doc

Notice: git automatically determines whether the file content is changed and does not bind the file name to the file content.

Situation: when you are modifying the code. py code, someone will tell you that your previous code. py has a bug. What should I do? I am modifying code. py, but the Code has not been tested. I need to go back to the original code. py and change it to go online again. At this time, you can use

Git stash (clean up the current desktop and roll back to the original status. After fixing, take out the desktop and continue code)

Git stash list

Git stash pop

The history we mentioned above is actually a commit object, which can be accessed through a 40-byte hash code.

Git log (-- oneline) outputs the repository log.

Powerful command: git cat-file [-T,-p] [hash code or object]

-T output type: commit, tree, and blog types

-P: output the file content.

For example, git cat-file-T head (Head ~ *) (* Is a positive integer)

Git rev-Parse head outputs a 40-byte hash of the head.

Git rev-Parse head/Master (when there is only one branch, the head is equivalent to the master)

In addition, head ~ 4 ^ {tree}, head ~ 4: code. py (the path starts from the root directory, that is, the directory where. Git is located)

Cat-file-P equals show

The preceding content belongs to the tree-ish part.

Create another branch: If you have a new idea, but you don't want to code it under the current master branch, because you are not sure yet. At this time, you can:

  1. Git branch tryidea

  2. Git branch allows you to view all branches of the current project:

* Master

Tryidea

3. Switch to the tryidea branch through git checkout tryidea: Run git branch again: there will be:

Master

* Tryidea

You can also directly run git branch-B tryidea at the time of creation, that is, switch to tryidea while creating a new one.

Delete branch: git branch-D tryidea (provided that you have moved the content of the tryidea branch to the master Branch)

Git merge tryidea

Two types:

  1. Fast-forward: only the content of the tryidea branch is modified.

  2. 3-way merge: both master and tryidea are modified.

Attach two logical diagrams of the commit object:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/A5/wKioL1RWPKrgeXVhAAO1YpiQsyo817.jpg "style =" float: none; "Title =" commit .jpg "alt =" wkiol1rwpkrgexvhaao1ypiqsyo817.jpg "/>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/A6/wKiom1RWPFbC53HlAAPcXOktk-A170.jpg "style =" float: none; "Title =" commit has.png "alt =" wKiom1RWPFbC53HlAAPcXOktk-A170.jpg "/>

.

This article is from the "blue fin dolphin" blog, please be sure to keep this source http://likunheng.blog.51cto.com/9527645/1571028

Git version controller is easy to use

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.