Git
Git
2022/06/29 (新增連結)
基本概念
Git是個分散式版本管理系統 (Version Control System),所以,使用Git的使用者都會擁有一份完整的程式碼。Git本身並沒有權限管控,所以,一般而言,會利用git server來協助權限管控。如果不想自己架設git server,可以使用git雲端服務。常用的git雲端服務有:github、 gitlab、 bitbucket。這三個雲端服務的主要差別在於在github免費的專案都必須是公開的,所以,目前大部分的open source專案都可以在github上找到,也可以有免費的私密專案,但是,只能有三個成員。在gitlab上,目前不管是公開或私密的專案都是免費的。在bitbucket上,允許5個成員以下的小專案(不管是公開或私密)免費使用 (2017/4/11起提供教育單位免費免限制的使用)。Bitbucket在權限的管控上比較完整,有四個層級:Global、Project、Repository、Branch (詳參: 4 Levels of Bitbucket Server Permissions)。
組態管理在敏捷開發很重要,由於週期很短,所以,通常會進行持續性整合、持續性交付甚至DevOps,沒有組態管理工具,就很難進行持續性整合。當然,組態管理不是敏捷開發的專利,傳統的開發方法下,只要是開發人員的數量到了一定程度,就會使用組態管理工具。
![](https://www.google.com/images/icons/product/drive-32.png)
2022/04/28 (更新投影片)
在github開帳號
如果使用google登入,還是要設定帳號、密碼,否則無法直接跟github溝通
在gitlab開帳號
如果使用google登入,還是要設定帳號、密碼,否則無法直接跟gitlab溝通
下載git
git init
Git Clone
Stage changes (利用vs code)
Commit
Push
Conflict
Branch
設定
當使用git時,我們會有本地資料庫(local repository)及遠端資料庫(remote repository),通常我們會把遠端資料庫(remote repository)設在git server上。
Git (王曉瑜的git筆記)
首先,我們要先設定remote repository,可以使用github或gitlab。先要開一個帳號,接下來開一個專案。
接下來,要進行本地端的設定,首先,先安裝git。本地端可以下載git for Windows或者Download for macOS 。使用macOS時,如果已經安裝過XCode,git就已經安裝,可直接使用。如果安裝過homebrew,建議利用homebrew安裝,如果沒有,就透過binary安裝。
安裝好之後,要做一些設定,首先,在命令列下(如:git bash)設定使用者的名字與email。
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
Git Advanced Configuration : An in-depth look at Git config commands and the Git config file.
要讓git了解這個專案是個git專案,就要先進行初始化。這時候,會產生一個.git的隱藏資料夾,在這裡面會有git所需要的檔案 (本地資料庫),如果沒有進行這個步驟,git就不認得這個專案。記得,這個動作只要做一次,重複進行會把原本的紀錄刪除重建。
$ git init
** 有時候,有些工具 (如:create-react-app)會自動把專案設定為git專案,就不需要進行初始設定。但是,要注意branch的名稱。
另外,要注意github以及gitlab產生新專案的預設主分支是main (過去是master),但是利用git init的時候,local的預設主分支是可能是master。所以,當我們要push main時,會有同樣的問題,因為local的分支的名稱是master。
$git push origin main
改local branch的名稱 (如:master到main)
$git branch -m master main
接下來就是連接遠端資料庫,這個指令的意思是設定遠端資料庫,add是新增一個遠端資料庫,origin是遠端資料庫的名稱 (你可以用其他的名稱),後面是遠端資料庫的URL。
$ git remote add origin https://gitlab.com/benwu/web.git
如果遠端的專案都已經設定好了,也可以先開一個空白的資料夾,利用clone複製遠端的專案
$ git clone https://gitlab.com/benwu/web.git
基本操作
基本上這些操作都可以利用編輯器裡的功能進行,也可以透過指令操作,我們來看一下常用的操作與對應的指令。
status
status可以看目前的狀態,對於初學者來說,可以比較清楚的理解目前的狀況
$ git status
add
接下來,進行git的基本操作,第一件事是設定哪些檔案已經完成 (staged)。通常會利用編輯器裡的功能,如果要利用指令,會使用git add,如果要將這個資料夾裡的所有檔案都設定為已經完成,可以下:
$ git add .
如果只是一個檔案 (如:index.html)
$ git add index.html
或者所有的html檔案
$ git add *.html
如果想知道那些檔案被加入,可以利用:
$ git status
commit
Commit是將剛剛的更動儲存到本地的資料庫,如果要利用指令,會使用git commit,並且會設定這次更動內容的註解,例如:「home page」。
$ git commit -m "home page"
Your Git Commit History Should Read Like a History Book. Here’s How.
Use git hooks to enforce Conventional Commits
遠端操作
查看 local/remote repository 的關係
連結 local-remote 的關係
更新 local 端的分支到不同的 remote 端
管理remote
新增remote,請複製你的github/gitlab的連結 (如:https://gitlab.com/benwu/web.git)
$ git remote add origin https://gitlab.com/benwu/web.git
也可以clone remote (remote的預設名稱是 origin)
$ git clone https://gitlab.com/benwu/web.git
看目前所有已新增的remote
git remote -v
push
Push是將更動從本地資料庫送到遠端資料庫,注意,如果沒有設定好遠端對應的branch,會無法進行push,請利用upstream設定 (詳見:分支操作)。
$ git push
如果你有多個遠端,也可以指定遠端,例如:origin
$ git push origin
fetch
fetch是從遠端資料庫取得內容,注意,如果沒有設定好遠端,會無法進行fetch。
$ git fetch
如果你有多個遠端,也可以指定遠端,例如:origin
$ git fetch origin
pull
Pull是從遠端資料庫取得內容,並且進行合併,如果,注意,如果沒有設定好遠端(upstream),會無法進行pull,請利用upstream設定 (詳見:分支操作)。
$ git pull
如果你有多個遠端,也可以指定遠端,例如:origin
$ git pull origin
分支操作
一般而言,我們會將已完成的工作push到遠端資料庫讓大家使用,可是如果有人就是很白目,常常把沒完成的內容push上去,那怎麼辦呢? 比較容易的解決方法是利用分支。
3.4 使用 Git 分支 - 分支工作流程 (還沒翻譯)
3.5 使用 Git 分支 - 遠端分支 (還沒翻譯)
3.6 使用 Git 分支 - 衍合 (rebase 還沒翻譯)
Better Git branching strategy — Multi-apps, monorepos and multiple teams in focus.
新建及切換分支
新建分支的指令就是利用git branch,例如要加個ben的分支,可以:
$ git branch ben
切換分支
$ git checkout ben
或者,這時候,會從現在的分支建立一個分支,並且切換到該分支。
$ git checkout -b ben
如果想切換到其他的分支,例如:master
$ git checkout master
切換分支之後,所有的commit、push、pull就是針對該分支進行。
如果不清楚自己在哪個branch,可以利用git branch來看現在在哪個分支上:
$ git branch
看遠端的branch
$ git branch -r
修改內容後,執行push
$ git add .
$ git push
$ git commit -m "date added"
如果看到:
fatal: The current branch ben has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin ben
push並設定upstream
$ git push --set-upstream origin ben
如果只要設定upstream
$ git branch --set-upstream-to=origin/ben ben
push特定的分支 (如:ben)
$ git pull origin ben
合併分支
當我們要把benwu合併到原本的master時,我們要先切換回master,再去進行合併:
$ git checkout master
$ git merge ben
如果有衝突,請先參閱解決衝突,解決衝突之後,才能完成merge。
接下來就可以push已經合併了ben的master
$ git push
How to adopt a Git branching strategy
Use feature branches for all new features and bug fixes
Merge feature branches into the master branch using pull requests
Keep master branch up-to-date
You don’t need Feature Branches anymore… (另一種觀點)
What is Trunk Based Development?
The team develops without using any branches
Trunk Based Development helps teams improve in a number of areas:
Deployment Frequency and Mean Time to Recovery
Code Quality and Knowledge Sharing
Teamwork
Potential Challenges:
Unfinished features
Tests and monitoring
Frequent Feature Deployments with large teams
If you want to do regular feature releases multiple times a day with a large team, just go with Trunk based development.
conflict
rebase
An introduction to Git merge and rebase: what they are, and how to use them
What does rebase mean?
Side effects
Alright, gotcha. But why should I rebase?
Increases tests reliability
Makes debugging easier
Solve conflicts before merging
Intro to Interactive Rebasing in Git and Customizing Vim Preferences
Differences Between Git Merge and Rebase — and Why You Should Care
workflow
3 Git Workflows to Showcase Unfinished Work : Using branching to your advantage to run a successful demo
The Solo Workflow
The Integration Branch Workflow
The Multi-Environment Review Workflow
常見問題
「error: src refspec xxxx does not match any」
可能的原因:
1.local沒有這個branch (可能打錯字)
$ git push origin mian
另外,要注意github以及gitlab產生新專案的預設主分支是main (過去是master),但是利用git init的時候,local的預設主分支是可能是master。所以,當我們要push main時,會有同樣的問題,因為local的分支的名稱是master。
$ git push origin main
改local branch的名稱 (如:master到main)
$ git branch -m master main
2.加上HEAD (『Git』git push相關與error: src refspec does match any解法)
$ git push origin HEAD:master
3.local跟remote名稱不一致 (Willy's Fish教學筆記 )
Local name : ZeWeiNote
Remote name : feature/ZeWeiNote
fast-forward (待修改)
[Ting's筆記Day3] 解決Git常見錯誤 non-fast-forward問題
在git push之前先進行git fetch及git merge
git push -f
注意! 雖然較簡單,但是,遠端的內容會被本地內容完全取代
「You are not allowed to force push code to a protected branch on」
當使用Gitlab時,為了保持某些branch(如:master)的完整性,系統會有protected branch的功能,第一、避免protected branch被任意產生,第二,避免protected branch被更動(push),第三、避免protected branch被強迫更動(force pushing),第四、避免protected branch被刪除。在setting下,選擇repository,下面可以找到protected branch的設定。
解決方案:1.設定開發者(developer)及管理者(masters)可以merge,這樣的話,所有的更動都應該,先新增branch,等確定之後,合併(m,erge)回master(最好的做法)。2.可以開發者(developer)及管理者(masters)可以push,這樣的話,還可以管制protected branch的產生與刪除。3.可以將該branch改為unprotected (最不好的做法)。
如何取消剛剛的commit?
(待完成)
git revert是用一次新的commit來回滾之前的commit,git reset是直接刪除指定的commit
如何取消剛剛的merge?
(待完成)
參考資料
Making a self-committing micro Git
git init
git commit
A Step-By-Step Guide to Becoming a Git Expert
What is Git?
Basic Git Workflow
Log
Branching & Merging
Remote Repository
Git Collaboration Workflow
How to Make a Perfect Pull Request
Add “Why” Code Comments
Make Your PRs Small
Make a Clear Description
Comment Your Own Pull Request
Discuss the Overall Approach Before Implementing the Whole Feature
Rebase Onto Fresh Master Before Creating a PR
Respond to Reviews Quickly
Don’t Amend Already Reviewed Commits — Send New Ones
Thank Reviewers for Their Suggestions
Server
首先,要在git server上建帳號 並開好repository及設定好repository的權限,通常,每個repository會有一個URL,利用這個URL來存取repository。常用的雲端服務有: GitHub、Bitbucket及GitLab
github
免費的方案 (詳參:Github pricing )
Unlimited public repositories
Unlimited private repositories ** NEW
3 collaborators for private repositories
Issues and bug tracking
Project management
The GitHub Development Workflow
The Fundamentals
Creating a Repository
Creating an Issue
The GitHub Flow
Creating a Branch
Committing a file
Opening a Pull Request
Merging a Pull Request
GitHub Flow vs. OneFlow: Which Git Workflow Works the Best for You?
Getting started with Git and GitHub: the complete beginner’s guide
How to be more productive on GitHub
Quick fuzzy file search in repositories
Code change suggestions in pull requests
Navigate the code tree like in an IDE
Jump to a function when reviewing code
Creating a permalink to a file
Viewing the blame and change recency heatmap
Powerful code search
Saved replies
Change your default text editor associated with Git
Change your dotfiles
Install Hub
Practice merge conflicts
Make a GitHub Page
A beginner’s guide to Git — how to start and create your first repository: If you are a developer and you want to get started with Git and GitHub, then this article is made for you.
Introducing GitBuddy: A Changelog and Release Manager for GitHub
Is Your Package File Older Than You Are?
GitHub has an automatic service that sends you an email when your dependencies are out of date.
4 New GitHub Products That Will Change How You Code
Codespaces
Discussions
Code Scanning and Secret Scanning
Private Instances
GitHub on Chrome — Git Setup for Pros!
Enhanced Github
Github Code Folding
Github Dark Theme
Refined Github
Octotree
4 Tips For Using GitHub More Efficiently
Tip #1 Search Files In The Repo Quickly And Easily With File Finder
Tip #2 Use Search Qualifiers To Search For The Target You Want
Tip #3 Enable the Read Me on Your Github Profile Page
Tip #4 Add The Badges To Your GitHub Repo
The new GitHub is here : What you should keep from GitHub Universe 2020
GitHub Discussions
GitHub CLI
Dependency Review
GitHub Sponsors
GitHub Actions
New Dark Theme
8 Cool GitHub Tricks To Make You Look Like A Senior Developer
Actions
Hook
Templates
gitlab
Automatic semantic versioning using Gitlab merge request labels
在gitlab上開repository (詳參: 學習版本控制基礎 - Git & Gitlab)
免費的方案 (詳參: Gitlab pricing)
2,000 CI pipeline minutes per group per month on our shared runners
Unlimited private projects and collaborators
Community Support
Built-in CI/CD
Project Issue Board
ChatOps
Client
git client有很多 (詳參: https://www.slant.co/topics/2089/~git-clients-for-windows)
Visual Studio Code **
VS Code的Git功能,是基於本機電腦上安裝的Git套件。如果要使用的話,要先確認Git套件是否已經安裝。若無,請至Git官網的download頁面下載安裝檔以進行安裝。
利用git clone就可以下載一個已經存在的專案
Commit, Push, Pull, Branch
VS CODE X GITLAB 首次註冊使用教學 如何從 VSCODE 上傳到 GITLAB
ssh
Using Version Control in VS Code (VS Code 官方文件)
SCM Provider extensions
The Git CLI Is Dead: Are You Still Using Git from the Command Line?
Plug-ins in VS Code
GitLens
Git Graph
GitLive
Git Automator
Sourcetree
GitKraken
Katalon Studio
Eclipse
目前的Eclipse版本(Helios以上)已經內建git,以下就以Eclipse的內建功能為例說明相關步驟。
要先有一位同學(如:組長)先建立local repository,並將專案commit到local repository,然後再將local repository的內容push到remote repository (在git server上)。完成後,就可以在git server上看到專案的內容了。
其他同學會利用Import下載專案,首先,先選擇Import,Git/Projects from Git,因為目前還沒建立local repository,所以,先選擇Clone URI,選擇remote repository上的branch(例如:master)以及local repository的路徑。
當任何一位同學修改專案內容時,就可以進行Commit and Push,將更動的內容送到remote repository給其他同學,其他同學可以利用Pull下載最新的更動。也可以利用workspace synchronization進行同步,同時上傳也下載最新的更動,如果有多人同時更動同一檔案,也可以比對各版本的內容。
現在的版本管理軟體都支援分支(branch)的概念,原本是讓程式開發者能依據不同的使用者(如:公司或單位)開發不同的版本,不過,現在即使是只開發一個版本,也常使用分支。首先,每個repository會有一個稱為"master"的分支。通常,我們會維持master為最終穩定版本。為避免影響目前的最終穩定版本,當每位同學要開始進行修改時,會先產生一個新分支,當這個分支被完整測試後,再合併(merge)回master。這樣的話,就比較不會因為某一位同學push有問題的程式碼而造成大家都找不到最終穩定版本 (詳參: https://backlogtool.com/git-guide/tw/stepup/stepup1_1.html)。
參考資料 (待整理)
Git
30 天精通 Git 版本控管 (作者: 黃保翕 / Will 保哥 )
git for windows + github for windows + SourceTree
為你自己學 Git (作者: 高見龍)
有紙本書、電子書、網頁版、有影片
pro Git (2014) 2nd Ed.
The Architecture and History of Git: A Distributed Version Control System
Why you should write merge requests like you’re posting to Instagram
Git Tutorial for Beginners: Basic Git Commands
git init
git status
git add .
git commit -m “description for this commit.”
git log
Git Tutorial for Beginners: Remote Repository Management
At start, we already have a local repository in our machine.
Create a remote repository in GitHub, well known cloud service for Git repository.
Push local repository to remote repository.
git push [Remote_URL] master
Pull from remote repository to another local repository for simulating cooperation between teammates.
git pull [Remote_URL] master
For convenience, create short name for remote repository.
git remote add [Short_Name] [Remote_URL]
git push [Short_Name] master
git pull [Short_Name] master
git remote -v
10 Git Commands You Should Know
Inspecting Things
git diff
git log
git blam [filename]
git reflog
Undoing Things
git reset --hard HEAD
git checkout my_commit
git revert my_commit
git clean -n
Tidying Things
git commit --amend
git push my_remote --tags
Change Default Editor
git config --global core.editor "atom --wait"
Less Common Git Commands That Are Good to Know
git diff
git stash
git grep
Branch
Git Flow
Most common Git mistakes and how to fix them
Spelled last commit message wrong
Spelling mistake on branch name
Accidentally committed all changes to the master branch
Forgot to add a file to that last commit
Added a wrong file in the repo
Oops… I did it again
undo
Git Repository Transfer Keeping All History: How to replicate your Git repo and keep all previous commits, branches, and tags
單一的repository (Monorepos)
The frank reality is that, at scale, how well an organization does with code sharing, collaboration, tight coupling, etc. is a direct result of engineering culture and leadership, and has nothing to do with whether a monorepo or a polyrepo is used. The two solutions end up looking identical to the developer. In the face of this, why use a monorepo in the first place? Please don’t!
The default behavior of a polyrepo is isolation — that’s the whole point. The default behavior of a monorepo is shared responsibility and visibility — that’s the whole point.
4 not-so-basic Git situations and what to do
Where is the ‘undo’ button?
Undoing unstaged changes in your workspace
Undoing staged changes
Undoing last commit
Creating your own Git commands
Tagging project releases
Rewriting history
How to use git as a standup tool
Using git shortlog
Using date range
Using the commit range
Filter by files
Formatting git shortlog
Git standup