Try   HackMD

[Version Control] Git使用紀錄

tags: Version Control Git

:+1: 持續更新中!有用到就會更新上來。

Log

只想查版號跟對應的commit內容

git log --oneline

分支

:warning: checkout到某個分支後,記得先pull一下。

:book: 回到前一個工作分支,git checkout -

Local沒有但要直接同步Remote的某個分支

git clone <repo-name> // 這個會建立一個新的local分支,且已經同步remote git checkout <brach-name>

檢視

git branch # local branch git branch -r # remote branch git branch -a # both local and remote branch

創建(local)

git clone <repo-name> # 在local創建分支,同時切換到新分支 git checkout -b <branch-name> # checkout 可以縮寫成`co`

:book: 如果想要創建(local)分支feature/kickuser,Local端已經有feature這個名稱的分支的話,git會丟fatal error。

Example

如果想要創建的分支長這樣

feature/kickuser

輸入:

git co -b feature/kickuser

刪除(local)

git branch -d <branch-name> // 強制刪除 git branch -D <branch-name>

刪除(remote)

TODO!

更新

假如要更新remote branch (feature/kickuser)的到local

git pull origin feature/kickuser

[Github] Branching and Pull Request流程

git clone <repo> git checkout -b <branch-name> git add <files> git commit -m "add files" git push -u origin <branch-name>

:warning: push到remote branch的時候,remote branch的名稱要跟local branch的名稱一樣!

成功推上去後,到剛推的分支下面,會有Compare & pull request的按鈕,這樣就可以pull request給原作者code review了。

:smile: 如果pull request的內容被comments,修正過後,在conversation的欄位附上更新版的hash code,再從新提交一次code review即可。

突然要緊急處理插件

git stash git pull -r <remote-branch> # <remote-branch>例如: origin develop git stash pop

gitignore for macOS

# Generated by macOS .DS_Store .DS_Store? ._* .Spotlight-V100 .Trashes Icon? ehthumbs.db Thumbs.db

刪除

已經簽上repo的檔案

git rm --cached <file> git commit -m "Deleted file from repository only" git push

指定資料夾與資料夾底下的檔案

git rm -r <folder> git commit -m "Deleted the folder from the repository" git push

刪除已簽上repo的資料夾底下的全部檔案

git rm -r --cached FolderName git commit -m "Removed folder from repository" git push origin master

暫存

改到一半要切到別的分支或pull其他線上分支的時候,可以把剛剛改的變更暫存起來。

git stash

提取剛剛的暫存,回到剛剛工作的狀態

git stash pop

清除暫存

git stash drop // 把所有暫存清除 git stash clear

Merge & Conflict

情境題:Pull到一半有conflict了,想退回先不合併

git merge --abort

情境題:If you're already in conflicted state

// 如果要用自己改的,把`theirs`改成`ours` git checkout --theirs path/to/file

Reset

回到前一版

git reset <commit-hash>^ // Or git reset HEAD^

往回N個版本

git reset <commit-hash>~(N) // Or git reset HEAD~(N)

`Revert

Reference

If files are not staged

git checkout -- .

Revert specificed file

git checkout HEAD -- path/to/some/file.txt

Fast Forward

TODO!

Reference