顯示日誌信息
常用
// 查看日誌
git log
// 顯示詳細日誌
git reflog
// 查看git日誌一行展示;展示10條
git log --oneline - 10
//同理
git reflog --oneline -10
其他:
git log --online --decorate
顯示提交歷史、各個分支的指向以及項目的分支分叉
git log --oneline --decorate --graph --all
創建分支
基本步驟 創建分支–> 切換分支
創建分支 —> 創建新分支;但是不會直接切換到該分支
git branch branch1
#切換到branch1分支命令
git checkout branch1
以上兩步完成創建分支並切換到該分支; 可由下面一步替換
快速創建分支並切換到該分支下
創建分支並切換到該分支下面
git checkout -b branch1
GIT 分支查看
// 查看本地分支
git branch
// 查看本地分支的最後一次指向
git branch -v
// 查看本地分支所對應的遠程分支
git branch -vv
// 查看所有分支
git branch -a
//查看遠程分支列表
git branch -r
GIT分支合併
首先切換到需要合併的分支上;然後使用 git merge
需求 : 將 branch1分支合併到master上面
//切換到最終合併到的分支
git checkout master
//執行合併;被合併分支
git merge branch1
刪除GIT分支命令
//刪除指定 branch1 的分支;次分支合併之後下面刪除纔會成功;
git branch -d branch1
//如果需要強制刪除;使用下面操作
git branch -D branch1
遠程倉庫分支下載本地其他分支
使用遠程倉庫 origin 下面的branch1分支,創建本地分支branch2;不切換當前分支創建出來的本地分支 branch2沒有遠程倉庫指向
// git push <遠程主機名> <本地分支名>:<遠程分支名>
// 創建本地分支 branch2 ;將 遠程branch1 的代碼下載到branch2分支
git fetch origin branch1:branch2
推送本地分支到遠程指定分支倉庫
// 來將本地的 branch_local 分支推送到遠程倉庫上的 branch_remote 分支。
git push origin branch_local:branch_remote
檢出遠程倉庫origin的branch1分支並創建本地分支branch進行追蹤
// git checkout -b <branch> <remote>/<branch> 格式
// 檢出遠程的 branch1分支到本地的 branch分支
git checkout -b branch orign/branch1
// 上面操作的簡單寫法,如下, 下面意思追遠程倉庫branch1 並創建本地分支 branch1;並切換到該分支
git checkout --track orign/brach1
// 設置當前本地分支追蹤一個剛纔下載下來的遠程分支
參考地址 :https://stackoverflow.com/questions/520650/make-an-existing-git-branch-track-a-remote-branch
// 設置當前 分支的追蹤遠程的 origin/branch 分支
git branch -u origin/branch
Given a branch foo and a remote upstream:
As of Git 1.8.0:
git branch -u upstream/foo
Or, if local branch foo is not the current branch:
git branch -u upstream/foo foo
Or, if you like to type longer commands, these are equivalent to the above two:
git branch --set-upstream-to=upstream/foo
or
git branch --set-upstream-to=upstream/foo foo
As of Git 1.7.0:
git branch --set-upstream foo upstream/foo
Notes:
All of the above commands will cause local branch foo to track remote branch foo from remote upstream.
The old (1.7.x) syntax is deprecated in favor of the new (1.8+) syntax. The new syntax is intended to be more intuitive and easier to remember.
Defining an upstream branch will fail when run against newly-created remotes that have not already been fetched. In that case, run git fetch upstream beforehand.
提交的文件 commit到本地倉庫的 ;比對上一次commit的差別 HEAD^ -->commit 上一次的提交; HEAD —> 本次提交指向
git diff HEAD^ HEAD
臨時暫存分支數據不提交
參考文章 https://blog.csdn.net/stone_yw/article/details/80795669
官方地址 :https://git-scm.com/docs/git-stash
場景 :
git在編寫代碼的時候,代碼寫了一半;線上有bug需要修復;但是功能沒有完成;所以不想commit的解決方案;下面的命令將會把本次的修改暫時倉儲到某個其他的地方去
git stash
顯示所有被暫時倉儲的數據
git stash list
恢復暫時被倉儲的數據
方式一 :一是用git stash apply恢復,但是恢復後,stash內容並不刪除,你需要用git stash drop來刪除;
// 恢復數據
git stash apply
// 刪除stash的數據
git stash drop
方式二: 恢復的同時把stash內容也刪了;等同於上面的操作
git stash pop
=====================================================
你可以多次stash,恢復的時候,先用git stash list查看,然後恢復指定的stash,用命令:
//下面的 0 就是你需要恢復的制定stash的值
git stash apply stash@{0}
//可以保存你之前的分支歷史。能夠更好的查看 merge歷史,以及branch 狀態。
git merge –no-ff
//則不會顯示 feature,只保留單條分支記錄。
git merge
案例說明;比如:我當前分支是master, 修復bug的分支是issue-001
//檢出到master分支
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits.
//合併issue-001分支
$ git merge --no-ff -m "merged bug fix 001" issue-001
Merge made by the 'recursive' strategy.
readme.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
//刪除該分支
$ git branch -d issue-001
Deleted branch issue-101 (was cc17032).
git merge 和 git merge --no-ff 的區別圖解
參考文章地址 : https://www.cnblogs.com/phpper/p/8034480.html
下圖是分析圖圖示例
合併多次commit 提交
參考地址 : https://blog.csdn.net/w57685321/article/details/86597808
請勿對已經推送到遠程的歷史版本進行此操作
git rebase -i [startpoint] [endpoint]
上面含義 :其中-i的意思是–interactive,即彈出交互式的界面讓用戶編輯完成合並操作,[startpoint] [endpoint]則指定了一個編輯區間,如果不指定[endpoint],則該區間的終點默認是當前分支HEAD所指向的commit(注:該區間指定的是一個前開後閉的區間,也就是 -i 後面的 startpoint 的這次提交不會被合併)
實例演示
需求
需要 合併 從 第二次提交到最後一次提交
實現步驟
//因爲前面所說的前開後閉原則; 我們需要合併的是從第二次; 所以此處 -i 後面的commit id是第一次提交的id
git rebase -i 65fab01
字段含義
pick:保留該commit(縮寫:p)
reword:保留該commit,但我需要修改該commit的註釋(縮寫:r)
edit:保留該commit, 但我要停下來修改該提交(不僅僅修改註釋)(縮寫:e)
squash:將該commit和前一個commit合併(縮寫:s)
fixup:將該commit和前一個commit合併,但我不要保留該提交的註釋信息(縮寫:f)
exec:執行shell命令(縮寫:x)
drop:我要丟棄該commit(縮寫:d)
按照上面解釋的含義修改前綴執行合併; 修改部分 pick 爲 s;之後執行 wq保存
wq保存之後頁面跳轉至第二個彈框;下面是 commit提示;可以進行自主修改
修改commit的信息爲 下圖所示; 然後 wq保存退出
保存退出之後顯示結果 :如下;說明成功
在執行 日誌命令;如下圖所示;至此合併結束
請勿對已提交到遠程的代碼進行此操作!!!