changed 5 years ago
Linked with GitHub

7.4 0515 Git 小劇場 Part 3

Case 1 git add .完之後準備要commit,卻發現忘記開分支

解法

  1. 在此分支加上新貼紙fishgit branch fish,並且趕快git checkout fish過去。
  2. 再進行一次git add .git commit
  3. fish分支上,刪掉master branch(撕掉貼紙)
tingdeAir:git-rebase tingtinghsu$ touch fish1.html
tingdeAir:git-rebase tingtinghsu$ git add .

tingdeAir:git-rebase tingtinghsu$ git branch fish
tingdeAir:git-rebase tingtinghsu$ git checkout fish
tingdeAir:git-rebase tingtinghsu$ git add .
tingdeAir:git-rebase tingtinghsu$ git commit -m "add fish1"
[fish 7ae141a] add fish1
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 fish1.html
 
tingdeAir:git-rebase tingtinghsu$ git branch -d master

  1. 回到前一狀態(尚未增加新檔案的狀態),再度加上master branch(再貼上master貼紙)

Case 2 比較機密的檔案不想放在Git裡

解法:

ignore https://github.com/github/gitignore

1.新增.gitignore

MacBook-Air:git-rebase tingtinghsu$ touch .gitignore

2.編輯.gitignore,加入黑名單

pig*
bird*
  1. 加入黑名單的檔案不再進入版控中
MacBook-Air:git-rebase tingtinghsu$ touch pig.html
MacBook-Air:git-rebase tingtinghsu$ touch bird.html
MacBook-Air:git-rebase tingtinghsu$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.gitignore

限制:
之前已經產生的檔案,就算加入了.gitignore,還是會被git記錄到。必須先刪除再重新產生一份同名檔案。

Case 3 手邊的事情做一半,被老闆叫去修別的bug: git stash

可能的解法: 切換到master分支,做完再切回來

git commit
git checkout master
...(做別的步驟)
git checkout cat
git reset HEAD^ --mix

更好的解法:

  1. git stash 使用git 中斷指令
    (也是一種commit,只是不會出現在歷史紀錄上)
tingdeAir:git-stash tingtinghsu$ git stash

Saved working directory and index state WIP on cat: e12d8ef add database.yml in config folder

HEAD is now at e12d8ef add database.yml in config folder

  1. git stash list 查詢中斷的項目
tingdeAir:git-stash tingtinghsu$ git stash list

stash@{0}: WIP on cat: e12d8ef add database.yml in config folder
  1. 切換到master分支修改完bug,再切回自己手邊待做的branch
tingdeAir:git-stash tingtinghsu$ git checkout master
Switched to branch 'master'

...(做別的步驟)

tingdeAir:git-stash tingtinghsu$ git checkout cat
Switched to branch 'cat'
  1. git stash pop
tingdeAir:git-stash tingtinghsu$ git stash pop
On branch cat
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:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (3e7ae10f684ba63b95b38ff8697cbd06f8c7179b)

重新回到之前的進度。

重點: git stash applygit stash pop兩者都可以使用,但pop指令會把原本的stash砍掉,apply保留stash

Switched to branch 'master'
tingdeAir:git-stash tingtinghsu$ git checkout cat
Switched to branch 'cat'
tingdeAir:git-stash tingtinghsu$ git stash apply
On branch cat
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:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
tingdeAir:git-stash tingtinghsu$ git stash list
stash@{0}: WIP on cat: e12d8ef add database.yml in config folder

Case 4. 線上專案已被其他人執行git push -f

只能找最接近歷史的一次記錄再git push -f蓋回去,然後修改中間改變過的檔案

Case 5. 已經push出去的進度該如何reset?

eg. 多人協作的情況下:想取消上次版本的其中一個commit

解法: 使用reverse commit

  1. 在source tree想要取消的那顆commit上按右鍵reverse commit
  2. reverse commit會做出新的commit,逆轉上次做的步驟(如果上次commit是新增檔案,這次commit就是刪除檔案)

eg. 如果是只有自己一個人的專案,直接reset,commit再git push -f就好

Select a repo