雖然已經每天跟 git 為伍了,也有把常用指令整理整理在一個 markdown 檔,但還是放到網頁上在開發的時候比較容易看~
# 將 Respository 加入 git
$ git init
$ git add .
$ git commit -m "[commit message]"
$ git remote add origin [github repository url]
# remove remote
# git remote remove origin
# 第一次 push
$ git push --setupstream origin [branch name]
# 以後 push
$ git push
# clone 別人的 repo 後 push 到自己的 new repo
$ git clone [someone's github repository url]
$ git remote rename origin upstream
$ git remote add origin [new repository url]
$ git push origin master
# branch 相關
# 新增 branch
$ git branch [branch name]
# 將 branch apple 重新命名為 banana
$ git branch -m apple banana
# 刪除 branch
$ git branch -d [branch name]
# 強制刪除 branch
$ git branch -D [branch name]
# 查看 local 的 branch
$ git branch -v
# 查看 remote 的 branch
$ git branch -rv
# 同時查看 local 和 remote 的 branch
$ git branch -av
# 如果有 [branch name] 就切換過去,沒有的話就建立該 branch 並切換到該 branch
$ git checkout -b [branch name]
# 在 feature branch 開出 sub branch
$ git checkout -b sub feature
# 或是
$ git branch sub feature
# 新增 local branch 並連結 remote repository 的 branch
$ git checkout --track origin/[remote branch name]
# 等同於
$ git checkout -b [remote branch name]
$ git branch --set-upstream-to=origin/[remote branch name] [remote branch name]
# 新增 local branch 並 push 到 remote 上
$ git checkout -b [branch name]
$ git push -u origin [branch name]
# 在 remote 和 local delete branch
# delete branch locally
$ git branch -d localBranchName
# delete branch remotely
$ git push origin --delete remoteBranchName
# commit 相關
# 捨棄最後一次的 commit
$ git revert HEAD --no-edit
# 捨棄最近一次的 commit
$ git reset master^
# 捨棄目前 branch 上最近一次的 commit
$ git reset HEAD~1
# 恢復到某次 commit 的狀態,同時更新 remote 狀態
$ git reset SHA1 --hard
# 強制 push,忽略 remote 版本較新的問題
$ git push -f
# 捨棄某次的 commit
$ git revert SHA1
# 修改 commit message - 尚未 push 到遠端
$ git commit --amend -m "New message"
# 修改 commit message - 已經 push 到遠端,修改目前最新的 commit message
$ git commit --amend -m "New message"
$ git push --force origin <branchName>
# cherry-pick
$ git cherry-pick <commit-hash>
# git stash
# 暫存修改過的檔案
$ git stash
# 查看目前有哪些暫存
$ git stash list
# 把最新一次的暫存拿出來用
$ git stash pop
# ignore 相關
# 暫時忽略已經 tracked 的檔案
git update-index --assume-unchanged path/to/file
# 解除忽略已經 tracked 的檔案
git update-index --no-assume-unchanged path/to/file
# 在 local 設定不要推上 remote 的檔案
# -> 在 .git/info/exclude 新增檔案
# rebase
# pull 目前 master 的進度
$ git pull origin master
# merge origin branch
$ git pull origin master --no-rebase
# merge 到一半,要捨棄目前的 merge
$ git reset --hard ORIG_HEAD
# tags 相關
# 在 local 新增 tag
$ git tag <tag_name>
# 把 tag push 到 remote
$ git push origin <tag_name>
如果覺得我的文章有幫助的話,歡迎幫我的粉專按讚哦~謝謝你!