Git基于命令行的常規操作
? 初始化本地git環境
#!/bin/bash
username=$1
email=$2
git=/usr/bin/git
if [ $# -ne 2 ];then
echo $"Usage:$0 <Username> <Email>"
exit 2
fi
$git config --global user.name "$username"
$git config --global user.email "$email"
$git config --global color.ui true
$git config --global alias.olog "log --pretty=oneline"
$git config --global alias.plog "log -p"
說明:SHA1:是git 對象中的40位hash編碼數
? 文件提交
Git add index.html(多個文件,就用點號.)
Git commit –m “add index.html”
? 文件重命名
[root@localhost test]# git mv pay.html pay.php
[root@localhost test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: pay.html -> pay.php
? 撤銷工作區的修改
git checkout -- news.html
? 從暫存區中刪除,工作目錄保留
git rm --cached test.bin
? 撤銷提交到工作區(版本回退到工作區)
git reset --mixed HEAD^ 或者 git reset --mixed SHA1
? 撤銷提交到暫存區(版本回退到暫存區)
Git reset –soft HEAD^ 或者 git reset –soft SHA1
? 將工作區、暫存區和版本庫恢復到指定版本
[root@localhost test]# git reset --hard cd6495c
HEAD is now at cd6495c news01.html
(執行這個操作,做好在其他窗口執行git log –oneline)
? 查看某個文件的散列值和版本庫里的內容
[root@localhost test]# git hash-object -w about3.html
641d57406d212612a9e89e00db302ce758e558d2
[root@localhost test]# git cat-file -p 641d57
111
222
333
? 查看某個文件是誰提交或誰修改的
作用:查看文件內容是誰添加
案例:我想查看about3.html這個文件第二行是誰提交的
[root@localhost test]# cat about3.html
111
222
333
[root@localhost test]# git blame about3.html -L 2,+1
400fa95a (louis 2018-04-19 15:52:20 +0800 2) 222
? 查看某個文件的修改歷史
git log -C news.html
? 提交查找
[root@localhost test]# git grep --name-only news (查看包含news的文件名)
news.html
news01.html
[root@localhost test]# git grep -n news (查看包含news的文件名,并顯示該內容在第幾行)
news.html:1:news center
news01.html:1:news center
分支管理
? 本地新建分支
[root@localhost test]# git branch dev
[root@localhost test]# git checkout dev
Switched to branch 'dev'
注意:我們可以基于任意一次提交來創建分支
[root@localhost test]# git log --oneline
dd97293 rename pay.html pay.htm
cd6495c news01.html
b20d55e add detail.html
400fa95 squash commit about3.html
c9bd929 abount2
bbe4912 about
7fecdff pay
9770bad news
72dc15a first commit
[root@localhost test]# git branch pay 7fecdff
[root@localhost test]# git checkout pay
Switched to branch 'pay'
[root@localhost test]# ll
total 12
-rw-r--r--. 1 root root 0 Apr 23 13:16 clib.a
drwxr-xr-x. 2 root root 45 Apr 23 13:19 doc
-rw-r--r--. 1 root root 31 Apr 19 16:11 index.html
-rw-r--r--. 1 root root 12 Apr 23 14:53 news.html
-rw-r--r--. 1 root root 26 Apr 23 16:27 pay.html
-rw-r--r--. 1 root root 0 Apr 23 10:23 test.bin
-rw-r--r--. 1 root root 0 Apr 23 13:14 test.o
? 刪除分支
對于已經合并的分支 git branch –d dev
對于沒有合并的分支 git branch –D dev
? 分支重命名
git branch –m dev Dev
? 查看哪些分支沒有被合并
Git branch –no-merged
? 分支儲藏
作用:當你正在一個分支上寫代碼,但是此時突然有個緊急事情需要處理,然后當前分支上的代碼又不能提交,這個時候就會用到儲藏的功能
[root@localhost test]# touch stash.txt
[root@localhost test]# echo "statsh:add1" >> stash.txt
[root@localhost test]# git add stash.txt
[root@localhost test]# git stash (把之前沒有提交的代碼暫存起來)
Saved working directory and index state WIP on dev: dd97293 rename pay.html pay.htm
HEAD is now at dd97293 rename pay.html pay.htm
[root@localhost test]# git checkout -b bug (然后切換到其他分支,開始修復bug,修復完畢后,合并到主分支)
Switched to a new branch 'bug'
[root@localhost test]# git checkout dev (bug修復完,并合并到主分支后,然后再切換到dev分支,開始之前的開發)
Switched to branch 'dev'
[root@localhost test]# git stash pop 彈出
[root@localhost test]# git stash list
? 如果不知道想要恢復分支的散列值,可以通過reflog
[root@localhost test]# git reflog
dd97293 HEAD@{0}: checkout: moving from bug to dev
dd97293 HEAD@{1}: checkout: moving from dev to bug
dd97293 HEAD@{2}: checkout: moving from master to dev
? 合并分支(merge)
一) 快進式合并
[root@localhost test]# git branch -a
bug
* dev
master
pay
[root@localhost test]# git commit -m "add stats"
[dev 1cef480] add stats
1 file changed, 1 insertion(+)
create mode 100644 stash.txt
[root@localhost test]# git checkout master
Switched to branch 'master'
[root@localhost test]# git merge dev
Updating dd97293..1cef480
Fast-forward
stash.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 stash.txt
二) 合并提交
場景:當主分支和dev分支同時又內容提交的時候,我們需要把dev分支合并到master分支,這個時候使用git merge,就會生成一個新的commit對象
Git merge dev
? 壓合合并
場景:將一個分支上的所有歷史提交合并成一個提交,然后合并到另一個分支,一般用于修改bug或某個小型功能分支
git merge –squash bug
git commit
? 分支衍合
場景:基于某個開源軟件做二次開發。當我們在基于開源軟件做二次開發的時候,這個時候開源軟件master分支也在不斷的更新,此時我們需要使用開源軟件的master分支,我們就可以通過git rebase把master分支合并到我們的分支上
1)先切換到自己的分支(dev)
2)Git rebase master
? 推送分支到遠程
[root@linux7-node2 app01]# git checkout dev
Switched to branch 'dev'
[root@linux7-node2 app01]# git push origin dev:dev
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 293 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@git.51yuki.cn:java01/app01.git
* [new branch] dev -> dev
? 拉取遠程分支并創建本地分支
[root@linux7-node2 app01]# git fetch origin bug:Newbug (遠程分支bug,本地分支Newbug)
該方法本地不會自動切換到Newbug分支
? 基于本地分支建立一個于遠程分支的關聯
[root@linux7-node2 app01]# git branch -a
Newbug
* dev
master
remotes/origin/dev
remotes/origin/master
[root@linux7-node2 app01]# git checkout Newbug
[root@linux7-node2 app01]# git push origin Newbug:bug
Everything up-to-date
[root@linux7-node2 app01]# git branch -a
* Newbug
dev
master
remotes/origin/bug
remotes/origin/dev
remotes/origin/master
- 第一部分:Git的基本操作
- 第一章:git軟件的安裝
- 第一節:在centos6.8上安裝git-2.3
- 第二節:在windows上安裝git-bash和TortoiseGit軟件
- 第二章: Git的基本概念
- 第一節:git的基本操作
- 第二節:git的深入理解(內部運行機制)
- 第三章:git的文件管理
- 第一節:文件的添加及提交
- 第二節:文件的刪除、回退等
- 第三節:忽略指定格式的文件
- 第四節:撤銷本地倉庫的修改
- 第四章:git的commit對象深入理解
- 第一節:查看提交歷史(git log)
- 第二節:提交查找(git grep)
- 第三節:git版本庫回退
- 第五章: Git分支管理
- 第一節:分支的概念及基本使用
- 第二節:Git分支管理策略
- 第三節:Git分支合并
- 第四節:推送本地分支到遠程分支
- 第七章: Git常用命令詳解
- 第一節:git fetch命令
- 第二節:git pull命令
- 第三節:git push命令
- 第四節: git merge命令
- 第五節:git rebase命令
- 第八章:遠程倉庫管理
- 第二部分: gitlab版本控制系統
- 第一節:"遠程倉庫”版本回退解決方案
- 第二節:遠程分支和本地分支
- 第九章:Git沖突解決
- 第十章:客戶端操作
- 第十章:git常規操作
- 第一章:安裝gitlab服務器
- 第一節:在centos系統上安裝
- 第二章:配置GitLab服務器
- 第一節:關閉gitlab注冊功能
- 第二節:在gitlab上創建項目、組、用戶
- 第三節:漢化gitlab
- 第四節:gitlab配置郵箱通知
- 第五節:gitlab配置https訪問
- 第三章:使用過程常見的故障
- 第三部分: gogs版本控制系統
- 第一章:在centos6上基于二進制包安裝gogs軟件
- 第二章:gogs服務器的配置
- 第一節:gogs服務器上創建項目、用戶
- 第二節:nginx反代gogs,通過https
- 第三節:gogs啟動腳本
- 第四節:git保存密碼到本地
- 第三章:gogs服務器的備份和恢復
- 第一節:gogs備份操作