### 1. stash 保存現場
> 在某個分支上修改后,add之后,暫時不想提交。但是此時要切換分支是系統不允許的。
```
wangyijiadeMacBook-Air:August Notes bizzbee$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
Code.html
Please commit your changes or stash them before you switch branches.
Aborting
```
> 系統提示你只能提交或者stash保存現場之后才能進行切換分支。
```
wangyijiadeMacBook-Air:August Notes bizzbee$ git status
On branch feature1
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: Code.html
wangyijiadeMacBook-Air:August Notes bizzbee$ git stash
Saved working directory and index state WIP on feature1: 4cf57c2 mod code
wangyijiadeMacBook-Air:August Notes bizzbee$ git status
On branch feature1
nothing to commit, working tree clean
wangyijiadeMacBook-Air:August Notes bizzbee$
```
>執行git stash后整個工作區的修改被隱藏,git status 提示是干凈的。
>這時候也可以切換分支去干其他工作了。
>干完回來。工作區是干凈的,剛才的工作現場存到哪去了?用`git stash list`命令看看:
~~~
$ git stash list
stash@{0}: WIP on dev: f52c633 add merge
~~~
> 工作現場還在,Git把stash內容存在某個地方了,但是需要恢復一下,有兩個辦法:
1. 一是用`git stash apply`恢復,但是恢復后,stash內容并不刪除,你需要用`git stash drop`來刪除;
2. 另一種方式是用`git stash pop`,恢復的同時把stash內容也刪了:
```
wangyijiadeMacBook-Air:August Notes bizzbee$ git stash apply
On branch feature1
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: Code.html
no changes added to commit (use "git add" and/or "git commit -a")
wangyijiadeMacBook-Air:August Notes bizzbee$ git add .
wangyijiadeMacBook-Air:August Notes bizzbee$ git commit -m 'stash back'
[feature1 760d527] stash back
1 file changed, 1 insertion(+), 1 deletion(-)
wangyijiadeMacBook-Air:August Notes bizzbee$ git status
On branch feature1
nothing to commit, working tree clean
wangyijiadeMacBook-Air:August Notes bizzbee$
```
### 2. 丟去沒有被合并過的分支
如果要丟棄一個沒有被合并過的分支,可以通過`git branch -D <name>`強行刪除。
### 3.推送到遠程倉庫指定某個分支。
推送分支,就是把該分支上的所有本地提交推送到遠程庫。推送時,要指定本地分支,這樣,Git就會把該分支推送到遠程庫對應的遠程分支上:
~~~
$ git push origin master
~~~
如果要推送其他分支,比如`dev`,就改成:
~~~
$ git push origin dev
~~~
> 本地有的分支才能推送到遠程,而且是對應的分支。
查看當前對應的遠程庫
當你從遠程倉庫克隆時,實際上Git自動把本地的`master`分支和遠程的`master`分支對應起來了,并且,遠程倉庫的默認名稱是`origin`。
要查看遠程庫的信息,用`git remote`:
~~~
$ git remote
origin
~~~
或者,用`git remote -v`顯示更詳細的信息:
~~~
$ git remote -v
origin git@github.com:michaelliao/learngit.git (fetch)
origin git@github.com:michaelliao/learngit.git (push)
~~~
上面顯示了可以抓取和推送的`origin`的地址。如果沒有推送權限,就看不到push的地址。