# [Version Control] Git使用紀錄 ###### tags: `Version Control` `Git` :::warning :+1: 持續更新中!有用到就會更新上來。 ::: ## Log 只想查版號跟對應的commit內容 ```bash= git log --oneline ``` ## 分支 :::warning :warning: `checkout`到某個分支後,記得先`pull`一下。 ::: :::info :book: 回到前一個工作分支,`git checkout -`。 ::: ## Local沒有但要直接同步Remote的某個分支 ```bash= git clone <repo-name> // 這個會建立一個新的local分支,且已經同步remote git checkout <brach-name> ``` ### 檢視 ```bash= git branch # local branch git branch -r # remote branch git branch -a # both local and remote branch ``` ### 創建(local) ```bash= git clone <repo-name> # 在local創建分支,同時切換到新分支 git checkout -b <branch-name> # checkout 可以縮寫成`co` ``` :::info :book: 如果想要創建(local)分支`feature/kickuser`,Local端已經有`feature`這個名稱的分支的話,git會丟fatal error。 ::: #### Example 如果想要創建的分支長這樣 ``` feature/kickuser ``` 輸入: ```bash= git co -b feature/kickuser ``` ### 刪除(local) ```bash= git branch -d <branch-name> // 強制刪除 git branch -D <branch-name> ``` ### 刪除(remote) TODO! ### 更新 > 假如要更新remote branch (feature/kickuser)的到local ```bash= git pull origin feature/kickuser ``` ## [Github] Branching and Pull Request流程 ```bash= git clone <repo> git checkout -b <branch-name> git add <files> git commit -m "add files" git push -u origin <branch-name> ``` :::warning :warning: `push`到remote branch的時候,remote branch的名稱要跟local branch的名稱一樣! ::: 成功推上去後,到剛推的分支下面,會有`Compare & pull request`的按鈕,這樣就可以pull request給原作者code review了。 :::success :smile: 如果pull request的內容被comments,修正過後,在conversation的欄位附上更新版的hash code,再從新提交一次code review即可。 ::: ## 突然要緊急處理插件 ```bash= git stash git pull -r <remote-branch> # <remote-branch>例如: origin develop git stash pop ``` ## gitignore for macOS ```bash= # Generated by macOS .DS_Store .DS_Store? ._* .Spotlight-V100 .Trashes Icon? ehthumbs.db Thumbs.db ``` ## 刪除 ### 已經簽上repo的檔案 ```bash= git rm --cached <file> git commit -m "Deleted file from repository only" git push ``` ### 指定資料夾與資料夾底下的檔案 ```bash= git rm -r <folder> git commit -m "Deleted the folder from the repository" git push ``` ### 刪除已簽上repo的資料夾底下的全部檔案 ```bash= git rm -r --cached FolderName git commit -m "Removed folder from repository" git push origin master ``` ## 暫存 改到一半要切到別的分支或pull其他線上分支的時候,可以把剛剛改的變更**暫存**起來。 ```bash= git stash ``` 提取剛剛的暫存,回到剛剛工作的狀態 ```bash= git stash pop ``` 清除暫存 ```bash= git stash drop // 把所有暫存清除 git stash clear ``` ## Merge & Conflict ### 情境題:Pull到一半有conflict了,想退回先不合併 ```bash= git merge --abort ``` ### 情境題:If you're already in conflicted state... ```bash= // 如果要用自己改的,把`theirs`改成`ours` git checkout --theirs path/to/file ``` ## Reset 回到前一版 ```bash= git reset <commit-hash>^ // Or git reset HEAD^ ``` 往回N個版本 ```bash= git reset <commit-hash>~(N) // Or git reset HEAD~(N) ``` ## `Revert > [Reference](https://stackoverflow.com/questions/21693295/whats-the-svn-revert-equivalent-in-git) ==If files are not staged== ```bash= git checkout -- . ``` *Revert* specificed file ```bash= git checkout HEAD -- path/to/some/file.txt ``` ## Fast Forward > TODO! ## Reference - [與其它開發者的互動 - 使用 Pull Request(PR)](https://gitbook.tw/chapters/github/pull-request) - [Git Branches: List, Create, Switch to, Merge, Push, & Delete](https://www.nobledesktop.com/learn/git/git-branches) - [How to create a pull request in GitHub](https://opensource.com/article/19/7/create-pull-request-github) - [Mac中Git忽略.DS_Store文件](https://orianna-zzo.github.io/sci-tech/2018-01/mac%E4%B8%ADgit%E5%BF%BD%E7%95%A5.ds_store%E6%96%87%E4%BB%B6/) - [How To Delete File on Git](https://devconnected.com/how-to-delete-file-on-git/) - [How to remove a directory from git repository?](https://stackoverflow.com/questions/6313126/how-to-remove-a-directory-from-git-repository) - [Gitflow](https://blog.hellojcc.tw/the-flaw-of-git-flow/) - [Stash(暫存)](https://backlog.com/git-tutorial/tw/reference/stash.html) - [【狀況題】剛才的 Commit 後悔了,想要拆掉重做…](https://gitbook.tw/chapters/using-git/reset-commit) - [What's the svn revert equivalent in git?](https://stackoverflow.com/questions/21693295/whats-the-svn-revert-equivalent-in-git) - [Resolve Git merge conflicts in favor of their changes during a pull](https://stackoverflow.com/questions/10697463/resolve-git-merge-conflicts-in-favor-of-their-changes-during-a-pull) - [Delete remote branch and local branch](https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely)