git使用筆記

https://blog.phpgao.com/note_for_git.html


GIT雖然概念比較難理解,但不得不說他是一款開發利器。

老高總結出了一些GIT中很常見的操作命令,分享給大家。但由於GIT命令繁多,所以我將分爲基礎和進階兩部分。

I. 基礎篇:

幫助

git help # 獲取幫助,內容如下usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
           [-p|--paginate|--no-pager] [--no-replace-objects]
           [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
           [--help] COMMAND [ARGS]

The most commonly used git commands are:
   add        Add file contents to the index
   bisect     Find by binary search the change that introduced a bug
   branch     List, create, or delete branches   checkout   Checkout a branch or paths to the working tree   clone      Clone a repository into a new directory   commit     Record changes to the repository   diff       Show changes between commits, commit and working tree, etc   fetch      Download objects and refs from another repository
   grep       Print lines matching a pattern
   init       Create an empty git repository or reinitialize an existing one   log        Show commit logs   merge      Join two or more development histories together   mv         Move or rename a file, a directory, or a symlink   pull       Fetch from and merge with another repository or a local branch   push       Update remote refs along with associated objects
   rebase     Forward-port local commits to the updated upstream head
   reset      Reset current HEAD to the specified state   rm         Remove files from the working tree and from the index
   show       Show various types of objects
   status     Show the working tree status   tag        Create, list, delete or verify a tag object signed with GPG

配置git

# 查看配置git config -l/--list# 以下是可能出現的配置core.symlinks=falsecore.autocrlf=truecolor.diff=auto
color.status=auto
color.branch=auto
color.interactive=truepack.packsizelimit=2g
help.format=html
...
...# 配置全局信息git config --global user.name=phpgao# 配置局部信息git config --system [email protected]# 查看某一個配置信息git config user.email

初始化倉庫

git init # 在當前目錄初始化一個git倉庫git init --bare # 在當前目錄初始化一個git裸倉庫

查看

git status # 顯示工作流中的狀態git diff # 顯示工作目錄(Working tree)和暫存區域快照(index)之間的差異git diff --stat # 簡報git diff --cached # 顯示已經暫存起來的文件(staged)和上次提交時的快照之間(HEAD)的差異git diff --staged # 下一次commit時會提交到HEAD的內容(不帶-a情況下)git diff dev # 比較當前目錄和dev分支 git diff HEAD # 工作目錄和HEAD的差別git diff HEAD^ HEAD # 比較上次和上上次提交的不同git diff dev master # 比較兩個分支最新提交git diff dev..master # 同上git diff dev...master # 比較從分支開始時至今所有的修改git log --pretty=oneline # 顯示日誌### 美化格式一git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short### 美化格式二git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative

II. 增刪提

先讀懂這個圖

提交關係圖

git add # 添加工作區修改的文件提交至Stage(index)git commit -m "comment" # 將Stage(index)中的文件提交至本地庫中(commit),並添加註釋git commit -am "comment" # 省略了add步驟,直接提交Working Directory和Stage(index)中的內容git rm <文件名> # 刪除庫中的文件git reset --hard # 恢復最近一次提交過的狀態,即放棄上次提交後的所有本次修改git reset -- . # 從暫存區恢復到工作文件

III. 分支與合併

git branch <分支名> <老分支名># 根據分支創建新分支git branch -r # 查看遠程分支git branch -v # 查看各分支最近的提交git branch -d <分支名> # 刪除一個分支git br -D <分支名> # 強制刪除某個分支 (未被合併的分支被刪除的時候需要強制)git branch -m <分支名> <新分支名> # 重命名一個分支git checkout <分支名> # 切換至某分支git checkout -b <分支名> # 創建並切換至新分支git merge dev # 將當前分支與dev分支合併git merge dev --no-ff # 不使用Fast-Foward合併,爲了保持項目的清晰的軌跡,推薦合併時使用此選項

這裏前者到一個概念叫分支策略,可以參考這篇文章Git分支管理策略

IV. 遠程操作

clone

git clone /xx/xxx/xxx.git # 克隆某個項目git支持很多協議,如ssh、git、https等。

remote

git remote -v # 查看遠程庫git remote add origin xxxx.git # 添加一個遠程主機git remote rm # 刪除一個遠程主機

fetch

git fetch <遠程主機名> # 取回所有信息git fetch <遠程主機名> <分支名> # 只取回某分支git branch -a # 查看所有分支git branch -r # 查看遠程分支

pull

git push

push


V. jetbrains系列軟件問題

GIT Remotes "Can't push, because no remotes are defined"

軟件中沒有添加remote的功能,所以如果你要新加入一個遠程庫就需要在terminal中使用以下命令

git remote add origin "path to .git"

fatal: No existing author found with 'john doe'

先使用git config -l查看配置,得到name和email如下

user.name=aaa
[email protected]

軟件配置裏填入

aaa [email protected]

這樣軟件轉化的命令就變成

git commit --author="aaa <[email protected]>" -m "Note"

參考:

https://ruby-china.org/topics/939


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章