Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。
Git 与 SVN 最大的区别:GIT是分布式的,SVN是不是。
git config [--global] user.name <name> 设置用户名
git config [--global] user.email <email> 设置邮箱
git config [--global] core.editor <editor> 设置编辑器
git config [--global] github.user <user> 设置github帐号名
git config [--global] github.token <token> 设置github的token
--global是对当前系统用户的全局设置,在~/.gitconfig中。对系统所有用户进行配置,/etc/gitconfig。对当前项目,.git/config
2.基本操作:
假设创建一个名为test的仓库
[root@VM_41_84_centos wee]# git init test Initialized empty Git repository in /home/weelin/test/.git/ [root@VM_41_84_centos wee]# cd test [root@VM_41_84_centos test]# ls -la total 12 drwxr-xr-x 3 root root 4096 Mar 6 15:27 . drwx------ 9 weelin wee 4096 Mar 6 15:27 .. drwxr-xr-x 7 root root 4096 Mar 6 15:27 .git [root@VM_41_84_centos test]#
分支操作之前我们要保证‘test‘仓库有内容并且已经提交(commit)过
[root@VM_41_84_centos test]# echo "add readme file">readme #创建一个readme文件 [root@VM_41_84_centos test]# git status #查看仓库状态 # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # readme nothing added to commit but untracked files present (use "git add" to track) [root@VM_41_84_centos test]# git add readme #把readme假如缓冲区 [root@VM_41_84_centos test]# git commit -m "add readme" #把缓冲区内容提交到仓库 [master (root-commit) 4e35ce8] add readme Committer: root <root@VM_41_84_centos.(none)> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.com If the identity used for this commit is wrong, you can fix it with: git commit --amend --author=‘Your Name <you@example.com>‘ 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 readme [root@VM_41_84_centos test]# git status # On branch master nothing to commit (working directory clean)
[root@VM_41_84_centos test]# git branch #查看分支,此时只有一个主分支master
* master
[root@VM_41_84_centos test]# git branch dev #创建一个dev的分支
[root@VM_41_84_centos test]# git branch dev1 #创建一个dev1的分支
[root@VM_41_84_centos test]# git branch -a #查看分支,此时新分支dev,dev1创建成功(*号表示当前所在分支)
dev
dev1 * master
[root@VM_41_84_centos test]# git branch -d dev1 Deleted branch dev1 (was 4e35ce8). [root@VM_41_84_centos test]# git branch -a dev * master
[root@VM_41_84_centos test]# git checkout dev Switched to branch ‘dev‘ [root@VM_41_84_centos test]# git branch * dev master
[root@VM_41_84_centos test]# git branch * dev master [root@VM_41_84_centos test]# git checkout -b dev2 Switched to a new branch ‘dev2‘ [root@VM_41_84_centos test]# git branch dev * dev2 master
[root@VM_41_84_centos test]# git diff diff --git a/readme b/readme index f32981e..5b7babd 100644 --- a/readme +++ b/readme @@ -1 +1 @@ -add readme file +add readme filea [root@VM_41_84_centos test]# cat readme add readme filea
[root@VM_41_84_centos test]# git log --pretty=oneline 4e35ce8cf4b27255b2e025568162b074e1b0ab77 add readme
[root@VM_41_84_centos test]# git log
commit 4e35ce8cf4b27255b2e025568162b074e1b0ab77
Author: root <root@VM_41_84_centos.(none)>
Date: Mon Mar 6 15:32:47 2017 +0800
add readme
[root@VM_41_84_centos test]# git reflog
4e35ce8 HEAD@{0}: checkout: moving from dev to dev2
4e35ce8 HEAD@{1}: checkout: moving from master to dev
[root@VM_41_84_centos test]# echo add new line>>readme #追加一行内容 [root@VM_41_84_centos test]# cat readme add readme filea add new line [root@VM_41_84_centos test]# git add readme #添加到缓存区 [root@VM_41_84_centos test]# git checkout -- readme #后悔了,不想添加这一行了,尝试撤销 [root@VM_41_84_centos test]# git status # On branch dev2 # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: readme # [root@VM_41_84_centos test]# cat readme #没有成功,因为已近添加进缓冲区 add readme filea add new line [root@VM_41_84_centos test]# git reset --hard #换种方法尝试 HEAD is now at 4e35ce8 add readme [root@VM_41_84_centos test]# git status # On branch dev2 nothing to commit (working directory clean) [root@VM_41_84_centos test]# cat readme #成功撤销缓冲区和工作区更改 add readme file [root@VM_41_84_centos test]#
[root@VM_41_84_centos test]# git status # On branch master nothing to commit (working directory clean) [root@VM_41_84_centos test]# echo newline1>>readme [root@VM_41_84_centos test]# git add readme readme [root@VM_41_84_centos test]# git commit -m "add newline1" [master 380db57] add newline1 Committer: root <root@VM_41_84_centos.(none)> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.com If the identity used for this commit is wrong, you can fix it with: git commit --amend --author=‘Your Name <you@example.com>‘ 1 files changed, 1 insertions(+), 0 deletions(-) [root@VM_41_84_centos test]# git log commit 380db57859995a678b1a54261ab1d43220fe3be1 Author: root <root@VM_41_84_centos.(none)> Date: Mon Mar 6 16:31:29 2017 +0800 add newline1 commit 4e35ce8cf4b27255b2e025568162b074e1b0ab77 Author: root <root@VM_41_84_centos.(none)> Date: Mon Mar 6 15:32:47 2017 +0800 add readme [root@VM_41_84_centos test]# cat readme add readme file newline1 [root@VM_41_84_centos test]# echo newline2>>readme [root@VM_41_84_centos test]# git add readme [root@VM_41_84_centos test]# git commit -m "add newline2" [master b63dd68] add newline2 Committer: root <root@VM_41_84_centos.(none)> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.com If the identity used for this commit is wrong, you can fix it with: git commit --amend --author=‘Your Name <you@example.com>‘ 1 files changed, 1 insertions(+), 0 deletions(-) [root@VM_41_84_centos test]# git log commit b63dd686a86a1fb130b9749f170a44a71d9e7a8b Author: root <root@VM_41_84_centos.(none)> Date: Mon Mar 6 16:35:02 2017 +0800 add newline2 commit 380db57859995a678b1a54261ab1d43220fe3be1 Author: root <root@VM_41_84_centos.(none)> Date: Mon Mar 6 16:31:29 2017 +0800 add newline1 commit 4e35ce8cf4b27255b2e025568162b074e1b0ab77 Author: root <root@VM_41_84_centos.(none)> Date: Mon Mar 6 15:32:47 2017 +0800 add readme [root@VM_41_84_centos test]# cat readme add readme file newline1 newline2 [root@VM_41_84_centos test]# git reset --hard 4e35ce8cf4 #回退到4e35ce8cf4b27255b2e025568162b074e1b0ab77 HEAD is now at 4e35ce8 add readme [root@VM_41_84_centos test]# cat readme add readme file [root@VM_41_84_centos test]# git reset --hard 380db5785 #回退到380db57859995a678b1a54261ab1d43220fe3be1
HEAD is now at 380db57 add newline1
[root@VM_41_84_centos test]# cat readme
add readme file
newline1
[root@VM_41_84_centos test]# git reset --hard b63dd686a86 #回退到b63dd686a86a1fb130b9749f170a44a71d9e7a8b
HEAD is now at b63dd68 add newline2
[root@VM_41_84_centos test]# cat readme
add readme file
newline1
newline2
[root@VM_41_84_centos test]# git tag v0.01 -m "v0.0.1 on master" #创建tag v0.01 [root@VM_41_84_centos test]# git tag #查看所有tag v0.01 [root@VM_41_84_centos test]# git tag v0.02 #创建tag v0.02 [root@VM_41_84_centos test]# git tag v0.01 v0.02 [root@VM_41_84_centos test]# git tag -d v0.02 #删除tag v0.02 Deleted tag ‘v0.02‘ (was b63dd68) [root@VM_41_84_centos test]# git tag v0.01
3.与中心库交互
原文:http://www.cnblogs.com/diaosir/p/6510676.html