Git 操作

Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。

Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。

GitSVN 最大的区别:GIT是分布式的,SVN是不是。

 

1.Git 配置

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.基本操作:

  • 仓库的创建:git init 仓库名

    假设创建一个名为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]# 
  • 查看仓库当前状态:git status
  • 把工作区修改的文件添加到缓存区:git add filename(或者git add .)
  • 将缓存区内容添加到仓库中:git commit -m "备注内容"
  • 查看分支:git branch/git branch -a

 

  分支操作之前我们要保证'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                         
  • 创建一个分支:git branch 分支名
[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
  • 删除分支:git branch -d  分支名
[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
  • 切换分支:git checkout 分支名
[root@VM_41_84_centos test]# git checkout dev
Switched to branch 'dev'
[root@VM_41_84_centos test]# git branch
* dev
  master
  • 想在创建某一个分支的同时直接进入该分支,可以使用checkout -b 分支名
[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
  • 查看修改内容:git diff
[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
  • 查看提交历史:git log --pretty=oneline/git log/git reflog(查看操作历史)
[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
  • 撤销工作区和缓存区的修改:git reset --hard (修改了文件并且执行了git add filename)
  • 撤销工作区的修git checkout -- readme.txt (只是修改了文件,但是没有使用add)
[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]# 
  • 版本回退git reset --hard HEAD^(相对当前版本的上一个版本)或者git reset --hard 版本号(任何版本)
[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
  •  给分支打上标签:git tag v0.0.1 -m "tag comment"
[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.与中心库交互

  • 从中心库克隆一个仓库到本地:git clone <url>
  • 从中心库获取最新版本到本地,不会自动merge:git fetch [orgin master](相比git pull origin master更安全)
  • 把从远程库获取的最新版本合并到本地:git merge origin/master
  • 从中心库获取新版本并进行merge:git pull origin master(相当于上面两条:git fetch orgin master+git merge origin/master)
  • 将本地修改提交到中心库:git push -u origin master

 

posted @ 2017-03-06 18:26  weelin_area  阅读(392)  评论(0编辑  收藏  举报