歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Git分支利器簡介

Git分支利器簡介

日期:2017/3/3 16:12:14   编辑:關於Linux

一、基本動作

Git 中的分支實際上僅是一個包含所指對象校驗和(40 個字符長度SHA-1 字串)

的文件,所以創建和銷毀一個分支就變得非常廉價。

Git 是如何知道你當前在哪個分支上工作的呢?其實答案也很簡單,它保存著一個

名為HEAD 的特別指針。

創建:git branch iss53

刪除:git branch -d iss53

切換:git checkout iss53

創建並切換:git checkout -b iss53

查看哪些分支已被並入當前分支:git branch --merge 或者相反:git branch --no-merged

二、切換分支必須遵循的狀態

只有在以下狀態下才可以切換分支:

1、當前分支中的所有文件都已經提交到本地版本庫。

2、把當前的還未提交的文件進行儲藏,保證工作目錄是干淨的之後,才可以切換分支。

儲藏的主要命令是:

git status
# On branch test
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   b.txt
#   modified:   mx
#
no changes added to commit (use "git add" and/or "git commit -a")

再執行git stash之後

git stash
Saved working directory and index state WIP on test: 32211b0 add b.txt
HEAD is now at 32211b0 add b.txt
git status
# On branch test
nothing to commit (working directory clean)

要查看現有的儲藏,你可以使用git stash list:

git stash list
stash@{0}: WIP on test: 32211b0 add b.txt

你可以重新應用你剛剛實施的儲藏,所采用的命令就是之前在原始的stash 命令的幫助輸出裡提示的:git stash apply。如果你想應用更早的儲藏,你可以通過名字指定它,像這樣:gitstash apply stash@2。如果你不指明,Git 默認使用最近的儲藏並嘗試應用它。

git stash apply
# On branch test
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   b.txt
#   modified:   mx
#
no changes added to commit (use "git add" and/or "git commit -a")
git stash list
stash@{0}: WIP on test: 32211b0 add b.txt

可以看出:

apply 選項只嘗試應用儲藏的工作——儲藏的內容仍然在棧上。要移除它,你可以運行

git stash drop,加上你希望移除的儲藏的名字:

git stash drop stash@{0}

Dropped stash@{0} (2bb9fe1993cf55843152025ae2bd79d5f7d8974c)

你也可以運行git stash pop 來重新應用儲藏,同時立刻將其從堆棧中移走。

作者:51cto博客 phper-每天一點點

 

Copyright © Linux教程網 All Rights Reserved