Git命令使用筆記

Git中文件管理三種狀態:

已修改(modified)

在工作目錄中修改Git文件

已暫存(staged)

對已經修改的文件執行暫存操作,存入暫存區

已提交(committed)

將已暫存的文件執行Git提交操作,存入本地版本庫


Git安裝

Linux:

sudo apt-get install git


Mac:

安裝命令行工具(如已安裝Xcode,命令行工具會在首次啓動Xcode時提示安裝)

homebrew(mac上安裝軟件常用工具)

macports


Windows:

通過msyspit(http://code.google.com/p/myysgit)


安裝後建議使用Git命令行,如果要使用GUI,推薦使用SourceTree工具,有Mac和Windows版本。


第一步創建本地倉庫

切換到倉庫目錄後執行:git init


man git 查看git幫助文檔

git add 將已修改的文件放入暫存區

git commit 將暫存區中的文件提交到本地倉庫

git commit -am 'message' 將工作區中的修改直接添加到暫存區並提交到本地版本庫中

git rm 刪除版本庫中的文件

git help 查看幫助

git log 查看日誌 git log -3 查看最近3條的操作日誌

格式化,每一條操作日誌爲一行

git log --pretty=oneline

git log --pretty=format:"%h-%an,%ar:%s"


git diff 比較工作區與暫存區中的文件差別,暫存區中作爲源文件

git diff HEAD 比較工作區與當前分支最新提交的文件差別

git diff --cached 比較暫存區與最新提交文件差別

git pull 從遠程版本庫拉取到本地版本庫

git push 將本地版本庫推送到遠程版本庫

git config --local user.name "name" 設置名稱

git config --local user.email "[email protected]" 設置郵箱

git checkout -- filename  撤銷修改,將文件內容恢復到暫存區中最新狀態

git reset HEAD filename 將暫存區中的文件恢復成已修改狀態


修正上一次提交的註釋內容

git commit --amend -m 'it is a right message'


在git倉庫根目錄下創建.gitignore文件,裏面的內容被git忽略,不加入版本管理中

.gitignore

noneed.txt

*.properties

!b.properties

conf/

allconf/**/*.txt


以當前分支基礎上創建分支

git branch branchname

切換到分支

git checkout branchname

分支修改名稱

git branch -m branchname newbranchname

刪除本地分支

git -d branch mydev

刪除遠程分支方式一:將一個空分支推送到遠程dev分支即爲身刪除,:前面有一個空格

git push origin  :dev

刪除遠程分支方式二

git push origin --delete dev

如果要刪除的分支有過文件修改可以使用

git -D branch mydev

創建新的分支並切換到新分支

git checkout -b newbranch

將當前分支(newbranch)推動到遠程版本庫,並在遠程版本庫中建立newbranch分支

git push --set-upstream originnewbranch

以遠程dev分支爲基準,在本地創建dev分支與之關聯

git checkout -b dev origin/dev

將另一個分支的文件合併到當前分支

git merge newbranch

查看本地分支的上一次提交註釋

git branch -v

查看本地和遠程所有分支上一次提交註釋

git branch -av

將當前分支上的修改暫時保存

git stash save 'save message'

恢復上一個保存,並從stash列表中刪除

git stash pop

恢復某個保存,不從stash列表中刪除,不指定stash id的話默認恢復上一個保存

git stash apply [stash id]

查看stash列表

git stash list

刪除stash列表中某個記錄

git stash drop stash@{0}

查看某個文件的修改歷史

git blame filename

比較暫存區和工作區文件內容的區別,暫存區作爲源文件 --

git diff test.txt

比較最新提交的和工作區文件的區別,最新提交的作爲源文件 --

git diff HEAD test.txt

比較最新提交和暫存區文件的區別,最新提交的作爲源文件 --

git diff --cached test.txt



注:HEAD永遠指向當前的分支。

注:當分支合併時,如果當前分支的log版本低於要merge的分支時,會直接fast-forward,而不會出現版本衝突問題。

注:當文件被修改後(未提交暫存區),執行git checkout -- file 會將文件的修改丟棄,恢復爲上一個版本暫存區的內

容。如果已提交到暫存區,則需要先執行git reset HEAD file先將文件從暫存區中恢復到已修改狀態,再丟棄修改,恢

復爲上一個版本暫存區的內容。總之,git checkout -- file 修改的是工作區,將工作區的內容恢復成暫存區的版本。


使用git連接github遠程倉庫

首先創建github倉庫,再在本地建立git倉庫。

關聯本地倉庫和github倉庫

git remote add origin [email protected]:yinhaonefu/mygit.git

查看遠程分支 git remote show origin 如果顯示如下信息

$ git remote show origin
The authenticity of host 'github.com (192.30.253.113)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

因爲github遠程倉庫中沒有放置本地的公鑰,所以訪問失敗。

使用ssh-keygen生成公鑰,默認在當前用戶目錄中.ssh中。

$ /usr/bin/ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/yinhao/.ssh/id_rsa):回車
Enter passphrase (empty for no passphrase):回車
Enter same passphrase again:回車
Your identification has been saved in /c/Users/yinhao/.ssh/id_rsa.
Your public key has been saved in /c/Users/yinhao/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:wkBh5aTLuttfWhcZR4OV0rCuBeOogPoLyosPd/YDOmQ yinhao@ZB-3PYK442
The key's randomart image is:
+---[RSA 2048]----+
|    +oo   .=+.   |
|   o +    ooo.   |
|    o . o o..    |
| . . + o + +     |
|. . o + S =      |
|. Eo.. . o .     |
|+oo.+.  + .      |
|+=o= ..+ .       |
|o+B+..+.         |
+----[SHA256]-----+

然後將.ssh目錄下的id_rsa.pub中的公鑰內容設置到github中。

在github對應的倉庫點擊Settings-Deploy keys-Add deploy key將公鑰保存後再查看遠程信息

$ git remote show origin
* remote origin
  Fetch URL: [email protected]:yinhaonefu/mygit.git
  Push  URL: [email protected]:yinhaonefu/mygit.git
  HEAD branch: (unknown)

公鑰設置成功,然後可以正常推送到github遠程代碼庫中

$ git push -u origin master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 299 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To github.com:yinhaonefu/mygit.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.


從已經存在的遠程倉庫克隆到本地mygitname目錄下

git clone [email protected]:yinhaonefu/mygit.git mygitname

查看遠程信息

git remote show

查看所有分支,包括本地和遠程

git branch -a

從遠程拉取代碼

git pull = git fetch + git merge


git 別名功能

git config --global alias.br branch  使用br代替branch命令功能

git config --global alias.unstage 'reset HEAD' 也可以給組合命令設置別名

這些映射關係,因爲是global作用域,會保存在當前用戶home目錄下的.gitconfig文件中

$ cat ~/.gitconfig
[alias]
        br = branch
        unstage = reset HEAD





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