分支操作
[tom@tom-virtual-machine test]$git branch -av #显示本地和远程分支及其详细信息(sha1和commit、与上游分支的关系)
master 7659d66 change
* ton-A ee47f4c three # 当前所在分支
remotes/origin/HEAD -> origin/master #指向origin/master
remotes/origin/master 7659d66 change
remotes/origin/tom-A d31b4a0 test_from_a #origin仓库的tom-A 指向d3***这个提交
[tom@tom-virtual-machine test]$git branch -vv #git查看本地分支关联(跟踪)的远程分支之间的对应关系,本地分支对应哪个远程分支
* br_local 2498bc75 [origin/master: ahead 1] DTS:DTSxxx Description:xxxxxx # [origin/master: ahead 1]:表示当前分支与远程仓库的 master 分支相比,本地多了一个提交。
根据git的hash值创建分支
- 查看当前最近一次 commit 哈希值
git log
- 使用上一部拿到的哈希值新建一个分支
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 git@github.com:tomton/test.git (fetch)
clancy git@github.com:tomton/test.git (push)
origin git@github.com:tomton/test.git (fetch)
origin git@github.com:tomton/test.git (push)
移除远程分支
[root@tom-virtual-machine test]#git remote -v
clancy git@github.com:tomton/test.git (fetch)
clancy git@github.com:tomton/test.git (push)
origin git@github.com:tomton/test.git (fetch)
origin git@github.com:tomton/test.git (push)
[root@tom-virtual-machine test]#git remote rm origin
[root@tom-virtual-machine test]#git remote -v
clancy git@github.com:tomton/test.git (fetch)
clancy git@github.com: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 git@github.com:tomton/test.git
[tom@tom-virtual-machine test]$git log --graph
* commit ee47f4c0afe1d7667a7ba570dd5ab4d05d5b20e1
| Author: ton ubuntu <ton_tom@163.com>
| 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).
提交差别
- 修改文件时
diff
与diff 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]$
- 创建文件时
diff
与diff 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
得到下面的结果
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,所以说程序员来来回回就那么几出戏,只不过在不同的场景下粉饰一下改头换面上演而已。