git常用命令及技巧

強推,即利用強覆蓋方式用你本地的代碼替代git倉庫內的內容

git push -f



git fetch --prune  #這樣就可在本地刪除在遠程不存在的branch

man git-fetch

  --prune

      After fetching, remove any remote tracking branches which no longer exist on the remote.

  -t, --tags

      Most of the tags are fetched automatically as branch heads are downloaded, but tags that do not

      point at objects reachable from the branch heads that are being tracked will not be fetched by

      this mechanism. This flag lets all tags and their associated objects be downloaded.

#另外,關於git branch的幾個命令

git branch     # 查詢本地存在的branch

git branch -r  # 查詢遠程的branch

git branch -a  # 查詢本地和遠程branch

git branch -d -r origin/todo  #刪除遠程的todo branch



git tag -l | xargs git tag -d    #delete local tag(s)

git fetch vgt --prune   #fetch from remote repo

#查詢遠程heads和tags的命令如下:

git ls-remote --heads origin

git ls-remote --tags origin

git ls-remote origin





git commit:

git commit --amend 撤銷上一次提交


git push:

git push [remote-name] [master] 推送數據到遠程倉庫 

git push origin :[branch-name] 刪除遠程分支.


git diff:

git diff --name-only 73a79c 2d49d2 查看兩個版本中間改動過的文件列表

git diff : workspace and index file.

git diff HEAD: workspace and commint

git diff --cached: index file and commit


git fetch:

git fetch [remote-name] 取回遠程倉庫的所有提交信息


git clone:

git clone [url] 獲取遠程倉庫的 master 分支


git log:

git log -1 HEAD 顯示最後一次提交信息

git log -p 顯示每次提交的內容差異, 可加參數 -num 顯示最近num次提交差異

git log --stat 僅顯示增改行數統計.

git reflog 查看rest, checkout 等操作紀錄. git log --pretty=oneline 用一行顯示每次提交信息.

git log --pretty=fuller 額外顯示提交日期.

git log --since=2.weeks 顯示最近兩週修改

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


選項 說明

%H 提交對象(commit)的完整哈希字串

%h 提交對象的簡短哈希字串

%T 樹對象(tree)的完整哈希字串

%t 樹對象的簡短哈希字串

%P 父對象(parent)的完整哈希字串

%p 父對象的簡短哈希字串

%an 作者(author)的名字

%ae 作者的電子郵件地址

%ad 作者修訂日期(可以用 -date= 選項定製格式)

%ar 作者修訂日期,按多久以前的方式顯示

%cn 提交者(committer)的名字

%ce 提交者的電子郵件地址

%cd 提交日期

%cr 提交日期,按多久以前的方式顯示

%s 提交說明


git checkout:

git checkout -- <file> 撤銷對文件file的修改

git checkout -b branch-name 創建並切換到分區.


git branch:

git breanch -d branch-name 刪除分支.

git branch -D branch-name 強制刪除分支.

git merge master 合併主分支到當前分支.


git remote:

git remote 列出當前項目的遠程庫 

git remote -v 顯示當前項目對應的克隆地址 

git remote add [shortname] [url] 添加遠程倉庫 

git remote show [remote-name] 查看遠程倉庫的詳細信息 

git remote [remote-old-name] [remote-new-name] 修改遠程倉庫名稱

git remote rm [remote-name] 刪除遠程倉庫


git reset:

git reset HEAD <file> 撤銷已經被暫存(git add)的文件


git reset --soft: 撤銷並回退 commit, 不影響 index file, 撤銷到哪個位置由最後一個參數指定. git reset --soft HEAD^ 

git reset --hard: 撤銷 commit, index file and workspace

git reset --mixed: 默認選項, 撤銷 commit and index file, 只保留workspace. 

git reset --: 刪除登記在 index file 裏的某個文件.


git show-branch:

+(加號)表示所在分支包含此行所標識的commit

(空格)表示所在分支不包含此行所標識的commit

-(減號)表示所在分支是經過merge得到的,而所在行的內容即是merge的基本信息。

*(星號)表示如果需要在某列標識+(加號),且此列爲當前分支所在列,那麼則將+(加號)轉變爲*(星號)。


git:

git gc 收縮空間

git count-objects -v 查看佔用空間大小.


git恢復刪除文件

git 創建分支恢復文件步驟:


git branch [recover-branch]

git checkout [recover-branch]

git checkout master

git branch -D recover-branch


要查看刪除的文件: git ls-files –deleted

恢復則需要從新checkout: git checkout – <deleted_file>

多個文件同時操作可以使用xargs

git ls-fies -d | xargs git checkout --


git checkout -f 恢復刪除文件


忽略提交某些文件或文件夾

1. 在 .gitignore 文件內寫入文件名或目錄名即可忽略提交, 但只對只對沒有被 track 的文件或目錄有效, 對於已經加入版本管理的文件是無效的.


2. 已經加入版本管理的文件或目錄可使用如下命令忽略:


git update-index --assume-unchanged PATH


常見問題

1. 執行 git remote add origin... 時出現錯誤: fatal: remote origin already exists.

執行如下指令後再git remote add origin... :


git remote rm origin




git stash : Git還提供了一個stash功能,可以把當前工作現場“儲藏”起來,等以後恢復現場後繼續工作

git stash list


工作現場還在,Git把stash內容存在某個地方了,但是需要恢復一下,有兩個辦法:

一是用git stash apply恢復,但是恢復後,stash內容並不刪除,你需要用git stash drop來刪除;

另一種方式是用git stash pop,恢復的同時把stash內容也刪了




Git常用操作命令:

1) 遠程倉庫相關命令

檢出倉庫:$ git clone git://github.com/jquery/jquery.git

查看遠程倉庫:$ git remote -v

添加遠程倉庫:$ git remote add [name] [url]

刪除遠程倉庫:$ git remote rm [name]

修改遠程倉庫:$ git remote set-url --push [name] [newUrl]

拉取遠程倉庫:$ git pull [remoteName] [localBranchName]

推送遠程倉庫:$ git push [remoteName] [localBranchName]

 

*如果想把本地的某個分支test提交到遠程倉庫,並作爲遠程倉庫的master分支,或者作爲另外一個名叫test的分支,如下:

$git push origin test:master         // 提交本地test分支作爲遠程的master分支

$git push origin test:test              // 提交本地test分支作爲遠程的test分支

 

2)分支(branch)操作相關命令

查看本地分支:$ git branch

查看遠程分支:$ git branch -r

創建本地分支:$ git branch [name] ----注意新分支創建後不會自動切換爲當前分支

切換分支:$ git checkout [name]

創建新分支並立即切換到新分支:$ git checkout -b [name]

刪除分支:$ git branch -d [name] ---- -d選項只能刪除已經參與了合併的分支,對於未有合併的分支是無法刪除的。如果想強制刪除一個分支,可以使用-D選項

合併分支:$ git merge [name] ----將名稱爲[name]的分支與當前分支合併

創建遠程分支(本地分支push到遠程):$ git push origin [name]

刪除遠程分支:$ git push origin :heads/[name] 或 $ git push origin :[name] 

 

*創建空的分支:(執行命令之前記得先提交你當前分支的修改,否則會被強制刪乾淨沒得後悔)

$git symbolic-ref HEAD refs/heads/[name]

$rm .git/index

$git clean -fdx

 

3)版本(tag)操作相關命令

查看版本:$ git tag

創建版本:$ git tag [name]

刪除版本:$ git tag -d [name]

查看遠程版本:$ git tag -r

創建遠程版本(本地版本push到遠程):$ git push origin [name]

刪除遠程版本:$ git push origin :refs/tags/[name]

合併遠程倉庫的tag到本地:$ git pull origin --tags

上傳本地tag到遠程倉庫:$ git push origin --tags

創建帶註釋的tag:$ git tag -a [name] -m 'yourMessage'

 

4) 子模塊(submodule)相關操作命令

添加子模塊:$ git submodule add [url] [path]

   如:$git submodule add git://github.com/soberh/ui-libs.git src/main/webapp/ui-libs

初始化子模塊:$ git submodule init  ----只在首次檢出倉庫時運行一次就行

更新子模塊:$ git submodule update ----每次更新或切換分支後都需要運行一下

刪除子模塊:(分4步走哦)

 1) $ git rm --cached [path]

 2) 編輯“.gitmodules”文件,將子模塊的相關配置節點刪除掉

 3) 編輯“ .git/config”文件,將子模塊的相關配置節點刪除掉

 4) 手動刪除子模塊殘留的目錄

 

5)忽略一些文件、文件夾不提交

在倉庫根目錄下創建名稱爲“.gitignore”的文件,寫入不需要的文件夾名或文件,每個元素佔一行即可,如

target

bin

*.db

 

=====================

Git 常用命令

git branch 查看本地所有分支

git status 查看當前狀態 

git commit 提交 

git branch -a 查看所有的分支

git branch -r 查看本地所有分支

git commit -am "init" 提交併且加註釋 

git remote add origin [email protected]:ndshow

git push origin master 將文件給推到服務器上 

git remote show origin 顯示遠程庫origin裏的資源 

git push origin master:develop

git push origin master:hb-dev 將本地庫與服務器上的庫進行關聯 

git checkout --track origin/dev 切換到遠程dev分支

git branch -D master develop 刪除本地庫develop

git checkout -b dev 建立一個新的本地分支dev

git merge origin/dev 將分支dev與當前分支進行合併

git checkout dev 切換到本地dev分支

git remote show 查看遠程庫

git add .

git rm 文件名(包括路徑) 從git中刪除指定文件

git clone git://github.com/schacon/grit.git 從服務器上將代碼給拉下來

git config --list 看所有用戶

git ls-files 看已經被提交的

git rm [file name] 刪除一個文件

git commit -a 提交當前repos的所有的改變

git add [file name] 添加一個文件到git index

git commit -v 當你用-v參數的時候可以看commit的差異

git commit -m "This is the message describing the commit" 添加commit信息

git commit -a -a是代表add,把所有的change加到git index裏然後再commit

git commit -a -v 一般提交命令

git log 看你commit的日誌

git diff 查看尚未暫存的更新

git rm a.a 移除文件(從暫存區和工作區中刪除)

git rm --cached a.a 移除文件(只從暫存區中刪除)

git commit -m "remove" 移除文件(從Git中刪除)

git rm -f a.a 強行移除修改後文件(從暫存區和工作區中刪除)

git diff --cached 或 $ git diff --staged 查看尚未提交的更新

git stash push 將文件給push到一個臨時空間中

git stash pop 將文件從臨時空間pop下來

git stash list

---------------------------------------------------------

git remote add origin [email protected]:username/Hello-World.git

git push origin master 將本地項目給提交到服務器中

-----------------------------------------------------------

git pull 本地與服務器端同步

-----------------------------------------------------------------

git push (遠程倉庫名) (分支名) 將本地分支推送到服務器上去。

git push origin serverfix:awesomebranch

------------------------------------------------------------------

git fetch 相當於是從遠程獲取最新版本到本地,不會自動merge

git commit -a -m "log_message" (-a是提交所有改動,-m是加入log信息) 本地修改同步至服務器端 :

git branch branch_0.1 master 從主分支master創建branch_0.1分支

git branch -m branch_0.1 branch_1.0 將branch_0.1重命名爲branch_1.0

git checkout branch_1.0/master 切換到branch_1.0/master分支

du -hs




不要用git pull,用git fetch和git merge代替它



git rebase用於把一個分支的修改合併到當前分支


在rebase的過程中,也許會出現衝突(conflict). 在這種情況,Git會停止rebase並會讓你去解決 衝突;在解決完衝突後,用"git-add"命令去更新這些內容的索引(index), 然後,你無需執行 git-commit,只要執行:

$ git rebase --continue

這樣git會繼續應用(apply)餘下的補丁。

在任何時候,你可以用--abort參數來終止rebase的行動,並且"mywork" 分支會回到rebase開始前的狀態。

$ git rebase --abort


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