Git 版本控管 === ###### tags: `Version Control` ## 版本控制介紹 ## Git的基本指令 | Git 命令 | 說明 | | ------------------------------------------ | --------------- | | git config --list | 查詢config設定 | | git config --global user.name "使用者名稱" | 設定使用者名稱 | | git config --global user.email 使用者 Email | 設定使用者email | | git checkout --orphan gh-pages | 建立一個沒有parent的branch,並移到該branch上 (GitHub只允許將網頁放於gh-pages) | | git add -A | 加入buffer, -A代表加入全部 | | git commit -a -m "message" | 上傳至local server | | git push origin gh-pages | 上傳gh-pages這個branch到remote server | | history | show出Git命令操作步驟 | ## Git 實務操作 - **(2017.06.18) 創建GitHub網頁步驟** - 網頁連結 https://ysceason.github.io/MyTest/MyTest.html ``` Eason@Eason MINGW64 /d/GitServer/MyTest (gh-pages) $ history 1 git checkout --orphan gh-pages 2 git status 3 git add -A 4 git status 5 git commit -a -m "add a html file" 6 git push orinin gh-pages 7 history ``` - **(2017.07.10) 設定Username 與 email步驟** ``` EasanJhang@TPEasanJhang MINGW32 ~ $ history 1 git config --list 2 git config --global user.name EasanJhang 3 git config --global user.email EasanJhang@Altek.com.tw 4 git config --list 5 history ``` - **(2017.07.11) 初始上傳git,並開新branch** ``` 1 git status 2 git add . 3 git status 4 git commit -m "Initial version for MyCoding" 5 git branch Easan 6 git checkout Easan 7 history ``` - **(2018.11.12) GitLab 設定SSH Key** 1. 檢查ssh keys ```lang=C $ cd ~/.ssh # Checks to see if there is a directory named ".ssh" in your user directory ``` 2.新增ssh keys ```lang=C $ ssh-keygen -t rsa -C "your_email@example.com" # Creates a new ssh key, using the provided email as a label # Generating public/private rsa key pair. # Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter] ``` - 輸入$ ssh-keygen -t rsa -C "your_email@example.com" 後 - 會提醒你,按下Enter後會在 /c/Users/you/ssh/ 新增 is_rsa 檔案 - 接下來會要你設定passphrase,這會是你在push要輸入的密碼(可選擇不填) ```lang=C Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again] ``` 3. 在 C:/Users/you/.ssh/ 裡會新增一個 **id_rsa.pub**檔案,這是你的public key ```lang=C Your identification has been saved in /c/Users/you/.ssh/id_rsa. # Your public key has been saved in /c/Users/you/.ssh/id_rsa.pub. # The key fingerprint is: # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com ``` 4. 在gitlab上新增ssh keys - 將**id_rsa.pub**的內容,直接複製到 gitlab上 SSH key 的欄位 - Title 填上email - 最後Add Key ![](https://i.imgur.com/cEBJm8k.png) 5. 完成後即可做git clone的操作 ```lang=C $ git clone git@ASICSW-Git.altek.com.tw:isphost/aq360-msm8953-dual-imx477.git ``` - **(2018.11.13) branch** ``` $ git branch -r # 看遠端有什麼 branch $ git branch -a # 查詢目前local以及遠端的 branch $ git checkout origin/new_branch -b new_branch # 建立 local new_branch 並與遠端連接 $ git checkout -b new_branch # 建立 local branch $ git push -u origin new_branch # 建立遠端 branch (將 new_branch 建立到遠端) ``` - **(2019.03.07) local端有修改,又想update資料** ``` $ git stash $ git stash list # 看是否有駐存 $ git pull origin branch_name $ git stash pop # 發生衝突時不會刪除暫存 $ git stash apply # 覆蓋有衝突的文件 $ git checkout --theirs <file> $ git add <file> $ git commit -m "Force applied stash" $ git stash drop ``` - **(2019.03.21) 在Linux 安裝Smart Git** http://www.kvcodes.com/2016/03/install-uninstall-smartgit-ubuntu/ - **(2019.07.26) 將Local的master分支推送到origin遠端的master分支。如果master不存在,則會被新建。** ``` $ git push origin master ``` - **(2019.08.15) 透過Git Grep進行搜索** ``` $ git grep -n xmmap # 查詢"xmmap",'-n'表示顯示行號 $ git grep --name-only xmmap # 查詢"xmmap",只顯示檔名 ``` - **(2019.08.29) Git log** ``` $ git log --color --date-order --graph --oneline --decorate --all $ git log --pretty=format:"%h%x09%an%x09%ad%x09%s" ``` - **(2024.10.09) (合併)補上次commit** ``` $ git add 檔案 $ git commit --amend $ git push XXX // $ git push origin HEAD:refs/for/0B_ABupdate // $ git push origin HEAD:refs/for/0C // $ git push origin HEAD:refs/for/master ``` - **(2024.12.09) 切換遠端分支** - 切換到遠端的指定分支並建立一個本地分支,可以按照以下步驟使用 Git 命令完成: ``` $ git checkout -b <local-branch> origin/<remote-branch> ``` - **(2025.09.15) Gerrit環境,無法用reset** ``` # 建立 revert commit git revert a30ceb4 # 編輯 commit message(會自動加上 "Revert ..."),存檔退出 # 推送到 Gerrit review git push origin HEAD:refs/for/master ``` Reference === - [Git 版控基礎操作教學](https://www.youtube.com/watch?v=etTDkZiW1Ng) - [Git 官方網站](https://git-scm.com/) - [Tortoise Git](https://tortoisegit.org/) - [Apache HTTP Serve](https://archive.apache.org/dist/httpd/) - [在 Windows 上架設 Git Server](http://jason2506.github.io/blog/2012/05/14/setup-a-git-server-on-windows/) - [Windows架設GIT server](http://mapler-learn.blogspot.tw/2016/03/windowsgit-server.html) - [windows架git server的安裝流程紀錄](http://sam0512.blogspot.tw/2013/06/windowsgit-server.html) - [GitHub](https://goo.gl/fbo9yY) - [Web程式:如何使用 github 架設網站](https://goo.gl/UY2Epd) - [建立自己的GitHub Project Pages](https://goo.gl/NME8X3) - [程式人雜誌](http://programmermagazine.github.io/home/) - [如何在 Github Pages 建立靜態網站](https://www.youtube.com/watch?v=bU0f1IvUcZA) - [github 或者gitlab 設置添加SSH](https://bit.ly/2qvk7dh) - [開始使用gitlab-建立新專案](https://bit.ly/2RzMrXl) : 包含添加SSH - [Generating a new SSH key and adding it to the ssh-agent (GitHub)](https://bit.ly/2zusklT) - http://doraak47.blogspot.com/2013/05/git-push.html - [Filename too long in Git for Windows](https://bit.ly/2RxKbzI)