雪花台湾

Git常用命令及日常問題集錦

Git當下最流行的版本管理工具,結合自己工作中的實際應用做了以下梳理。

基礎命令

分支操作

修改遠程倉庫地址

遠程分支獲取最新的版本到本地

  1. 首先從遠程的origin的master主分支下載最新的版本到origin/master分支上git fetch origin master
  2. 比較本地的master分支和origin/master分支的差別git log -p master..origin/master
  3. 進行合併

    git merge origin/master

拉取遠程倉庫指定分支到本地

fatal: Cannot update paths and switch to branch dev at the same time.
Did you intend to checkout origin/dev which can not be resolved as commit?

工具類

常見問題

Question1

如何解決 ``` failed to push some refs to git ```

Answer1

* ```git pull --rebase origin master``` 進行代碼合併
* ```git push -u origin master``` 即可完成代碼上傳

Question2

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/ master

Answer2

指定當前當前工作目錄工作分支,跟遠程倉庫分支之間的聯繫

git branch --set-upstream master origin/master

Question3

git pull 獲取最新代碼報以下錯誤

fatal: refusing to merge unrelated histories

Answer3

git pull之後加上可選參數 --allow-unrelated-histories 強制合併

git pull origin master --allow-unrelated-histories

Question4

使用鉤子pre-commit,提交代碼提示如下錯誤:

$ git commit -m .
sh: eslint: command not found
pre-commit:
pre-commit: Weve failed to pass the specified git pre-commit hooks as the `fix`
pre-commit: hook returned an exit code (1). If youre feeling adventurous you can
pre-commit: skip the git pre-commit hooks by adding the following flags to your commit:
pre-commit:
pre-commit: git commit -n (or --no-verify)
pre-commit:
pre-commit: This is ill-advised since the commit is broken.
pre-commit:

Answer4

#!/bin/bash
TSLINT="$(git rev-parse --show-toplevel)/node_modules/.bin/tslint"
for file in $(git diff --cached --name-only | grep -E .ts$)
do
git show ":$file" | "$TSLINT" "$file"
if [ $? -ne 0 ]; then
exit 1
fi
done

Question5

.gitignore規則不生效的解決辦法

Answer5

把某些目錄或文件加入忽略規則,按照上述方法定義後發現並未生效,原因是.gitignore只能忽略那些原來沒有被追蹤的文件,如果某些文件已經被納入了版本管理中,則修改.gitignore是無效的。那麼解決方法就是先把本地緩存刪除(改變成未被追蹤狀態),然後再提交:

git rm -r --cached . 或者 git rm -r README.md
git add .
git commit -m update .gitignore

如果您在工作中還遇到其它問題,可以在下方評論區進行提問

作者:五月君鏈接:imooc.com/article/26929

來源:慕課網

本文首次發佈於慕課網 ,轉載請註明出處,謝謝合作

推薦閱讀:

接手別人的代碼,死的心有嗎?

普通的程序員和大神級的程序員有什麼區別?

網上黑程序員的現實依據是什麼?程序員真的那麼悲慘嗎?

有哪些視頻堪稱有毒?

暴露真實IP真的沒關係嗎?

有哪些程序員特有的習慣?

月薪3萬的程序員都避開了哪些坑?

和程序猿談戀愛是一種怎樣的體驗?


推薦閱讀:
相关文章