git 常用命令

獲得版本庫(kù)

  • git init

  • git clone

    ?

查看信息

  • git help

  • Git log

    ? 查看提交日志

  • git diff

    ? 比較文件在不同狀態(tài)中的文件區(qū)別

版本管理

  • Git add

    ? 加入到版本暫存區(qū)中

    ? . 表示當(dāng)前文件夾都納入到暫存區(qū)

  • git commit

    ? 將暫存區(qū)的文件提交到git版本庫(kù)中

  • git rm

    ? git rm 版本庫(kù)中的文件

遠(yuǎn)程協(xié)作

  • git pull

    ? 將遠(yuǎn)程版本庫(kù)中的文件拉取到本地

  • git push

    ? 將本地版本庫(kù)中的文件推送到遠(yuǎn)程庫(kù)

Git配置

git 查看版本

  • git —version

首次使用前的配置

對(duì)于user.name與user.email來(lái)說(shuō),有3個(gè)地方可以設(shè)置

  • 1,/etc/gitconfig (幾乎不用) 對(duì)于操作系統(tǒng)的 git config —system

  • 2,~/.gitconfig (很常用) 對(duì)于所有的git項(xiàng)目 git config —global

  • 3,針對(duì)于特定的項(xiàng)目的 .git/config文件中 對(duì)于特定的項(xiàng)目 git config —local

     git config --local user.name 'lihao'
     git config --local user.email 'test@qq.com'
     git config user.name
     git config user.email
     
     //重置
      git config --local user.name 'lisan'
      
      //刪除
      git config --local --unset user.name
      
      
    

    ?

git的提交更新

提交更新,每次準(zhǔn)備提交前,先用 git status 看下,是不是都已暫存起來(lái)了,然后在運(yùn)行提交命令。

  • Git commit
  • git commit -m 'init'
  • Git commit —amend -m 'init' 修改最后一次提交的message
  • git commit -am 'init' 添加到索引庫(kù)并提交 不用寫(xiě)add了

git 刪除文件

  • 刪除文件
  • Git rm readme

以上兩者區(qū)別

Git rm :

  • 刪除了一個(gè)文件
  • 將被刪除的文件納入到暫存區(qū)(stage,index)
  • git rm --cached -r .idea 從暫存區(qū)刪除

如果恢復(fù)文件

git reset HEAD index.php 由暫存區(qū)恢復(fù)到工作區(qū)

git checkout — index.php 丟棄工作區(qū)刪除文件的內(nèi)容

rm:

? 將文件刪除了,這時(shí),被刪除的文件并未納入緩存區(qū)當(dāng)中.

如果恢復(fù)文件:

git checkout — index.php

git 重命名

git mv from_file to_file

Git 查看提交歷史

  • git log
  • -p 展開(kāi)顯示每次提交的內(nèi)容差異
  • -n 僅顯示最近的n次更新
  • —stat 僅顯示簡(jiǎn)要的增改行數(shù)統(tǒng)計(jì)
  • —pretty=oneline
  • —pretty=format:'%h - %an,%ar:%s'
  • —graph 圖形化的方式查看log

忽略文件 .gitignore

  • 新建 .gitignore 文件
  • *.a 忽略所有 .a結(jié)尾的文件
  • !lib.a 但 lib.a除外
  • /todo 僅僅忽略項(xiàng)目根目錄下的todo文件不包括subdir/todo
  • Build/ 忽略build/目錄下的所有文件
  • doc/*.txt 會(huì)忽略doc/notes.txt 但不包括 doc/server/arch.txt
  • ** 表示所有的目錄

如果.gitignore嘗試以下方法

.gitignore只能忽略哪些原來(lái)沒(méi)有被track的文件,如果某些文件已經(jīng)被納入了版本管理中,
則修改.gitignore是無(wú)效的,那么解決方法就是先把本地緩存刪除(改變成未track狀態(tài)),然后再提交:

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

分支

列出當(dāng)前所有分支

  • git branch

創(chuàng)建分支

  • git branch branch_name

切換分支

  • git checkout branch_name

刪除分支

  • git branch -d branch_name

創(chuàng)建并轉(zhuǎn)向所創(chuàng)建的分支

  • git checkout -b branch_name

修改分支的名稱

  • git branch -m branch_name new_branch_name

fast-forward

  • 如果可能,合并分支時(shí)git 會(huì)使用fast-forward 模式
  • 在這種模式下,刪除分支時(shí)會(huì)丟掉分支信息。
  • 合并時(shí)加上 —no-ff 參數(shù)會(huì)禁用fast-forword, 這樣會(huì)多出一條commit id
  • git merge —no-ff dev

合并分支

  • Git merge branch_name

git 回退版本

  • 回退到上一個(gè)版本

    git reset -- hard HEAD^  //回退到上一個(gè)版本
    git reset -- hard HEAD^^ //回退到上兩個(gè)版本
    git reset -- hard HEAD~2 //回退到指定第幾個(gè)版本
    git reset -- hard commit_id //回退到指定的版本
    

    ?

  • 查看操作日志

    git reflog 
    

checkout

  • Git checkout — test.txt

    作用是:丟棄掉相對(duì)于暫存區(qū)中最后一次添加的文件內(nèi)容所做的變更

  • Git reset HEAD test.txt

    作用是:將之前添加到暫存區(qū)(stage,index)的內(nèi)容從暫存區(qū)移除到工作區(qū)。

保存工作現(xiàn)場(chǎng)

保存現(xiàn)場(chǎng)

  • git stash
  • git stash sava 'init'
  • git stash list
  • git stash —help

恢復(fù)現(xiàn)場(chǎng)

  • Git stash apply (stash 內(nèi)容并不刪除,需要通過(guò)git stash drop stash@{0} 手動(dòng)刪除)
  • git stash pop (恢復(fù)的同時(shí)也將stash 內(nèi)容刪除)
  • Git stash apply stash@{0}

git 標(biāo)簽

  • 新建標(biāo)簽,標(biāo)簽有兩種:輕量級(jí)標(biāo)簽(lightweight)與帶有附注標(biāo)簽(annotated)

  • 創(chuàng)建一個(gè)輕量級(jí)標(biāo)簽

    git tag v1.0.1
    
  • 創(chuàng)建一個(gè)帶有附注的標(biāo)簽

    git tag -a v1.0.2 -m 'release verion'
    

    ?

  • 刪除標(biāo)簽

    git tag -d tag_name
    
  • 搜索標(biāo)簽

    git tag -l 'v*'
    
  • 查看標(biāo)簽

    git show v1.0.0 
    

    ?

  • 將標(biāo)簽推送到遠(yuǎn)程

    git push origin v1.0.0  //推送指定的標(biāo)簽
    
    git push origin --tags  // 推送所有的標(biāo)簽
    
    
  • 刪除遠(yuǎn)程標(biāo)簽

    git push origin --delete tag v1.0.1
    

    ?

    ?

    ?

查看一個(gè)文件都是誰(shuí)提交的

git blame  index.php

git diff 比較

  • git diff : 比較的是暫存區(qū)與工作區(qū)文件之間的差別,暫存區(qū)表示是原始文件。
  • git diff HEAD: 比較的是最新的提交與工作區(qū)之間的差別。
  • git diff —cached: 比較的是版本庫(kù)與暫存區(qū)之間的差別。

遠(yuǎn)程

查看遠(yuǎn)程倉(cāng)庫(kù)

  • Git remote show 顯示遠(yuǎn)程倉(cāng)庫(kù)的別名。

  • Git remote show origin 遠(yuǎn)程倉(cāng)庫(kù)詳細(xì)信息

    git remote show origin
    
    * remote origin
      Fetch URL: https://github.com/haojuwang/git.git
      Push  URL: https://github.com/haojuwang/git.git
      HEAD branch: master
      Remote branch:
        master tracked
      Local branch configured for 'git pull':
        master merges with remote master
      Local ref configured for 'git push':
        master pushes to master (up to date)
    

    ?

添加到遠(yuǎn)程倉(cāng)庫(kù)

  • Git remote add origin 遠(yuǎn)程地址
  • git push -u origin master

查看遠(yuǎn)程倉(cāng)庫(kù)

  • git branch -a 查看遠(yuǎn)程分支

  • git branch -av 查看遠(yuǎn)程分支并顯示最后一次提交的信息

  • git remote show origin 查看遠(yuǎn)程分支與本地的對(duì)比

    ?

clone

  • git clone git@github.com:haojuwang/git.git git2 下載到指定的git2 文件夾

創(chuàng)建遠(yuǎn)程分支

  • git push —set-upstream origin 遠(yuǎn)程名稱

  • git push —set-upstream origin src:dest (src 本地 dest 遠(yuǎn)程)

    ?

刪除遠(yuǎn)程分支

  • git push origin :dev(遠(yuǎn)程分支名稱)

  • git push origin --delete dev

    ?

修改遠(yuǎn)程分支名稱

git remote rename origin origin2

遠(yuǎn)程分支刪除了本地同步刪除

git remote prune origin

推送到遠(yuǎn)程

  • Push 完整命令: git push origin srcBranch:destBranch (srcBranch 本地 destBranch遠(yuǎn)程)
  • pull操作的完整命令是: git pull origin srcBranch:destBranch (srcBranch 遠(yuǎn)程 destBranch本地)
  • HEAD 標(biāo)記: HEAD 文件是一個(gè)指向你當(dāng)前所在分支的引用標(biāo)識(shí)符,該文件內(nèi)部并不包含SHA-1值,而是一個(gè)指向另外一個(gè)引用的指針。
  • 當(dāng)執(zhí)行g(shù)it commit 命令時(shí),Git會(huì)創(chuàng)建一個(gè)commit對(duì)象,并且將這個(gè)commit對(duì)象的parent指針設(shè)置為head所指向的引用的SHA-1值。
  • 我們對(duì)于HEAD修改的任何操作,都會(huì)被git reflog 完整記錄下來(lái)
  • 實(shí)際上,我們可以通過(guò)git底層命令symbolic-ref來(lái)實(shí)現(xiàn)對(duì)HEAD文件內(nèi)容的修改

基于遠(yuǎn)程分支創(chuàng)建分支

  • git checkout -b develop origin/develop
  • git checkout --track origin/test

git別名

  • git config --global alias.br branch br 表示 branch 的簡(jiǎn)寫(xiě)

  • ~/.gitconfig 別名文件

    cat ~/.gitconfig
    
    [alias]
      br = branch
      co = commit
    

    ?

查看遠(yuǎn)程分支提交log

git log origin/master

git log remotes/origin/master

git log refs/remotes/origin/master

git裸庫(kù)

  • Git裸庫(kù)是指沒(méi)有工作區(qū)的git 項(xiàng)目,常用于服務(wù)器端
  • 創(chuàng)建一個(gè)裸庫(kù) gi init —bare

git submodule

git submodule add git@10.211.55.3:java/git_child.git submodule

子倉(cāng)庫(kù)的地址:git@10.211.55.3:java/git_child.git
submodule 子倉(cāng)庫(kù)放置的目錄

更新submodule

  • 直接進(jìn)入submodule 的目錄 執(zhí)行g(shù)it pull
  • 更新所有的submodule git submodule foreach git pull

submodule git clone 發(fā)生了變化

git clone git@10.211.55.3:java/git_parent.git git_parent2

git submodule init

git submodule update --recursive

--------------
git clone git@10.211.55.3:java/git_parent.git git_parent2 --recursive

刪除submodule

git rm --cached mymodule

rm rf mymodule

git add .

git commit -am 'update'

rm .gitmodules

git push

git subtree

添加 subtree

git remote add subtree-origin git@10.211.55.3:java/git_subtree_child.git

git subtree add --prefix=subtree subtree-origin master --squash


git push

subtree: 表示目錄
subtree-origin: 遠(yuǎn)程版本庫(kù)
master: 表示遠(yuǎn)程版本庫(kù)的分支

parent subtree pull

git subtree pull --prefix=subtree subtree-origin master --squash

parent Subtee push

git push

git subtree push --prefix=subtree subtree-origin master 

git cherry-pick

將一個(gè)分支的修改應(yīng)用到另外一個(gè)分支上

現(xiàn)在是在dev 分支上修改,我想把修改放在master上

master:  git cherry-pick c7d3592

c7d3592 commit sha-1 這個(gè)id 是dev上面的

原來(lái)的分支修改刪除
dev:  git checkout  c7d3592 //切換到 c7d3592分支

c7d3592 :    git branch -D dev  刪除分支

c7d3592 :  git branch -b dev 創(chuàng)建分支

Git rebase

  • Rebase : 變基,意即改變分支的根基
  • 從某種程度上來(lái)說(shuō),rebase與merge可以完成類(lèi)似的工作,不過(guò)二者的工作方式有著明顯的差異

生成公鑰私鑰

  • ssh-keygen

    ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/lixueqin/.ssh/id_rsa):
    

    ?

  • id_rsa 私鑰 不能給別人

  • id_rsa.pub 公鑰

gretty

http://akhikhl.github.io/gretty-doc/Gretty-configuration.html

repositories {
    maven {
        name 'aliyun'
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
    mavenCentral()
}

//gretty
buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'org.akhikhl.gretty:gretty:2.0.0'
    }
}

if (!project.plugins.findPlugin(org.akhikhl.gretty.GrettyPlugin))
    project.apply(plugin: org.akhikhl.gretty.GrettyPlugin)

gretty{
    httpEnabled = true
    servletContainer = 'tomcat8'
    contextPath = '/mygit'
    httpPort = 8081
    host = "localhost"
    scanInterval = 1
    fastReload =true
    inplaceMode = 'soft'
    loggingLevel = "DEBUG"
    consoleLogEnabled = true
    
    debugPort = 6005
    debugSuspend = true

}





運(yùn)行
gradle appRun

debug

gradle appRunDebug

debug-2.png

遠(yuǎn)程debug

https://dancon.gitbooks.io/intellij-idea/content/remote-debugging.html

gitlab安裝

  • 安裝地址 https://about.gitlab.com/installation/#centos-7

  • 配置 vim /etc/gitlab/gitlab.rb

     external_url 'http://Vultr'
     external_url 'http://108.61.126.40/
     
    
  • 載入配置服務(wù)(初始化和修改/etc/gitlab/gitlab.rb 后需要重新載入)

    gitlab-ctl reconfigure
    
  • 啟動(dòng)服務(wù)

     gitlab-ctl start
    
  • 停止服務(wù)

    gitlab-ctl stop
    
  • 重啟服務(wù)

    gitlab-ctl restart
    
  • 查看日志

gitlab日志:/var/log/gitlab

查看gitlab日志:gitlab-ctl tail
查看gitlab對(duì)應(yīng)的Nginx訪問(wèn)日志:gitlab-ctl tail nginx/gitlab_access.log
查看gitlab對(duì)應(yīng)的數(shù)據(jù)庫(kù)postgre-sql的日志:gitlab-ctl tail postgresql

gitlab數(shù)據(jù)存放目錄:/var/opt/gitlab/git-data

  • 下載失敗

    https://packages.gitlab.com/gitlab/gitlab-ce
    
    rpm -i gitlab-ce-9.4.6-ce.0.el7.x86_64.rpm
    

    ?


    image.png

不讓用戶注冊(cè)

http://10.211.55.3/admin/application_settings

602C3386-31C1-4476-8E0E-E8D91D50381C.png

git 倉(cāng)庫(kù)

git init --bare 倉(cāng)庫(kù)名稱.git
///data/git/cangku.git/
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容