git常用筆記(二)

 

git常用命令

創建版本庫

什麼是版本庫呢?版本庫又名倉庫,英文名repository,你可以簡單理解成一個目錄,這個目錄裏面的所有文件都可以被Git管理起來,每個文件的修改、刪除,Git都能跟蹤,以便任何時刻都可以追蹤歷史,或者在將來某個時刻可以“還原”。gitTest文件夾下面會出現一個.git隱藏文件

git init

192:gitblit liqiang$ cd gitTest
192:gitTest liqiang$ git init
Initialized empty Git repository in /Users/liqiang/Desktop/gitblit/gitTest/.git/
192:gitTest liqiang$ 

在指定目錄執行git init 命令 該目錄下的所有目錄及文件都受git管理

關聯版本庫

liqiangdeMacBook-Pro:canal-canal-1.1.4 liqiang$ git remote add origin http://git.tuna.1919.cn/kh/soon/canal-canal-1.1.4.git
liqiangdeMacBook-Pro:canal-canal-1.1.4 liqiang$ git push -u origin master #推送最新代碼到遠程倉庫

 

查看工作區修改內容

git status

192:gitTest liqiang$ mkdir files
192:gitTest liqiang$ vi hello.txt
192:gitTest liqiang$ git status

 

創建了一個files目錄 並在files目錄下增加了一個hello.txt文件通過git status可以看到還沒有add

忽略提交文件

我們使用idea開發,會有很多target和maven的一些文件這些文件是不需要提交的所以在根目錄創建.gitignore

#忽略所有.svn目錄
.svn/
#忽略所有target目錄
target/
#忽略所有.idea目錄
.idea/
#忽略所有.iml文件
*.iml

 

 

添加和提交代碼

git add git commit

這2個是組合命令 先add將文件提交到暫存區  再commit 提交到分支  git默認會創建一個master分支

192:gitTest liqiang$ git add files
192:gitTest liqiang$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   files/hello.txt

執行status後再查看狀態 顯示還沒有提交   git add . 可以提交所有

192:gitTest liqiang$ git commit files -m '創建了一個hello文件'
[master (root-commit) 224f10f] 創建了一個hello文件
 1 file changed, 1 insertion(+)
 create mode 100644 files/hello.txt
192:gitTest liqiang$ git status
On branch master
nothing to commit, working tree clean
192:gitTest liqiang$ 

執行commit就成功提交到本地倉庫 或者可以到指定目錄執行git add . (空格+.) 提交目錄下面所有文件

查看提交詳情

查看提交的詳情: git log -

查看提交記錄

git log

192:gitTest liqiang$ git log
commit 224f10fd409bc7f8f83167167c42d05e36a5bb24 (HEAD -> master)
Author: liqiang <www.liqiand@qq.com>
Date:   Fri Jan 4 13:50:16 2019 +0800

    創建了一個hello文件

可以看到剛纔的提交記錄

查看某個分支日誌

 git log $分支名/tag名/遠程分

查看所有操作過的命令日誌

可以除了提交以爲操作分支分支相關 等命令都在

git reflog 
liqiangdeMacBook-Pro:ewei-helpdesk liqiang$ git reflog
7bd66c4bb (HEAD -> develop_liqiang, origin/develop) HEAD@{0}: checkout: moving from feature_liqiang_7.9.51.2 to develop_liqiang
2b79d6878 (origin/feature_liqiang_7.9.51.2, feature_liqiang_7.9.51.2) HEAD@{1}: commit: fix(7.9.51.2):修復未返provider_id導致權限校驗異常問題
f9d2e176b (tag: 7.9.51.2) HEAD@{2}: checkout: moving from develop_liqiang to feature_liqiang_7.9.51.2
7bd66c4bb (HEAD -> develop_liqiang, origin/develop) HEAD@{3}: checkout: moving from feature_liqiang_7.9.51.2 to develop_liqiang
f9d2e176b (tag: 7.9.51.2) HEAD@{4}: checkout: moving from f9d2e176b1ed089adcbd236c0ba45bd2a6694d48 to feature_liqiang_7.9.51.2
f9d2e176b (tag: 7.9.51.2) HEAD@{5}: checkout: moving from develop_7.9.51 to 7.9.51.2
4409aad29 (develop_7.9.51) HEAD@{6}: checkout: moving from feature_liqiang_7.9.51.2 to develop_7.9.51
f9d2e176b (tag: 7.9.51.2) HEAD@{7}: checkout: moving from develop_liqiang to feature_liqiang_7.9.51.2
7bd66c4bb (HEAD -> develop_liqiang, origin/develop) HEAD@{8}: checkout: moving from develop_7.9.51 to develop_liqiang
4409aad29 (develop_7.9.51) HEAD@{9}: checkout: moving from feature_liqiang_7.9.51.2 to develop_7.9.51

 

 

版本回退 

版本回退非常快,僅僅是把指針移動到指定版本,回退後如果想撤銷到回退之前的分支,需要把之前的版本id記下來 否則再次git log就看不到了

僞造測試數據 對文件進行2此修改並提交 查看日誌

192:gitTest liqiang$ git log
commit 3708002e7618ca88f349cdec3517b0b1f9611413 (HEAD -> master)
Author: liqiang <www.liqiand@qq.com>
Date:   Fri Jan 4 13:58:24 2019 +0800

    文件內容修改爲hell3

commit 209a176fa3413ac5f3528c2da1ab7126802160e3
Author: liqiang <www.liqiand@qq.com>
Date:   Fri Jan 4 13:57:08 2019 +0800

    文件內容修改爲hell2

commit 224f10fd409bc7f8f83167167c42d05e36a5bb24
Author: liqiang <www.liqiand@qq.com>
Date:   Fri Jan 4 13:50:16 2019 +0800

    創建了一個hello文件

 

或者加上--pretty=oneline 使日誌一行顯示

192:gitTest liqiang$ git log --pretty=oneline
3708002e7618ca88f349cdec3517b0b1f9611413 (HEAD -> master) 文件內容修改爲hell3
209a176fa3413ac5f3528c2da1ab7126802160e3 文件內容修改爲hell2
224f10fd409bc7f8f83167167c42d05e36a5bb24 創建了一個hello文件
192:gitTest liqiang$ 

 

 回退到上一個版本

192:gitTest liqiang$ git reset --hard HEAD^
HEAD is now at 209a176 文件內容修改爲hell2

 

打開文件可以看到變成了hello2

如果要回退到指定版本呢只需要hard參數加上要回退的版本號

$  git reset --hard 3708002e7618ca88f349cdec3517b0b1f9611413
HEAD is now at 3708002 文件內容修改爲hell3

 git每次提交 都會爲每次提交保留一個版本  版本回退只是將指針指向回退的版本

當版本回退 head 重新指向了 hello2

強制合併到遠程同名分支 讓遠程分支代碼跟本地一樣

git push origin HEAD --force

撤銷add 

192:files liqiang$ git add hello.txt
192:files liqiang$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   hello.txt

通過將文件修改通過git add到暫存區 如何撤銷提交(我工作中經常遇到這樣的需求)

192:files liqiang$ git reset HEAD hello.txt
Unstaged changes after reset:
M    files/hello.txt
192:files liqiang$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   hello.txt

git reset HEAD 不加文件信息 則是 撤銷所有暫存區

git reset --soft HEAD^

撤銷本地修改 

git checkout -- filepathname

192:files liqiang$ git checkout -- hello.txt 

刪除文件/目錄

git rm filename   git rm -r directoryName

92:gitTest liqiang$ git rm hello.txt
92:gitTest liqiang$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    files/hello.txt

如果需要撤銷與上面撤銷修改一致

撤銷Untracked files

針對pull代碼報untracked files

# 刪除 untracked files
git clean -f
 
# 連 untracked 的目錄也一起刪掉
git clean -fd
 
# 連 gitignore 的untrack 文件/目錄也一起刪掉 (慎用,一般這個是用來刪掉編譯出來的 .o之類的文件用的)
git clean -xfd
 
# 在用上述 git clean 前,牆裂建議加上 -n 參數來先看看會刪掉哪些文件,防止重要文件被誤刪
git clean -nxfd
git clean -nf
git clean -nfd

 

添加遠程倉庫 

$ git remote add [遠程倉庫名字(一般叫origin)] [遠程倉庫地址] $ git push -u origin master

$ git remote add  本地倉庫與遠程倉庫關聯

$ git push -u origin master  推送到遠程倉庫

可以使用github 我這裏是本地搭建了一個git服務器 gitblit

 

192:gitTest liqiang$ git remote add origin http://[email protected]:1234/r/gitTest.git
192:gitTest liqiang$ git push -u origin master
Password for 'http://[email protected]:1234': 
To http://127.0.0.1:1234/r/gitTest.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'http://[email protected]:1234/r/gitTest.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
192:gitTest liqiang$ failed to push some refs to

 報錯是因爲我上面創建版本庫 勾上了添加README文件 origin不是一個獨立的版本庫了。。如果使用clone 將沒有問題 

這個時候需要執行

$ git pull origin master --allow-unrelated-histories

合併origin和本地版本庫

$ git commit -m '合併'

最後推送到遠程origin

$ git push -u origin master

PUll回退

1、git reflog 

2、git reset --hard   <COMMIT_ID> 或者 git reset --hard HEAD@{2}

clone遠程分支

  git clone

$ git clone [git地址]

 

強制推送

git push --force

git push -f

git分支管理

創建並切換分支

git checkout -b feature/6.5.5.5_liqiang origin/feature/6.5.5.5

 切換分支

192:gitTest liqiang$ git checkout -b dev
Switched to a new branch 'dev'
192:gitTest liqiang$ git branch
* dev
  master
192:gitTest liqiang$ 

git branch爲查看當前分支  上面當前指向dev分支

當我們再dev上面做任何修改都不會影響其他分支

切換分支

192:gitTest liqiang$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
192:gitTest liqiang$ git branch
  dev
* master
192:gitTest liqiang$ 

解決衝突 

dev分支將hello.txt改爲並推送到遠程倉庫

hello2
hhhhhh
~      

再切換到master將hell.txt改爲並推送到遠程倉庫

hello5
llllll

執行$ git merge [分支名字]將指定分支合併到當前分支

192:files liqiang$ git branch
  dev
* master
192:files liqiang$ 

當前分支是master  執行git merge [分支名字] 就是講指定分支合併到當前分支

192:files liqiang$ git merge dev
Auto-merging files/hello.txt
CONFLICT (content): Merge conflict in files/hello.txt
Automatic merge failed; fix conflicts and then commit the result.

存在衝突無法自動合併  git告訴我們hello.txt衝突了 需要我們手動合併

192:files liqiang$ git status
On branch master
Your branch is up to date with 'origin/master'.

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

git status會告訴我們衝突的文件

查看文件  衝突的都用====標記起來

<<<<<<< HEAD
hello5
llllll
=======
hello2
hhhhhh
>>>>>>> dev
~                

Head爲 當前分支  dev爲dev內容 手動修改 選擇要保留的部分 

hello5
hhhhhh

重新add  commit push即可

Fast forward模式

當我們使用git merge dev進行合併時會將master直接將指針指向 dev 實現快速合併

使用--no-ff -m "merge with no-ff" 參數時會禁用Fast forward模式 會重新生成一個新的提交記錄 並將master指向他

$ git merge --no-ff -m "merge with no-ff" dev

如:

 

 

 

 執行命令創建一個dev分支

$ git checkout -b dev

上面會創建並切換分支 所以HEAD指向dev

我們再dev分支進行修改提交操作

192:files liqiang$ git commit -m '修改hello文件'
[dev 2ade7d5] 修改hello文件

Fast forward合併分支

直接快速切換將maste指向合併時dev的版本記錄線

Fast forward合併

會多一次commit並將master指針指向他

bug分支

工作中用得比較多  常常會遇到 自己本地改動了大量代碼 線上有bug需要立即更改部署  但是更改後會被本地修改代碼影響

我們將hello.txt改爲

hello5
hhhhhh
fffff
fffff
fffff
fffff
ffffff
fff
ffff
ffffff
192:files liqiang$ git stash save '備份'
Saved working directory and index state On master: 備份

這個時候暫存區 和工作區都會被還原成分支的最新版本  同時把自己修改的數據備份起來

這個時候我們就可以修改bug並並提交

通過git stash list可以查看我們的備份 

192:files liqiang$ git stash list
stash@{0}: On master: 備份

改完bug後通過

$ git stash apply stash@{0}

則可以還原

修改分支名字 

1.刪除遠程分支

git push --delete origin [分支名字]

2.重命名本地分支

git branch -m [修改的分支名字] [新增分支名字]

 

3.重新提交新分支

git push origin [新分支名字]

 

刪除本地分支

git branch -D 分支名字

 

刪除遠程分支

liqiangdeMacBook-Pro:comm-libs liqiang$  git push origin --delete upgrade_fs_user
git: 'credential-wincred' is not a git command. See 'git --help'.
To http://git.biaoguoworks.com/user/comm-libs.git
 - [deleted]         upgrade_fs_user

 

 

查看遠程分支

liqiangdeMacBook-Pro:comm-libs liqiang$ git branch -r
  origin/HEAD -> origin/master
  origin/dev
  origin/fix-feign
  origin/master
  origin/upgrade-20211129
  origin/upgrade_fs_user
  origin/yq_detail_log

查看本地分支

liqiangdeMacBook-Pro:comm-libs liqiang$ git branch
  dev
  master
* upgrade-20211129_liqiang
  upgrade_fs_user
  upgrand_trace_log_liqiang

 

 

解除關聯重新關聯新的倉庫

liqiangdeMacBook-Pro:canal-canal-1.1.4 liqiang$ git remote rm origin
liqiangdeMacBook-Pro:canal-canal-1.1.4 liqiang$ git remote add origin http://git.tuna.1919.cn/kh/soon/canal-canal-1.1.4.git
liqiangdeMacBook-Pro:canal-canal-1.1.4 liqiang$ git push -u origin master

基於tag創建分支

2.通過:git branch <new-branch-name> <tag-name> 會根據tag創建新的分支.

例如:git branch newbranch v1.0 . 會以tag v1.0創建新的分支newbranch;

3.可以通過git checkout newbranch 切換到新的分支.

4.通過 git push origin newbranch 把本地創建的分支提交到遠程倉庫.

 

merge管理

常見異常

You have not concluded your merge (MERGE_HEAD exists)

表示合併失敗可以

$:git merge --abort #取消合併如果Git版本 >= 1.7.4
$:git reset --merge #取消合併如果Git版本 >= 1.6.1
$:git pull

代碼比較

比較工作區和版本庫差異

git diff HEAD -- readme.txt //指定文件
git diff HEAD//所有

 

 


 

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