可以使用git branch -a
命令得知在Git初始化階段主分支尚未被建立,因使用git checkout -b <branch_name>
時並不會創建新分支,而是更改未來的主分支名稱。
GitHub為例(主要分支名為main
):
git init -b main
git remote add origin <your repository link>
git add .
git commit -m "Initial commit"
git push -u origin main
GitHub為例(主要分支名為main
):
git init -b main
git remote add -f -m main origin <your repository link>
git checkout -b main -t origin/main
進行協作,先在目前資料夾路徑cloone遠端儲存庫
git https://github.com/[UserID]/[projetID].git .
git remote set-url origin --push --add <遠端路徑>
git rm -r --cached <your directory>
git checkout -b new_branch # 建立 local branch
git push -u origin new_branch # 建立對應的遠端 branch
git checkout origin/new_branch -b new_branch # 建立 local new_branch 並與遠端連接
git checkout branch_name
git merge -m "merge commit message ..." --no-ff otherBranch # 不使用fast-forward
git branch -d <branch_name>
git branch -D <branch_name> #強制刪除
git push -d origin <branch_name>
git branch <branch_name> -u <remote_name>/<branch_name>
git reset <option> HEAD~1
git push origin -f
git revert HEAD~1
git tag new old
git tag -d old
git push origin :refs/tags/old
git push --tags
git push --delete origin <tagName> #遠端
git tag -d <tagName>
git stash save [message]
git stash list
git stash pop [stash ID]
git stash drop [stash ID]
git stash clear
git submodule add <.../Author/repo.git> <dir/repo>
初始化更新
git submodule update --init --recursive
之後更新
git submodule update --recursive --remote
git submodule deinit the_submodule
git rm the_submodule
# Unix
rm -rf .git/modules/path_to_submodule
# PowerShell
rm .git/modules/path_to_submodule -r -fo
git submodule deinit -f .
git submodule update --init --recursive
將多個commit壓縮成一個提交記錄。
用於合併不同分支時,希望在合併後只有一個提交記錄。
$ git merge —-squash <branch_name>
合併本地端所在分支的提交紀錄,用於整理自己的開發歷程。
$ git rebase -i <shl-1>
可使用
git log —-oneline
查看目前提交記錄
(cherry pick from commit ...)
並合併提交git cherry-pick -e -x <commit>
Get the commit
git cherry-pick -n <commit>
Unstage everything
git reset HEAD
Stage the modifications you want to keep
git add <path>
Make the work tree match the index
# (do this from the top level of the repo)
git checkout .
git checkout --orphan gh-pages #建立並切換本地孤立分支 gh-pages
git reset #清除內容索引
git commit --allow-empty -m "Initial commit" #創建空的初始commit
git checkout --force master #強制切換本地分支 master,消除有關孤立分支覆蓋文件的警告
git worktree add <folderPath> gh-pages # 新增一個子目錄到worktree,並成為gh-pages分支
#...產生文件到子目錄...
cd <folderPath> #切換至子目錄,即切換至gh-pages分支
git add --all #文件加入暫存區
git commit -m 'commit message' #創建commit
git push #推送至遠端
cd .. #切換回工作目錄
新增upstream git
git remote add upstream <git link>
獲取上游git最新進度
git fetch upstream
git fetch upstream --tags # If the project has tags
本地 git master 分支為上游git鏡像
git checkout master
git merge upstream/master
當您想與上游維護人員分享一些工作時,您將創建一個功能分支。如果您滿意,請將其推送到您的遠程存儲庫。
您也可以使用rebase,然後merge以確保上游有一組乾淨的提交(理想情況是一個)來評估:
git checkout -b feature-x
#some work and some commits happen
#some time passes
git fetch upstream
git rebase upstream/master
如果你需要將一些提交壓縮成一個,你可以在這一點上使用rebase interactive
完成上述步驟後,只需簡單推送即可在遠程fork中發布您的工作:
git push origin feature-x
如果您在發布遠程分支功能後,因為上游維護人員提供了一些反饋必須更新遠程分支功能-x,則會出現一個小問題。你有幾個選擇:
git push -f origin feature-x
# list changes
git ls-remote
# fetch patchset 1 of change 10647
git fetch <repo> refs/changes/47/10647/1
# checkout new branch as the patchset
git checkout -b <branch> FETCH_HEAD
# if to modify commit bbc643cd
git rebase --interactive bbc643cd~
在預設編輯器中,在提到bbc643cd
的行中修改pick
為edit
, 儲存文件並退出。
git 會移到bbc643cd
提交,執行以下命令進行更改
git commit --amend
更改完畢後執行以下命令返回HEAD
git rebase --continue
提交更改到Gerrit
git push origin HEAD:refs/for/<target-branch>