首页 > 代码库 > git 基本操作

git 基本操作

git基本操作

操作系统:centos6.5 X86

注意:

1?要先在github里注册一个账号,下面有用到

安装服务
yum -y install git
 
1、设置用户名
git config --global user.name “IterCast”
2、设置用户邮箱
git config --global user.email “demo@itercast.com”
3、查看设置
git config --list
 
git命令帮助
 
可以通过以下命令获取git帮助
git help
可以通过以下命令获取特定指令的帮助
git help 特定指令
 
初始化一个新的git仓库
1、创建一个文件夹(或使用已有文件夹)
mkdir gitfile
2、在文件夹内初始化git,创建git仓库
cd gitfile
git init
 
向仓库添加文件
1、创建文件
touch README
touch hellogit.rb
写入测试内容
vim hellogit.rb
rubu “hello git!”
 
2、添加文件

[root@text3 gitfile]# git add README

[root@text3 gitfile]# git add hellogit.rb 

[root@text3 gitfile]# git status

# On branch master

#

# Initial commit

#

# Changes to be committed:

#   (use "git rm --cached <file>..." to unstage)

#

# new file:   README

# new file:   hellogit.rb

 
3、提交文件
技术分享

[root@text3 gitfile]# git commit -m "init repo” ———创建一个说明为”init repo"

[master (root-commit) a832a14] init repo

 1 files changed, 1 insertions(+), 0 deletions(-)

 create mode 100644 README

 create mode 100644 hellogit.rb

[root@text3 gitfile]# git status

# On branch master

nothing to commit (working directory clean)

 
4、查看提交历史日志
git log
 
 
 
5、直接提交到仓库(不暂存)
 

[root@text3 gitfile]# git commit -a -m "modify README"

[master c455ba6] modify README

 1 files changed, 1 insertions(+), 0 deletions(-)

 

 

[root@text3 gitfile]# git status

# On branch master

nothing to commit (working directory clean)

 

 

6、删除文件

1)、删除文件

rm README

2)、从git中删除文件

git rm README

3)、提义操作

git commit -m “delete README”

技术分享 

 

7、重命名文件

重命名文件

git mv hellogit.rb helloword.rb

git commit -m “rename hellogit.rb"

 

8、查看文件差别

[root@text3 gitfile]# git diff

diff --git a/code.py b/code.py

index dde56c2..1e65e5e 100644

--- a/code.py

+++ b/code.py

@@ -1,4 +1,4 @@

 #!/usr/bin/env python

 print "welcome to git!"

-print "welcome to file"

+print "welcome to beijing"

 

 print "welcome to shanghai"

 

[root@text3 gitfile]# git diff HEAD

diff --git a/code.py b/code.py

index 1e65e5e..de898f0 100644

--- a/code.py

+++ b/code.py

@@ -2,3 +2,7 @@

 print "welcome to git!"

 print "welcome to beijing"

 print "welcome to shanghai"

+

+print "fkfkfkfkfk"

+print "fffffff"

 

+print "lslslsl"

 

 

9、撤销误操作

[root@text3 gitfile]# git add code.py

[root@text3 gitfile]# git reset code.py

Unstaged changes after reset:

M code.py

[root@text3 gitfile]# git status -s

 M code.py

[root@text3 gitfile]# git add code.py

[root@text3 gitfile]# git reset code.py

Unstaged changes after reset:

M code.py

[root@text3 gitfile]# git diff

diff --git a/code.py b/code.py

index 1e65e5e..de898f0 100644

--- a/code.py

+++ b/code.py

@@ -2,3 +2,7 @@

 print "welcome to git!"

 print "welcome to beijing"

 print "welcome to shanghai"

+

+print "fkfkfkfkfk"

+print "fffffff"

+print "lslslsl"

 

[root@text3 gitfile]# 

 

还原修改前

[root@text3 gitfile]# git

git                 git-receive-pack    git-shell           git-upload-archive  git-upload-pack

[root@text3 gitfile]# git checkout code.py 

[root@text3 gitfile]# git diff

[root@text3 gitfile]# cat code.py 

#!/usr/bin/env python

print "welcome to git!"

print "welcome to beijing"

 

print "welcome to shanghai"

 

 

方法二

[root@text3 gitfile]# vim code.py 

[root@text3 gitfile]# 

[root@text3 gitfile]# git diff

diff --git a/code.py b/code.py

index 1e65e5e..699862d 100644

--- a/code.py

+++ b/code.py

@@ -2,3 +2,12 @@

 print "welcome to git!"

 print "welcome to beijing"

 print "welcome to shanghai"

+print "welcome to shanghai"

+print "welcome to shanghai"

+print "welcome to shanghai"

+print "welcome to shanghai"

+print "welcome to shanghai"

+print "welcome to shanghai"

+

+

+

[root@text3 gitfile]# git checkout HEAD code.py 

[root@text3 gitfile]# git

git                 git-receive-pack    git-shell           git-upload-archive  git-upload-pack

[root@text3 gitfile]# git diff HEAD

[root@text3 gitfile]# cat code.py 

#!/usr/bin/env python

print "welcome to git!"

print "welcome to beijing"

 

print "welcome to shanghai"

 

 

 

 

10、暂存工作区

[root@text3 gitfile]# vim code.py  

[root@text3 gitfile]# git stash

Saved working directory and index state WIP on master: d34f019 update code

HEAD is now at d34f019 update code

[root@text3 gitfile]# git status

# On branch master

nothing to commit (working directory clean)

[root@text3 gitfile]# ls

code.py  hellogit.rb  README

[root@text3 gitfile]# cat code.py 

#!/usr/bin/env python

print "welcome to git!"

print "welcome to beijing"

print "welcome to shanghai"

[root@text3 gitfile]# git commit -am ‘quick fix‘

# On branch master

nothing to commit (working directory clean)

[root@text3 gitfile]# git status

# On branch master

nothing to commit (working directory clean)

[root@text3 gitfile]# 

[root@text3 gitfile]# 

[root@text3 gitfile]# git status list

# On branch master

nothing to commit (working directory clean)

[root@text3 gitfile]# ls

code.py  hellogit.rb  README

[root@text3 gitfile]# cat code.py 

#!/usr/bin/env python

print "welcome to git!"

print "welcome to beijing"

print "welcome to shanghai"

[root@text3 gitfile]# git stash list

stash@{0}: WIP on master: d34f019 update code

[root@text3 gitfile]# git stash pop

# On branch master

# Changed but not updated:

#   (use "git add <file>..." to update what will be committed)

#   (use "git checkout -- <file>..." to discard changes in working directory)

#

# modified:   code.py

#

no changes added to commit (use "git add" and/or "git commit -a")

Dropped refs/stash@{0} (46a1a4453974dd1203d1c49da7da2e524e351573)

[root@text3 gitfile]# git status -s

 M code.py 

[root@text3 gitfile]# ls

code.py  hellogit.rb  README

[root@text3 gitfile]# cat code.py 

#!/usr/bin/env python

print "welcome to git!"

print "welcome to beijing!"

print "welcome to shanghai!"

[root@text3 gitfile]# git commit -am ‘update 2 files‘

[master 2aa1347] update 2 files

 1 files changed, 2 insertions(+), 2 deletions(-)

[root@text3 gitfile]# git status -s

[root@text3 gitfile]# ls

code.py  hellogit.rb  README

[root@text3 gitfile]# cat code.py 

#!/usr/bin/env python

print "welcome to git!"

print "welcome to beijing!"

 

print "welcome to shanghai!"

 

11、图解commit对象

 技术分享

[root@text3 gitfile]# tree 

.

├── code.py

├── hellogit.rb

└── README

 

0 directories, 3 files

[root@text3 gitfile]# gi

gif2tiff             gio-querymodules-32  git                  git-shell            git-upload-pack

gindxbib             gio-querymodules-64  git-receive-pack     git-upload-archive   

[root@text3 gitfile]# git

git                 git-receive-pack    git-shell           git-upload-archive  git-upload-pack

[root@text3 gitfile]# git cat-file HEAD

usage: git cat-file (-t|-s|-e|-p|<type>) <object>

   or: git cat-file (--batch|--batch-check) < <list_of_objects>

 

<type> can be one of: blob, tree, commit, tag

    -t                    show object type

    -s                    show object size

    -e                    exit with zero when there‘s no error

    -p                    pretty-print object‘s content

    --batch               show info and content of objects fed from the standard input

    --batch-check         show info about objects fed from the standard input

 

[root@text3 gitfile]# git cat-file -t HEAD

commit

[root@text3 gitfile]# git cat-file -p HEAD

tree 2bc565bcf00e598163fed6efcf90b599f00bdd18

parent d34f019465356ccc587950ab67f0f4c922528ae2

author Tale <MAIL> 1459508052 +0800

committer Tale <MAIL> 1459508052 +0800

 

update 2 files

[root@text3 gitfile]# git cat-file -t 2bc565

tree

[root@text3 gitfile]# git cat-file -p 2bc565

100644 blob fdf22703338a6719d70e608b60203e777e17fff2 README

100644 blob f848bb38ff08b007643154a1dd19df8c79785553 code.py

100644 blob 5598838d8900dfccfe8cf6581d4292a65ae45221 hellogit.rb

[root@text3 gitfile]# git cat-file -p f848b

#!/usr/bin/env python

print "welcome to git!"

print "welcome to beijing!"

 

print "welcome to shanghai!"

 

 

12、理解tree-ish表达式

[root@text3 gitfile]# cat .git/refs/heads/master 

2aa134703bed75606470b05042f935828a0e63bb

[root@text3 gitfile]# git cat-file -p 2aa134703bed75606470b05042f935828a0e63bb

tree 2bc565bcf00e598163fed6efcf90b599f00bdd18

parent d34f019465356ccc587950ab67f0f4c922528ae2

author Tale <******> 1459508052 +0800

committer Tale <******> 1459508052 +0800

 

update 2 files

[root@text3 gitfile]# git log --oneline

2aa1347 update 2 files

d34f019 update code

7f44e04 add code

6cd3c89 delete Tale.py

b7641b0 text

c455ba6 modify README

a832a14 init repo

[root@text3 gitfile]# git rev-parse HEAD

2aa134703bed75606470b05042f935828a0e63bb

[root@text3 gitfile]# git rev-parse HEAD~

d34f019465356ccc587950ab67f0f4c922528ae2

[root@text3 gitfile]# git rev-parse HEAD~4

b7641b0dc821d05acf482dba50647820855f7995

[root@text3 gitfile]# git rev-parse master~4

b7641b0dc821d05acf482dba50647820855f7995

[root@text3 gitfile]# git rev-parse HEAD~4^{tree}

2d42961022572caa211f0502f6a0ec4d309bce35

[root@text3 gitfile]# git cat-file -t 2b4296

fatal: Not a valid object name 2b4296

[root@text3 gitfile]# git cat-file -t 2d4296

tree

[root@text3 gitfile]# git cat-file -p 2d4296

100644 blob fdf22703338a6719d70e608b60203e777e17fff2 README

100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 Tale.py

100644 blob 5598838d8900dfccfe8cf6581d4292a65ae45221 hellogit.rb

[root@text3 gitfile]# git rev-parse HEAD~4:code.py

 

 

[root@text3 gitfile]# git show HEAD~4:code.py 

 

 

13、创建及删除分支

[root@text3 gitfile]# git branch

* master

[root@text3 gitfile]# git branch tryidea

[root@text3 gitfile]# git branch

* master

  tryidea

[root@text3 gitfile]# git checkout tryidea

Switched to branch ‘tryidea‘

[root@text3 gitfile]# git branch

  master

* tryidea

[root@text3 gitfile]# ls .git/refs/heads/

master   tryidea  

[root@text3 gitfile]# ls .git/refs/heads/

master  tryidea

[root@text3 gitfile]# cat .git/refs/heads/

cat: .git/refs/heads/: 是一个目录

[root@text3 gitfile]# cat .git/refs/heads/*

2aa134703bed75606470b05042f935828a0e63bb

2aa134703bed75606470b05042f935828a0e63bb

[root@text3 gitfile]# cat .git/HEAD 

ref: refs/heads/tryidea

[root@text3 gitfile]# git checkout master

Switched to branch ‘master‘

[root@text3 gitfile]# 

[root@text3 gitfile]# 

[root@text3 gitfile]# git branch -d tryidea

Deleted branch tryidea (was 2aa1347).

[root@text3 gitfile]# cat .git/refs/heads/*

 

2aa134703bed75606470b05042f935828a0e63bb

 

14、合并分支

[root@text3 gitfile]# git checkout -b tryidea

Switched to a new branch ‘tryidea‘

[root@text3 gitfile]# git branch

  master

* tryidea

[root@text3 gitfile]# ls

code.py  hellogit.rb  README

[root@text3 gitfile]# vim code.py

[root@text3 gitfile]# git commit -am ‘new idea‘

[tryidea 18289a3] new idea

 1 files changed, 1 insertions(+), 0 deletions(-)

[root@text3 gitfile]# git checkout master

Switched to branch ‘master‘

[root@text3 gitfile]# git branch

* master

  tryidea

[root@text3 gitfile]# git branch -d tryidea

error: The branch ‘tryidea‘ is not fully merged.

If you are sure you want to delete it, run ‘git branch -D tryidea‘.

[root@text3 gitfile]# git merge tryidea

Updating 2aa1347..18289a3

Fast-forward

 code.py |    1 +

 1 files changed, 1 insertions(+), 0 deletions(-)

[root@text3 gitfile]# git log

commit 18289a315717e714ec8eb2b46a1a7a51147b3ffe

Author: Tale <********>

Date:   Fri Apr 1 20:29:36 2016 +0800

 

    new idea

 

 

commit c455ba68126ea42af3189b0925b67a66c44da1b1

[root@text3 gitfile]# git branch -d tryidea

Deleted branch tryidea (was 18289a3).

[root@text3 gitfile]# git branch

 

* master

 

 

 

 

 

git 基本操作