git command Summary

Source: Internet
Author: User
Tags version control system

What is Git?

Git is a distributed version control system. It makes it easy to keep track of every change you make, without having to back it up every time and making it easy for you and others to develop together. So every time you make a change, a glance will be clear.

--

Install Git

Download the git for your computer from the official website, click Install, all the way "Next" will be OK. When the installation is complete, open the console to start setting git parameters.

$ git config--global user.name "xxx" $ git config--global user.email "[email protected]"//fill in the above two quotes with your name and email address separately. Since Git is a distributed version control system, there may be many users, each of whom needs to have their own name and mailbox to differentiate each other.

--

Create a repository (repository)

Simply put, you can understand the repository as a directory. We use Git to manage our files, so you have to tell git which directory you need it to manage, and this directory is the repository.

Git init//Create a learngit directory and go to learngit directory, create repository MD LEARNGITCD learngitgit Init

--

Add Files to Repository

Files created under the warehouse and directories need to be manually submitted to the warehouse. The submission is divided into two steps:
1.git add
2. git commit -m "Notes submitted"

First create a file called Readme.txt, which is entered

Git is amazing! I Love git!

The Readme.txt is then submitted to the warehouse:

> Git add Readme.txtwarning:LF'll be replaced by CRLF in Readme.txt.The file would have their original line endings in Your working directory.> git commit-m "Add a Readme file" [Master (Root-commit) e7ddd65] Add a readme filewarning:lf W Ill is replaced by CRLF in Readme.txt.The file would have the its original line endings in your working directory. 1 file changed, 3 insertions (+) Create mode 100644 readme.txt

--

Status of the workspace

Use git status to view status information for the current workspace

Change the README.MD revision to

Git is great! I Love git!

Then use the git status command

> git statuson branch masterchanges not staged for commit: (use "git add <file> ..." To update what would be Commi tted) (use "Git checkout--<file> ..." to discard changes in working directory) Modified:readme.txtno Cha Nges added to commit (use "git add" and/or "Git Commit-a")

--

See what's changed in a file

Although we can git status know that the file has been modified, we don't know what is being modified. So use git diff can know the specific changes.

> Git diff readme.txtdiff--git a/readme.txt b/readme.txtindex 2482f69. 8E882DD 100644---a/readme.txt+++ b/readme.txt@@ -1,3 +1,2 @@-git is Amazing!-i love git!-+git are great!+i love git!\ No N Ewline at end of FILEWARNING:LF would be replaced by CRLF in Readme.txt.The file would have their original line endings in yo ur working directory.

--

View the entire modification process status

Submit a modified file

> Git add readme.txt

Then look at the status of your current workspace

> Git statuswarning:lf would be replaced by CRLF in Readme.txt.The file would have their original line endings in your wor King directory. On Branch Masterchanges to being committed: (use "git reset HEAD <file> ..." to Unstage) modified:readme.txt

Continue to submit the modified file

> Git commit-m "amazing became great" [Master Warning:lf'll be is replaced by CRLF in Readme.txt.The file'll has its Original line endings in your working DIRECTORY.469DCDC] amazing became GREATWARNING:LF would be replaced by CRLF in read Me.txt.The file would have the IT original line endings in your working directory. 1 file changed, 1 insertions (+), 1 deletions (-)

Then look at the status of your current workspace

> Git Statuson branch masternothing to commits, working directory clean

--

Historical records

git logcommand to view all the historical records

> Git logcommit e4af028a493a4459fea0c0f673149281e0d11949author:liyang <[email protected]>date:tue APR 26 16:1 7:58 +0800 Add new linecommit 469dcdcd4de36234666885f87fa581441a834992author:liyang <[email protected]>Date : Tue Apr 16:07:00 +0800 amazing became greatcommit E7ddd658bf0e5791acd07c25a56ba1c4fba1a181author:liyang &L T [Email protected]>date:tue Apr 15:49:34 +0800 Add a Readme file

--

Historical records

git logThe command gives you every commit and displays it in chronological order, very detailed. But sometimes you just want a simple result that you cangit log --pretty=oneline

> Git log--pretty=onelinee4af028a493a4459fea0c0f673149281e0d11949 Add new line469dcdcd4de36234666885f87fa581441a834992 Amazing became greate7ddd658bf0e5791acd07c25a56ba1c4fba1a181 add a Readme file

--

Back to historical records

In git there is a pointer HEAD to HEAD which snapshot, which state you are in. For the version of the nth stateHEAD~N

> Git reset--hard head~2head is now at e7ddd65 add a Readme file

--

Failed to return historical record

What if I want to go back to add new line this version now? Very simple, as long add new line as commit id you know. So you take it for granted. git log View commit id :

> Git log--pretty=oneline469dcdcd4de36234666885f87fa581441a834992 amazing became greate7ddd658bf0e5791acd07c25a56ba1c4fba1a181 Add a Readme file

add new lineIt's gone! Remember git log that you can only view previous versions of Head and head.

--

View all historical operations

git reflogThis command can be used to view all operation commands

> git reflog469dcdc [email protected]{0}: reset:moving to head~1e4af028 [email protected]{3}: Commit:add new line469d CDC [Email protected]{4}: commit:amazing became greate7ddd65 [email protected]{5}: Commit (initial): Add a Readme file

Then you'll know add new line id , and you'll be happy to go back.

> Git reset e4af028unstaged changes after reset:m readme.txt

--

Work area

The workspace is the directory where you store all your files. For example, we created a new directory, called learnGit , and then entered learnGit , in the learnGit directory using the git init command to learnGit become a git can manage the directory. git automatically generates a hidden directory called .git . At this point, the Learngit directory is the workspace.

--

Version Library

Each of the workspaces has a hidden directory called .git . .gitis the repository. This is the place that you commit to commit.

--

Staging Area

Although the .git directory is hidden, you can still open it. Open .git it and you'll find a lot of directories and files in it. There is a file named index, the index file is the staging area stage. Staging area is a temporary storage location in the repository, and is submitted to the repository via staging area.

Logical process for GIT version control:

    1. First, you create a file or modify a file in the workspace.

    2. Then you have the input git add , and the file is actually added to the staging area stage, which is the index file

    3. And then you type git commit it, and that's a formal submission. git defaults to creating a master branch and a head pointer to the master branch.


--

Complex modification Recovery Cnd.1 1th case: Changes in the workspace have not yet git addAdd to Staging area. Of course you can just open the editor and change it back to the way it was before, but we want git to do it for us, and we can execute the command. git checkout -- file

For example, you added a new line of text in Readme.txt.

Git is great! I Love Git!just Add line

This time there is a change, and now you regret that you should not add this line.

$ git checkout--readme.md

There are no prompts after entering the command. There is no hint that you are right. Then you open the Readme.txt and look at it and find that the line you just added is just add line gone.


--

Complex modification Recovery Cnd.2 2nd case: Modifications generated in the workspace are added git addto the staging area. At this point, you can only call this change back to the workspace, and then kill the change in the workspace. Can execute command line git reset HEAD file
> Git reset HEAD readme.txtunstaged changes after reset:m readme.txt> git checkout readme.txt


--

deleting files Part.1

Deleting a file is also a modification because something in the workspace has changed

Create a new file, then git add, then git commit

> Echo >new.txt> git add new.txt> git commit-m "Create New.txt" [detached HEAD 21e3fe5] Create new.txt 1 file Changed, 1 insertion (+) Create mode 100644 New.txt

--

deleting files Part.2

And then we'll delete it.

> del new.txt

At this point git finds that you deleted a file in your working directory, but there is still a new.txt in the Git repository.

> Git statushead detached from 9baed55changes not staged for commit: (use "Git add/rm <file> ..." to update 'll be committed) (use "Git checkout--<file> ..." to discard changes in working directory) deleted:new . Txtno changes added to commit (use "git add" and/or "Git Commit-a")

--

deleting files Part.3

If you're sure you want to remove new.txt, you need to execute the command.git rm file

> Git rm new.txtrm ' new.txt '

Again git status , I found out that your operation has not been formally submitted.

> Git statushead detached from 9baed55changes to being committed: (use "git reset HEAD <file> ..." to Unstage) Deleted:new.txt

You need to againgit commit

> Git commit-m "del new.txt" [detached HEAD 5e46854] del new.txt 1 file changed, 1 deletion (-) Delete mode 100644 new.t Xt

--

Create a GitHub account Part.1

On GitHub's website, register for an account and click after registering Settings .


Select in the left column SSH keys , clickNew SSH keys


--

Create a GitHub account Part.2

Go to the directory of the GIT installation directory \usr\bin and enter the following command at the command line

Replace the [email protected] section with your e-mail address, and then return to the same place. This generates a key pair under the user's home directory (C:\Documents and Settings\ user name \.ssh)//Id_rsa is the private key, Id_rsa.pub is the public key $ ssh-keygen-t rsa-c "[Email Protected] "

Go back to GitHub and fill in the title with a name, then id_rsa.pub paste the contents into the key bar and click on the Add SSH key button to finish.


--

Create a GitHub Warehouse

To create a repository on GitHub, clickNew repository


Fill in a Repository name column with a name as the warehouse name. Then click the Create repository button directly.


--

GitHub Warehouse

The basic interface of the warehouse is as follows, because we are using the SSH protocol, not the HTTPS protocol, so we switch the default HTTPS to SSH


--

Associate a remote Warehouse

The first case: you haven't built a warehouse locally yet.

echo "# learngit" >> readme.mdgit initgit Add readme.mdgit commit-m "First commit" git remote add origin [email prot Ected]:liyang/learngit.gitgit Push-u Origin Master

The second case: if you already have a warehouse locally

git remote add origin [email Protected]:liyang/learngit.gitgit push-u Origin Master

--

Create a branch Part.1

First create a Test.txt file and enter it inside:

# This is a test file

Then submit it:

$ git add test.txt$ git commit-m "Add a test file"

Then create a dev branch.

$ git Branch Dev

--

Create a branch Part.2

This creates a branch called Dev. But don't forget, we're still in the master branch.

$ git Branch dev* master//git branch lists all the branches and adds an * number to the front of the current branch.

If you need to switch to the Dev branch, simply perform the

The syntax for git checkout dev//undo modification is: Git checkout--file//and the Switch Branch command does not--these two short cross.

If you want to create a branch and switch directly to that branch, just do

$ git chekcout-b dev//This will create the Dev branch and switch directly to the dev branch.

--

Modify Branch Part.1

Make changes in the Dev branch. Add a line behind Test.txt

# This was a test Filefirst line

Proceed to submit

$ git add test.txt$ git commit-m "add first line on Dev"

Then switch back to the master branch:

$ git Checkout Master

When you switch back to the master branch, open Test.txt and find that the line you just added is missing because "first" is submitted on the Dev branch.

# This is a test file

--

Merging branches

Now on the Master branch, you are ready to merge with the Dev branch:

$ git merge devupdating 0fed6b6. 38fb696fast-forwardtest.txt | 1 +1 file changed, 1 insertion (+)

After the merge is complete, you can delete the dev branch

$ git branch-d dev

--

Crawl Branch

git clone urlRemote repositories can be cloned to local

git clone https://github.com/roastlechon/nodejs-rtorrent.git

git command Summary

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.