Git 一些简单命令
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git 一些简单命令相关的知识,希望对你有一定的参考价值。
Git的一些常用命令:
1、init,创建一个git仓库
[[email protected] git]# cd /usr/local/ [[email protected] local]# mkdir github [[email protected] local]# cd github [[email protected] git]# git init Initialized empty Git repository in /usr/local/github/.git/ [[email protected] git]# ls -a . .. .git
2、status,检查状态
[[email protected] git]# touch code1.py [[email protected] git]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # code1.py nothing added to commit but untracked files present (use "git add" to track)
3、add,文件添加到索引,也就是接受版本控制
[[email protected] git]# git add code1.py [[email protected] git]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: code1.py #
4、commit,提交
[[email protected] git]# git commit -m "New add code1.py" #第一次会提示我们输入信息,名字与邮件 [master 7894d32] New add code1.py Committer: root <[email protected](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 [email protected] If the identity used for this commit is wrong, you can fix it with: git commit --amend --author=‘Your Name <[email protected]>‘ 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 code1.py [[email protected] git]# git config --global user.name sunshine #名字 [[email protected] git]# git config --global user.email [email protected] #邮件地址 [[email protected] git]# git config --global color.ui #auto显示颜色
5、branc 创建、查看、删除分支
[[email protected] git]# git branch * master [[email protected] git]# git branch dev [[email protected] git]# git branch dev * master [[email protected] git]# git branch -d dev Deleted branch dev (was 7894d32). [[email protected] git]# git branch * master #删除分支会有另外一种诡异的现象发生 [[email protected] git]# git checkout -b dev1 #创建并切换dev1分支 Switched to a new branch ‘dev1‘ [[email protected] git]# cat code.py [[email protected] git]# echo "print ‘Sunshine Good~‘" > code.py #输出内容至code.py [[email protected] git]# cat code.py print ‘Sunshine Good~‘ [[email protected] git]# git add code.py [[email protected] git]# git commit -m "New add rom ‘Sunshine Good~‘" [dev1 865c84e] New add rom ‘Sunshine Good~‘ 1 files changed, 1 insertions(+), 0 deletions(-) [[email protected] git]# git checkout master #切换至master Switched to branch ‘master‘ [[email protected] git]# cat code.py #查看mastercode.py,没有内容 [[email protected] git]# git branch -d dev1 #删除分支dev1报错,俩中方法,第一dev1确实不是我需要的,那么小d换成大D即可,第二个就下面步骤 error: The branch ‘dev1‘ is not fully merged. If you are sure you want to delete it, run ‘git branch -D dev1‘. [[email protected] git]# git merge dev1 #合并分支dev1至master Updating bf266f5..865c84e Fast-forward code.py | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) [[email protected] git]# cat code.py print ‘Sunshine Good~‘ [[email protected] git]# git branch -d dev1 #使用小d删除分支dev1,删除成功 Deleted branch dev1 (was 865c84e). [[email protected] git]#
6、checkout 切换 创建分支
[[email protected] git]# git checkout -b dev Switched to a new branch ‘dev‘ [[email protected] git]# git branch * dev master [[email protected] git]# git checkout master Switched to branch ‘master‘ [[email protected] git]# git branch dev * master
7、clone 克隆一个repository 信息在本地目录,哪自己之前做好的gitolite
[[email protected]_master ~]# git clone [email protected]:dev Initialized empty Git repository in /root/dev/.git/ remote: Counting objects: 9, done. remote: Compressing objects: 100% (3/3), done. remote: Total 9 (delta 0), reused 0 (delta 0) Receiving objects: 100% (9/9), done. [[email protected]_master ~]# ls anaconda-ks.cfg appendonly.aof dev id_rsa.pub install.log install.log.syslog [[email protected]_master ~]# cd dev/ [[email protected]_master dev]# ls test.txt
8、diff 对比文件
[[email protected] git]# ls code1.py code.py [[email protected] git]# git status # On branch master nothing to commit (working directory clean) [[email protected] git]# vim code1.py [[email protected] git]# git diff #这里是对比暂存区与工作区对比 diff --git a/code1.py b/code1.py index e69de29..66708be 100644 --- a/code1.py +++ b/code1.py @@ -0,0 +1 @@ +print ‘Sunshine Good!‘ [[email protected] git]# git diff --staged #这里是暂存区对比staged也就是我们commit的staged区 [[email protected] git]# git add code1.py #添加到暂存区 [[email protected] git]# git diff #工作区对比暂存区 [[email protected] git]# git diff --staged #暂存区对比commit的staged diff --git a/code1.py b/code1.py index e69de29..66708be 100644 --- a/code1.py +++ b/code1.py @@ -0,0 +1 @@ +print ‘Sunshine Good!‘ [[email protected] git]# git commit -m "code1.py New add row Sunshine Good" #提交 [master bf266f5] code1.py New add row Sunshine Good 1 files changed, 1 insertions(+), 0 deletions(-) [[email protected] git]# git diff --staged #对比暂存区以staged区 [[email protected] git]# git diff #对比工作区对比暂存区
9、log 就是日志咯,不过这里显示的可能比较诡异
[[email protected] git]# git log #详细显示 commit bf266f5673089439efdd632a38b7220390af5cc7 Author: sunshine <[email protected]> Date: Wed Oct 5 23:34:03 2016 +0800 code1.py New add row Sunshine Good commit 7894d320ac92997fdb4d0c74487d87def3ebf756 Author: root <[email protected](none)> Date: Wed Oct 5 23:20:29 2016 +0800 New add code1.py commit b4213472064fbc292eff843b6a67549344197495 Author: root <[email protected](none)> Date: Wed Oct 5 21:45:23 2016 +0800 New add code.py [[email protected] git]# git log --oneline #简要显示 bf266f5 code1.py New add row Sunshine Good 7894d32 New add code1.py b421347 New add code.py [[email protected] git]# git log --color --graph #显示版本分支示意图 * commit bf266f5673089439efdd632a38b7220390af5cc7 | Author: sunshine <[email protected]> | Date: Wed Oct 5 23:34:03 2016 +0800 | | code1.py New add row Sunshine Good | * commit 7894d320ac92997fdb4d0c74487d87def3ebf756 | Author: root <[email protected](none)> | Date: Wed Oct 5 23:20:29 2016 +0800 | | New add code1.py | * commit b4213472064fbc292eff843b6a67549344197495 Author: root <[email protected](none)> Date: Wed Oct 5 21:45:23 2016 +0800 New add code.py
10、merge 合并分支
[[email protected] git]# ls code1.py code.py [[email protected] git]# git checkout -b dev #创建并切换至分支dev Switched to a new branch ‘dev‘ [[email protected] git]# git branch #查看当前位置所在分支 * dev master [[email protected] git]# vim code.py [[email protected] git]# cat code.py print ‘Sunsine !‘ [[email protected] git]# git checkout master #切换回master M code.py Switched to branch ‘master‘ [[email protected] git]# git merge dev #合并分支dev Already up-to-date. [[email protected] git]# cat code.py print ‘Sunsine !‘ [[email protected] git]# git branch -d dev #删除分支 Deleted branch dev (was bf266f5)
11、mv 重命名
[[email protected] git]# ls code1.py code.py [[email protected] git]# git mv code1.py code.txt #重命名code1.py-->code.txt [[email protected] git]# ls code.py code.txt [[email protected] git]# git status #查看状态,显示 # renamed: code1.py -> code.txt # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: code1.py -> code.txt # [[email protected] git]# git add code.txt [[email protected] git]# git commit -m " Modify file name code1.py-->code.txt" [master 924041d] Modify file name code1.py-->code.txt 1 files changed, 0 insertions(+), 0 deletions(-) rename code1.py => code.txt (100%)
12、rm 删除
[[email protected] git]# ll total 8 -rw-r--r-- 1 root root 23 Oct 5 23:53 code.py -rw-r--r-- 1 root root 23 Oct 5 23:32 code.txt [[email protected] git]# git rm code.txt #删除code.txt rm ‘code.txt‘ [[email protected] git]# ll total 4 -rw-r--r-- 1 root root 23 Oct 5 23:53 code.py [[email protected] git]# git commit -am " Delete file code.txt" [master 82ec99e] Delete file code.txt 1 files changed, 0 insertions(+), 1 deletions(-) delete mode 100644 code.txt
13、show等价于rev-parse,只不过一个是简要一个是明细
[[email protected] git]# git show HEAD~ #看hash值 commit 924041d5e2924e945f67816775869d4ceb9694dd Author: sunshine <[email protected]> Date: Wed Oct 5 23:57:25 2016 +0800 Modify file name code1.py-->code.txt diff --git a/code.txt b/code.txt new file mode 100644 index 0000000..66708be --- /dev/null +++ b/code.txt @@ -0,0 +1 @@ +print ‘Sunshine Good!‘ diff --git a/code1.py b/code1.py deleted file mode 100644 index 66708be..0000000 --- a/code1.py +++ /dev/null @@ -1 +0,0 @@ -print ‘Sunshine Good!‘ [[email protected] git]# git rev-parse HEAD~ #仔细看hash值是一样的 924041d5e2924e945f67816775869d4ceb9694dd
14、cat-file 查看tree与parent
[[email protected] git]# git cat-file -t HEAD~ #-t 显示commit commit [[email protected] git]# git cat-file -p HEAD~ #-p 显示tree与parent tree ea16f4c3781c4dc1f5e142eba114b0d5a1cefeaa parent 865c84e5f13fb239f83e3e27bcf0f3a28990034e author sunshine <[email protected]> 1475683045 +0800 committer sunshine <[email protected]> 1475683045 +0800 Modify file name code1.py-->code.txt
15、reset 这个就比较复杂啦
[[email protected] git]# ll total 4 -rw-r--r-- 1 root root 23 Oct 6 00:17 code.py [[email protected] git]# git log --oneline #查看历史commit版本hash值82ec99e 82ec99e Delete file code.txt 924041d Modify file name code1.py-->code.txt 865c84e New add rom ‘Sunshine Good~‘ bf266f5 code1.py New add row Sunshine Good 7894d32 New add code1.py b421347 New add code.py [[email protected] git]# ll total 4 -rw-r--r-- 1 root root 23 Oct 6 00:17 code.py [[email protected] git]# git rm code.py #删除文件code.py rm ‘code.py‘ [[email protected] git]# git commit -am "Delete file code.py" #commit -am等价于执行 #git add filename # git commit -m "Delete fie code.py",提示删除成功 [master 3271a1d] Delete file code.py 1 files changed, 0 insertions(+), 1 deletions(-) delete mode 100644 code.py [[email protected] git]# ll #查看不存在 total 0 [[email protected] git]# git status #也没报错 # On branch master nothing to commit (working directory clean) [[email protected] git]# git log --oneline #查看commit版本hash值 3271a1d Delete file code.py 82ec99e Delete file code.txt 924041d Modify file name code1.py-->code.txt 865c84e New add rom ‘Sunshine Good~‘ bf266f5 code1.py New add row Sunshine Good 7894d32 New add code1.py b421347 New add code.py [[email protected] git]# git reset --hard 82ec99e #回滚到上个版本 HEAD is now at 82ec99e Delete file code.txt [[email protected] git]# ll #OK文件回来了 total 4 -rw-r--r-- 1 root root 23 Oct 6 00:18 code.py [[email protected] git]# git log --oneline #回滚成功 82ec99e Delete file code.txt 924041d Modify file name code1.py-->code.txt 865c84e New add rom ‘Sunshine Good~‘ bf266f5 code1.py New add row Sunshine Good 7894d32 New add code1.py b421347 New add code.py
16、tag 标签,由于commit都是以hash记录,所以tag是为快速定义而准备的
[[email protected] git]# git log --oneline #查看commit版本的hash 2989e89 New add rom Sunshine bf266f5 code1.py New add row Sunshine Good 7894d32 New add code1.py b421347 New add code.py [[email protected] git]# git tag v0.1.2 2989e89 #为hash值2989e89打上标签v0.1.2 [[email protected] git]# git tag v0.1.3 bf266f5 #为hash值bf266f5打上标签v0.1.3 [[email protected] git]# git tag v0.1.1 v0.1.2 v0.1.3 [[email protected] git]# git tag v0.1.4 #修改代码或者增加功能都可以直接给上tag [[email protected] git]# git show 2989e89 #使用hash查找 commit 2989e8927da83be9087933c17b074adeb2e91e2c Author: sunshine <[email protected]> Date: Wed Oct 5 23:49:07 2016 +0800 New add rom Sunshine diff --git a/code.py b/code.py index e69de29..3a2a051 100644 --- a/code.py +++ b/code.py @@ -0,0 +1 @@ +print ‘Sunsine !‘ [[email protected] git]# git show v0.1.2 #使用标签查找 commit 2989e8927da83be9087933c17b074adeb2e91e2c Author: sunshine <[email protected]> Date: Wed Oct 5 23:49:07 2016 +0800 New add rom Sunshine diff --git a/code.py b/code.py index e69de29..3a2a051 100644 --- a/code.py +++ b/code.py @@ -0,0 +1 @@ +print ‘Sunsine !‘ [[email protected] git]# git tag -d v0.1.1 #删除标签 Deleted tag ‘v0.1.1‘ (was 2989e89) [[email protected] git]# git tag v0.1.2 v0.1.3
17、pull 获取,这个用到我们之前搭建的gitolite
[[email protected]_master dev]# git pull origin master #没发现有人push,所以拉不到信息 From 127.0.0.1:dev * branch master -> FETCH_HEAD Already up-to-date. [[email protected]_master dev]# git push -f #为了测试这边使用-f强制覆盖更新 Counting objects: 32, done. Delta compression using up to 4 threads. Compressing objects: 100% (19/19), done. Writing objects: 100% (32/32), 2.94 KiB, done. Total 32 (delta 0), reused 0 (delta 0) To [email protected]:dev + 4197bf0...addfd8f master -> master (forced update) [[email protected]_master dev]# git pull #再试一下git pull remote: Counting objects: 24, done. remote: Compressing objects: 100% (16/16), done. remote: Total 23 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (23/23), done. From 127.0.0.1:dev + 4197bf0...addfd8f master -> origin/master (forced update) Merge made by recursive. 855.py | 4 ++++ class.php | 1 + 2 files changed, 5 insertions(+), 0 deletions(-) create mode 100644 855.py create mode 100644 class.php [[email protected]_master dev]# ll #拉取到代码了 total 8 -rw-r--r--. 1 root root 45 Oct 6 01:03 855.py -rw-r--r--. 1 root root 4 Oct 6 01:03 class.php
18、push 推送,这个用到我们之前搭建的gitolite
[[email protected]_master dev]# git status #查看状态 # On branch master nothing to commit (working directory clean) [[email protected]_master dev]# cat test.txt this is test file 555 [[email protected]_master dev]# echo 44 >> test.txt #修改内容 [[email protected]_master dev]# git commit -am "New add string 44" #commit并说明 [master 4197bf0] New add string 44 1 files changed, 1 insertions(+), 0 deletions(-) [r[email protected]_master dev]# git push #推送至我们的远程代码托管 Counting objects: 5, done. Writing objects: 100% (3/3), 262 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:dev 8a54bbe..4197bf0 master -> master
TortoiseGit的GUI界面有时间也会写一篇,其实这里只是简简单单的介绍了下命令的正常场景所遇见的问题!~更深层次的下次介绍呗^-^,下章估计是介绍Gitlab~~~
本文出自 “SunshineBoySZF” 博客,请务必保留此出处http://sunshineboyszf.blog.51cto.com/12087328/1858891
以上是关于Git 一些简单命令的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段15——git命令操作一个完整流程
GitGit 分支管理 ( 克隆远程分支 | 克隆 master 分支 git clone | 查看远程分支 git branch -a | 克隆远程分支 git checkout -b )(代码片段
GitGit 分支管理 ( 克隆远程分支 | 克隆 master 分支 git clone | 查看远程分支 git branch -a | 克隆远程分支 git checkout -b )(代码片段