git 常用指令

git 常用指令整理

雖然已經每天跟 git 為伍了,也有把常用指令整理整理在一個 markdown 檔,但還是放到網頁上在開發的時候比較容易看~

1. 將 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

2. git clone

# 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

3. git branch

# 新增 branch
$ git branch [branch name]

# 如果有 [branch name] 就切換過去,沒有的話就建立該 branch 並切換到該 branch
$ git checkout -b [branch name]

# 將 branch apple 重新命名為 banana
$ git branch -m apple banana

# 刪除 local 的 branch
$ git branch -d [branch name]

# 強制刪除 local 的 branch
$ git branch -D [branch name]

# 刪除 remote 的 branch
$ git push origin --delete [remote branch name]

# 查看 local 的 branch
$ git branch -v

# 查看 remote 的 branch
$ git branch -rv

# 同時查看 local 和 remote 的 branch
$ git branch -av

# 在 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] 

4. git tag

# 在 local 端新增 tag
$ git tag [tag_name]

# push local 的 tag 到 remote
$ git push origin [tag_name]

# 刪除 local 端 tag
$ git tag -d [tag_name]

# 刪除 remote 端 tag
$ git push --delete origin [tag_name]

5. 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>

6. git merge

# merge branchA 到 branchB
$ git checkout branchB
$ git merge branchA

7. git stash

# 暫存修改過的檔案
$ git stash

# 暫存修改過的檔案(包含 untrack 的檔案)
$ git stash -u

# 查看目前有哪些暫存
$ git stash list

# 把最新一次的暫存拿出來用
$ git stash pop

8. git diff

# 比較目前檔案和上一個 commit 作的更動(如果目前沒有修改任何檔案並儲存,那 output 結果會是空的)
$ git diff

# 比較某個檔案和上一個 commit 作的更動
$ git diff [檔案的相對路徑]

# 比較兩個 branch 的更動
$ git diff [branch1] [branch2]

8. 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 新增檔案

9. rebase 相關

# rebase
# pull 目前 master 的進度
$ git pull origin master

# merge origin branch
$ git pull origin master --no-rebase

# merge 到一半,要捨棄目前的 merge
$ git reset --hard ORIG_HEAD

10. config 相關

# 依照 commit date 排序顯示 branch list
$ git config --global branch.sort -committerdate

# 依照版本號順序顯示 tag list
$ git config --global tag.sort -version:refname

如果覺得我的文章有幫助的話,歡迎幫我的粉專按讚哦~謝謝你!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top