Git - Tips

01 - 臨時保存和恢復當前改動

執行git stash保存後,git status將顯示無任何改動。html

git stash  # Temporarily stores all modified tracked files

git stash list  # Lists all stashed changesets
git stash pop <stash> # Restores the stashed files, and delete the stashed files.
git stash apply <stash> # Restores the stashed files, and reserve the stashed files.

git stash drop  # Discards the most recently stashed changeset
git stash show  # Show the latest changes recorded in the stash as a diff between the stashed state and its original parent. 
git stash clear  # Remove all the stashed states.

02 - 推送失敗

不一樣的人修改同個文件的同一個地方,而後推送到遠程庫是會發生「推送失敗」,由於推送有衝突。
解決方法:先用git pull抓取最新的提交,而後在本地合併,解決衝突,再推送。
使用git pull前,必須指定本地branch分支與遠程origin/branch分支的連接(git branch --set-upstream-togit

03 - 多人協做

嘗試用git push origin <branch name>推送修改。
若是推送失敗,可能由於遠程分支比本地更新早,使用git pull試圖合併。
若是合併有衝突,則須要解決衝突,並在本地提交,再用git push origin <branch name>推送。windows

04 - 配置local repository

Local配置優先級高於global配置,並且Local的配置必須在local repository目錄下完成。
示例:安全

$ git config --local user.name "anliven"  # 配置local repository的用戶名
$ git config --local user.email "anliven@yeah.net"    # 配置local repository的郵箱

$ git config --local --list # 顯示local repository配置信息
$ git config --local --unset [value-regex]  # 去除local repository配置

$ git config --local --edit  # 交互式local repository配置

05 - 配置文件

Git配置文件優先級:local > global > systembash

配置文件 有效範圍 查看 配置方法 名稱及目錄
local 本地倉庫 git config --local --list git config --local --edit 本地倉庫目錄下,例如:<local repository>\.git\config
global 全部倉庫 git config --global --list git config --global --edit 用戶目錄下,例如:C:\Users\xxx.gitconfig
system 不建議改動 git config --system --list git config --system --edit git的安裝目錄下,例如:C:\Program Files\Git\mingw64\etc\gitconfig

06 - 合併多個commit

利用git rebase -i把其它commits標註爲squash,從而將其它commits併入一個commit。
合併多個 Commit
合併 commit 保持分支幹淨整潔app

07 - 命令執行

  • 注意git命令的執行目錄、生效目錄和執行結果中的目錄信息
  • 利用tab鍵補全目錄、文件和命令名稱
  • 使用完整的git命令,便於理解和確認

08 - 對比git pull和git pull --rebase

link
git pull = git fetch + git merge
git pull --rebase = git fetch + git rebasefetch

09 - 修改文件權限

# 在相應Repository目錄中查看文件權限
git ls-tree HEAD

# 修改權限(權限修改後,至關於文件進入了index)
git update-index --chmod=+x <test.sh>

# 提交修改
git commit -m "script permission update"

# 確認修改結果
git ls-tree HEAD

10 - 報錯:^M: bad interpreter

檢查文件格式,必要時使用dos2unix命令轉換文件格式。
在windows git下,建議關閉自動換行,並啓用安全換行檢查。ui

# 關閉自動換行的設置
git config --global core.autocrlf false

# 啓用安全換行符檢查
git config --global core.safecrlf true

11 - Git添加空文件夾

默認狀況下,git將忽略空文件夾,也就是說空文件夾沒法加入到repository。
解決辦法:在空文件夾下建立包含!.gitignore內容的.gitignore文件便可。.net

相關文章
相關標籤/搜索