[Git] 常用指令

  • 初始化使用者資訊
git config --global user.email "my_email"
git config --global user.name "my_name"
  • 初始化git目錄
git init
  • 查看git狀態
git status
  • 一次加入所有更改檔案
git add .
git add -A
  • commit
git commit -m “commit message”
  • 操作遠端repo
# 顯示資訊
git remote -v
git remote show origin
# 刪除目前origin
git remote rm origin
# 新增origin
git remote add origin git@github.com:SongRongLee/xxxxx.git
  • 第一次push/pull
git push -u origin master
or
git branch -u origin/master
#以後只需要:
git push
  • 返回特定commit
git reset --hard HEAD^
git reset --hard HEAD~2
  • branch相關
# 以遠端分支為藍本建立本地new-branch
git checkout -b new-branch origin/master
# Delete a remote branch
git push -d origin bad-branch

# Delete a local branch
git branch -d bad-branch

# Sync remote branch information after deletion
git fetch --prune

# List all remote and local branches
git branch -a
# 修改branch名稱
git branch -m my-new-name
# push本地的local-branch到origin/remote-branch
git push origin local-branch:remote-branch
# Shows changes from from_branch to to_branch
git diff from_branch to_branch
  • stash相關
# pull但是保留local change
git stash
git pull
git stash pop

# Stash untracked files
git stash -u

# List all stashes
git stash list

# Pop a certain stash
git stash pop stash@{n}

# Stash with a given message
git stash -m my_message
  • 刪除first commit
git update-ref -d HEAD
  • Unstage all changes
git reset
  • Discard changes on one file
git checkout -- filename
  • Force overwrite local repo
git reset --hard origin/master
  • Move to next commit
git reset HEAD@{1}
  • Rename
git mv
  • Show ignored files
git status --ignored
  • Remove untracked files and directories
git clean -dn  # Dry run
git clean -df  # Actual remove
  • Check SHA-1 of commits from history
git reflog

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *