git 分支操作及日志

文章目录

        • 分支操作
          • 根据git的hash值创建分支
          • checkout 操作
        • 日志查看
        • 创建新分支
        • 删除分支
        • 提交差别
        • 合并分支
        • git reset
        • git stash
        • 在线演示网站

分支操作
  [tom@tom-virtual-machine test]$git branch -av       #显示本地和远程分支及其详细信息(sha1和commit、与上游分支的关系)
  master                7659d66 change
* ton-A                 ee47f4c three
  remotes/origin/HEAD   -> origin/master
  remotes/origin/master 7659d66 change
  remotes/origin/tom-A  d31b4a0 test_from_a
根据git的hash值创建分支
  1. 查看当前最近一次 commit 哈希值
    git log
  2. 使用上一部拿到的哈希值新建一个分支
    git branch <分支名> 哈希值 #基于master的分支,提交hash值创建新的分支 (哈希值只在输入4位以上就可以执行,如下500c
[root@tom-virtual-machine test]#git reflog 
500c79f HEAD@{0}: commit (amend): first
...
[root@tom-virtual-machine test]#git branch test-tom 500c
[root@tom-virtual-machine test]#git branch -a
  master
  test-tom
* ton-A
[root@localhost test]# git remote -v					#列出已经存在的远程分支
clancy  [email protected]:tomton/test.git (fetch)
clancy  [email protected]:tomton/test.git (push)
origin  [email protected]:tomton/test.git (fetch)
origin  [email protected]:tomton/test.git (push)

移除远程分支

[root@tom-virtual-machine test]#git remote -v
clancy  [email protected]:tomton/test.git (fetch)
clancy  [email protected]:tomton/test.git (push)
origin  [email protected]:tomton/test.git (fetch)
origin  [email protected]:tomton/test.git (push)
[root@tom-virtual-machine test]#git remote rm origin
[root@tom-virtual-machine test]#git remote -v
clancy  [email protected]:tomton/test.git (fetch)
clancy  [email protected]:tomton/test.git (push)
[root@tom-virtual-machine test]#
[root@localhost test]# git checkout -         			#快速切换分支
Switched to branch 'tom-A'
Your branch is up to date with 'clancy/tom-A'
checkout 操作
 git checkout --orphan <branch>      #基于当前所在分支新建一个新的孤立分支,没有任何的提交历史,但是当前分支的内容一一俱
 #全。新建的分支,严格意义上说,还不是一个分支,因为HEAD指向的引用中没有commit值,只有在进行一次提交后,它才算得上真正的分支。
 git checkout --merge <branch>		#将当前分支修改的内容一同打包带走
 git checkout -p <branch>			#打补丁(比较分支间差异,及单个文件)
 git checkout -B <branch>			#强制创建新分支
 git checkout --datch <branch>		#切换至分支的游离状态
 git checkout .						#本地所有修改的没有的都返回到原来的状态

参考https://blog.csdn.net/csflvcxx/article/details/81612336

日志查看
[tom@tom-virtual-machine test]$git reflog    #查看日志
7659d66 HEAD@{0}: clone: from [email protected]:tomton/test.git
[tom@tom-virtual-machine test]$git log --graph 
* commit ee47f4c0afe1d7667a7ba570dd5ab4d05d5b20e1
| Author: ton ubuntu <[email protected]>
| Date:   Fri Apr 23 17:43:39 2021 +0800
| 
|     three
|  
...
创建新分支
[tom@tom-virtual-machine test]$git checkout -b ton-A
Switched to a new branch 'ton-A'
删除分支
[tom@tom-virtual-machine test]$git branch -d ton-A 
Deleted branch ton-A (was 7659d66).
提交差别
  1. 修改文件时diffdiff HEAD的区别,diff HEAD提供最新提交差别
[tom@tom-virtual-machine test]$echo "hell" >> test1
[tom@tom-virtual-machine test]$git diff
diff --git a/test1 b/test1
index ce01362..0b877ac 100644
--- a/test1
+++ b/test1
@@ -1 +1,2 @@
 hello
+hell
[tom@tom-virtual-machine test]$git diff HEAD
diff --git a/test1 b/test1
index ce01362..0b877ac 100644
--- a/test1
+++ b/test1
@@ -1 +1,2 @@
 hello
+hell
[tom@tom-virtual-machine test]$git add test1 
[tom@tom-virtual-machine test]$git diff
[tom@tom-virtual-machine test]$git diff HEAD 
diff --git a/test1 b/test1
index ce01362..0b877ac 100644
--- a/test1
+++ b/test1
@@ -1 +1,2 @@
 hello
+hell
[tom@tom-virtual-machine test]$git commit -m "second"
[ton-A dacd71b] second
 1 file changed, 1 insertion(+)
[tom@tom-virtual-machine test]$git diff
[tom@tom-virtual-machine test]$git diff HEAD         
[tom@tom-virtual-machine test]$
  1. 创建文件时diffdiff HEAD的区别
[tom@tom-virtual-machine test]$touch tom.txt
[tom@tom-virtual-machine test]$git diff
[tom@tom-virtual-machine test]$echo "hello" >> tom.txt 
[tom@tom-virtual-machine test]$git diff
[tom@tom-virtual-machine test]$git diff HEAD
[tom@tom-virtual-machine test]$git add tom.txt 
[tom@tom-virtual-machine test]$git diff
[tom@tom-virtual-machine test]$git diff HEAD
diff --git a/tom.txt b/tom.txt
new file mode 100644
index 0000000..ce01362
--- /dev/null
+++ b/tom.txt
@@ -0,0 +1 @@
+hello
[tom@tom-virtual-machine test]$git commit -m "three"
[ton-A ee47f4c] three
 1 file changed, 1 insertion(+)
 create mode 100644 tom.txt
[tom@tom-virtual-machine test]$git diff
[tom@tom-virtual-machine test]$git diff HEAD
合并分支
[root@tom-virtual-machine test]#git branch 
  master
* ton-A
[root@tom-virtual-machine test]#git checkout master 
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
[root@tom-virtual-machine test]#git branch 
* master
  ton-A
[root@tom-virtual-machine test]#git merge --no-ff ton-A 

git 分支操作及日志_第1张图片
得到下面的结果

Merge made by the 'recursive' strategy.
 hello.php | 2 ++
 test1     | 2 ++
 tom.txt   | 1 +
 3 files changed, 5 insertions(+)
 create mode 100644 test1
 create mode 100644 tom.txt

这样ton-A分支的内容就合并到master分支中了。

git reset
 git reset --hard origin/master   		#放弃本地改动
 git reset --hard HASH					#不保留修改
 git reset --soft HASH					#保留修改,返回到某个节点   
git stash
所以 git stash 显得格外亲切。它帮你把手头未完成还不好提交(提交必然牵扯 commit-hook,又是运行单元测试又是静态检查的)的活收拢到一个暂存区,等新任务完成了可以再 git stash pop 恢复之前的工作。它的产品机理,像极了 CPU 的 exception,所以说程序员来来回回就那么几出戏,只不过在不同的场景下粉饰一下改头换面上演而已。
在线演示网站

https://try.github.io/

你可能感兴趣的:(Linux)