Git usage Summary

Source: Internet
Author: User
Tags git commands
Git Introduction

Recently, I want to use git to manage small projects. It should be that git can manage code on any Linux machine without the need of servers. It has advantages over SVN and CVS, so I chose
Git is used to manage my small projects and SVN management will be provided later.
After using it for a while, I want to write a summary, which may be similar to other git articles on the Internet. However, as a summary of my use, it is necessary.
The explanation of git Security lixnus is -- the stupid content tracker,
Dummies content tracker. Haha! In fact, it's not silly at all. It's quite intelligent. Maybe we should say it's "content tracker for stupid guy", huh, huh!
Git manages the creation of a storage repository locally, and all changes to the Code are stored locally. That is, the code and Management warehouse are invisible. You do not want SVN to be divided into clients and servers. Client only
Some simple repository information, and the real code and code changes are all stored on the server. Generally, the client can only obtain code files (only in general cases, but it can still be obtained if necessary ). Institute
Using git in this way can reduce the burden on the server-do not worry about Server failure or connection failure.

Git Configuration

So I should first talk about git Configuration:
Git commands are generally used in two forms:
Add). The other is a command for directly reducing the number of connections (for example, Git-add). The following describes how to use the latter to avoid problems caused by space usage.

helight@helight:~/mywork/zhwen.org$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/helight/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/helight/.ssh/id_rsa.
Your public key has been saved in /home/helight/.ssh/id_rsa.pub.
The key fingerprint is:
e0:4a:ba:d9:ba:b9:7a:0a:e4:aa:86:6c:a7:d8:85:c0 helight@helight
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . |
|. . . |
|.E . . S |
|o. + . |
|+.o o |
|==.B |
|X=@+. |
+-----------------+
helight@helight:~/mywork/zhwen.org$ vim /home/helight/.ssh/
id_rsa id_rsa.pub known_hosts

Generate a key for user communication and decryption. If default settings are accepted, 2048 bits is generated.
Ras key. The private key and public key file are located :~ /. Ssh/id_rsa and ~ /. Ssh/id_rsa.pub. The user must provide the public key to the server administrator.
(Id_rsa.pub), which authenticates the user when the user synchronizes the version library. The user must keep the private key properly.

helight@helight:~/kernel-mod/hello$ git-config user.name Zhwen Xu
helight@helight:~/kernel-mod/hello$ git-config user.email Helight.Xu@gmail.com

Configure the user name and use it when generating patches and logs. The git-config command with the -- global option is used to configure all user information. By default, only the current user is configured.
Write configuration information in Git-config. If the -- global option is added to git-config, the configuration information will be written ~ /. Gitconfig file.
Because you may use different identities to participate in different projects, and multiple projects are managed using git, we recommend that you do not use global configuration.
You can use other options of Git-config to configure git. -- list can be used to view the options that you have. For example:

helight@helight:~/kernel-mod/hello$ git-config --list
user.name=Zhwen Xu
user.email=Helight.Xu@gmail.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
helight@helight:~/kernel-mod/hello$
Use of Git

Command: Git-init-DB
Execute this command in the directory we want to manage to create the relevant git database file.
In this example, there are two files in the hello folder, a C program and a makefile.

helight@helight:~/kernel-mod/hello$ ls -a
. .. hello.c Makefile
helight@helight:~/kernel-mod/hello$

Then execute the preceding command:

helight@helight:~/kernel-mod/hello$ git-init-db 
Initialized empty Git repository in /home/helight/kernel-mod/hello/.git/
helight@helight:~/kernel-mod/hello$ ls -a
. .. .git hello.c Makefile
helight@helight:~/kernel-mod/hello$

After executing the preceding command, use "ls"
-A ", you will find an additional". Git "file, which is the GIT repository of the Hello small project. Readers interested in this repository can go to this folder to continue analysis.
Next we will introduce how to add files in the project to the repository and use git for version management. The command to add a file to the GIT repository is Git-Add. Of course, this does not really copy the file
Git repository folder. Just identify it in the repository to use git to manage it. The procedure is as follows:

helight@helight:~/kernel-mod/hello$ git-add hello.c 
helight@helight:~/kernel-mod/hello$ git-add *
helight@helight:~/kernel-mod/hello$

You can see that you can add a single file or multiple files together. Next, you can view the submission and project status. I believe that with the previous instructions, you may feel a little bit confused about git management.
Right!

View project submission and status:

Command: Git-commit, Git-status

helight@helight:~/kernel-mod/hello$ git-status 
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: Makefile
# new file: hello.c
#
helight@helight:~/kernel-mod/hello$ git-commit -m "init the project" ./*
Created initial commit f4808f0: init the project
2 files changed, 27 insertions(+), 0 deletions(-)
create mode 100644 Makefile
create mode 100644 hello.c
helight@helight:~/kernel-mod/hello$ git-status
# On branch master
nothing to commit (working directory clean)
helight@helight:~/kernel-mod/hello$

The first "Git-status" prompt tells us that two new files are added to the version Library (this is a change from the previous version), and git
We are prompted to submit these files. We can use the git-commit command to submit them. After the application is used again, the system prompts that the application has not changed.

Branch management:

Git mainly advocates branch management, which should be mastered by everyone who learns git.
View branch, establish branch, and switch Branch:

helight@helight:~/kernel-mod/hello$ git-branch 
* master
helight@helight:~/kernel-mod/hello$ git-branch helight
helight@helight:~/kernel-mod/hello$ git-branch
helight
* master
helight@helight:~/kernel-mod/hello$ git-checkout helight
Switched to branch "helight"
helight@helight:~/kernel-mod/hello$ git-branch
* helight
master
helight@helight:~/kernel-mod/hello$

It can be seen that "Git-branch" is used to view the branch status and create the branch, and "Git-checkout" is used to switch the branch. "*" Indicates the current branch.

Delete branch: Git-branch-D xxx
helight@helight:~/kernel-mod/hello$ git-branch xux
helight@helight:~/kernel-mod/hello$ git-branch
* helight
master
xux
helight@helight:~/kernel-mod/hello$ git-branch -D xux
Deleted branch xux.
helight@helight:~/kernel-mod/hello$ git-branch
* helight
master
helight@helight:~/kernel-mod/hello$
View branch differences: Git-show-branch, Git-diff, Git-whatchanged

After modifying the file, we can view it.

helight@helight:~/kernel-mod/hello$ git-show-branch 
* [helight] init the project
! [master] init the project
--
*+ [helight] init the project
Git-DIFF: differences between the last commit and the present
helight@helight:~/kernel-mod/hello$ git-diff
diff --git a/hello.c b/hello.c
index 843a6b8..c762de7 100644
--- a/hello.c
+++ b/hello.c
@@ -14,6 +14,7 @@ static int __init hello_init(void)
static void __exit hello_exit(void)
{
printk("kernel: %s/n","Bey world!");
+ printk("kernel: %s/n","Bey world!");
}

module_init(hello_init);
Git-commit: Submit
helight@helight:~/kernel-mod/hello$ git-commit -m "some change" ./*
Created commit 2d900d9: some change
1 files changed, 1 insertions(+), 0 deletions(-)
Git-whatchanged: view the changes of the current Branch
helight@helight:~/kernel-mod/hello$ git-whatchanged 
commit 2d900d918d24943b32f3d41b1974e0375be02c9e
Author: Zhenwen Xu
Date: Wed Nov 12 22:09:45 2008 +0800

some change

:100644 100644 843a6b8... c762de7... M hello.c

commit f4808f013f44e815831a3830a19925472be83424
Author: Zhenwen Xu
Date: Wed Nov 12 21:53:52 2008 +0800

init the project

:000000 100644 0000000... c151955... A Makefile
:000000 100644 0000000... 843a6b8... A hello.c
helight@helight:~/kernel-mod/hello$

For example, to view the differences between the master and helight versions, we can use the following command:

Helight @ helight :~ /Kernel-mod/Hello $ Git-diff helight master
Diff -- git a/Hello. c B/Hello. c
Index c762de7 .. 843a6b8 100644
--- A/Hello. c
++ B/Hello. c
@-14,7 + 14,6 @ static int _ init hello_init (void)
Static void _ exit hello_exit (void)
{
Printk ("kernel: % s/n", "Bey world! ");
-Printk ("kernel: % s/n", "Bey world! ");
}

Module_init (hello_init );
Helight @ helight :~ /Kernel-mod/Hello $
Patch creation: Git-format-patch
Helight @ helight :~ /Kernel-mod/Hello $ Vim hello. c
Helight @ helight :~ /Kernel-mod/Hello $ Git-commit-M "change by helight" Hello. c
Created commit 4772773: change by helight
1 files changed, 1 insertions (+), 0 deletions (-)
Make patches for sending emails:
helight@helight:~/kernel-mod/hello$ git-format-patch -s master
0001-change-by-helight.patch
helight@helight:~/kernel-mod/hello$ cat 0001-change-by-helight.patch
From 4772773ecbbde66b8febc1d8aed0da67d480f1e4 Mon Sep 17 00:00:00 2001
From: Zhenwen Xu
Date: Thu, 13 Nov 2008 10:30:10 +0800
Subject: [PATCH] change by helight


Signed-off-by: Zhenwen Xu
---
hello.c | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/hello.c b/hello.c
index 89795d2..2cbf9ee 100644
--- a/hello.c
+++ b/hello.c
@@ -6,6 +6,7 @@ MODULE_LICENSE("GPL");
static int __init hello_init(void)
{
printk("kernel: %s/n","Hello world!");
+printk("kernel: %s/n","This is Helight.Xu!");
return 0;
}

--
1.5.6.5

helight@helight:~/kernel-mod/hello$
Branch merge: Git-Merge

Now let's take a look at how to merge the work on the helight branch into the master branch. Transfer our current work branch
Master, and merge the work on the helight branch.

helight@helight:~/kernel-mod/hello$ git-checkout master
Switched to branch "master"
helight@helight:~/kernel-mod/hello$ git-merge "merge helight" HEAD helight
Updating f4808f0..2d900d9
Fast forward
hello.c | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
helight@helight:~/kernel-mod/hello$

But more is to put the current work pull to the main branch, the following command:

helight@helight:~/kernel-mod/hello$ vim hello.c 
helight@helight:~/kernel-mod/hello$ git-commit -m "another change" ./*
Created commit 1d6b878: another change
1 files changed, 0 insertions(+), 3 deletions(-)

Git-pull: Update jobs to the Branch

helight@helight:~/kernel-mod/hello$ git-checkout master
Switched to branch "master"
helight@helight:~/kernel-mod/hello$ git-pull . helight
From .
* branch helight -> FETCH_HEAD
Updating 2d900d9..1d6b878
Fast forward
hello.c | 3 ---
1 files changed, 0 insertions(+), 3 deletions(-)

Now let's take a look at how to return to the previous version: Git-Reset
Command Format:
Git-Reset [-- soft | -- hard] []
Command Options:
-- Soft
Changes made to the git-commit command remain unchanged.
-- Hard
Switch the content and header indexes in the work tree to the specified version location, that is, all the trace content and work tree content after the git-commit command is lost.
Therefore, this option should be used with caution unless you are very sure that you do not want to see any more.

View git information and logs:

Git-Log
Git-show
Git-show-branch
Git-show-Index

Git-show-ref

From: http://num7.javaeye.com/blog/704815

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.