git仓库相关知识01-安装和基本命令
一 。git工作流程
? ? ? Git是一個開源的分布式版本控制系統,用于敏捷高效地處理任何或小或大的項目,Git 與常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本庫的方式,不必服務器端軟件支持。GIT不僅僅是個版本控制系統,它也是個內容管理系統(CMS),工作管理系統等。如果你是一個具有使用SVN背景的人,你需要做一定的思想轉換,來適應GIT提供的一些概念和特征。
Git 與 SVN 區別點:
1、GIT是分布式的,SVN不是:這是GIT和其它非分布式的版本控制系統,例如SVN,CVS等,最核心的區別。
2、GIT把內容按元數據方式存儲,而SVN是按文件:所有的資源控制系統都是把文件的元信息隱藏在一個類似.svn,.cvs等的文件夾里。
3、GIT分支和SVN的分支不同:分支在SVN中一點不特別,就是版本庫中的另外的一個目錄。
4、GIT沒有一個全局的版本號,而SVN有:目前為止這是跟SVN相比GIT缺少的最大的一個特征。
5、GIT的內容完整性要優于SVN:GIT的內容存儲使用的是SHA-1哈希算法。這能確保代碼內容的完整性,確保在遇到磁盤故障和網絡問題時降低對版本庫的破壞。
?git 工作區 暫存區 版本庫概念:
? ?》工作區:就是你在電腦里能看到的目錄。
? ?》暫存區:英文叫stage, 或index。一般存放在 ".git目錄下" 下的index文件(.git/index)中,所以我們把暫存區有時也叫作索引(index)。
? ?》版本庫:工作區有一個隱藏目錄.git,這個不算工作區,而是Git的版本庫。
比如我新建了一個項目 該項目就是工作區 可以在該項目下創建一個git版本庫 ?編輯的源代碼默認都是存在于工作區 ?工作區可以通過git add 添加代碼到暫存區 暫存區的代碼可以提交到最終版本庫
使用eclipse ?其中新建的項目webtest默認是在工作區 其中git視圖中的 staged changes 就是講文件加入到暫存取 工作區有文件修改后 ?unstaged changes 出現該文件等待你加入暫存取 可以將暫存區的數據 commit到版本庫中
圖解(來自runoobj)
演示eclipse下的工作區 暫存區和版本庫
二。 git本地安裝
? ?官網 (https://git-scm.com/book/zh/v1)
? ?環境:Centos環境?
Yum -y isntall git-core? 查看git版本 Git --version? 查看git幫助 [root@node3 bin]# git --help usage: git [--version] [--help] [-c name=value][--exec-path[=<path>]] [--html-path] [--man-path] [--info-path][-p|--paginate|--no-pager] [--no-replace-objects] [--bare][--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]<command> [<args>]The most commonly used git commands are:add Add file contents to the indexbisect Find by binary search the change that introduced a bugbranch List, create, or delete branchescheckout Checkout a branch or paths to the working treeclone Clone a repository into a new directorycommit Record changes to the repositorydiff Show changes between commits, commit and working tree, etcfetch Download objects and refs from another repositorygrep Print lines matching a patterninit Create an empty Git repository or reinitialize an existing onelog Show commit logsmerge Join two or more development histories togethermv Move or rename a file, a directory, or a symlinkpull Fetch from and merge with another repository or a local branchpush Update remote refs along with associated objectsrebase Forward-port local commits to the updated upstream headreset Reset current HEAD to the specified staterm Remove files from the working tree and from the indexshow Show various types of objectsstatus Show the working tree statustag Create, list, delete or verify a tag object signed with GPG
三。 git常用基本命令
? 一般git管理本地有個完整的工作區,暫存區和版本庫 所以基本基于本機操作?
git常用基本命令如下
?1。git創建倉庫
? ? ?任意目錄 git init [目錄名 不指定當前目錄生成.git指定就在指定目錄創建.git]當前目錄創建一個隱藏目錄.git
?
?2。git配置(git config)
Git配置決定了git倉庫的行為 配置的參數以鍵值對存儲 具體參數 參考官網(https://git-scm.com/docs/git-config) 默認的git配置有三種
?????1》系統配置 /etc/gitconfig系統中所有用戶都公用的全局配置 如果用戶和當前倉庫都沒有某個參數時 系統配置文件中的參數就生效 ?git -config --system鍵“值”
?????2》全局配置 ~/.gitconfig ?當前登錄用戶的全局配置 在當前用戶的工作目錄下 當前用戶創建了多個倉庫 沒有某個參數時 全局配置生效git -config --global鍵“值”
? ? 3》當前倉庫 倉庫/.git/config文件里 就近原則 這里沒有 找全局 全局沒有找系統
git -config --local 鍵 “值”??(以下測試是git init需要先創建一個空倉庫 有.git文件夾)
測試例子
[root@node3 .git]# git config --system user.name "jiaozi" 系統中設置用戶名jiaozi [root@node3 .git]# git config --global user.name "jiaozi1" 全局中設置用戶名 jiaozi1 [root@node3 .git]# git config --local user.name "jiaozi2" 本地倉庫設置用戶名 jiaozi2 [root@node3 .git]# git config --get user.name 當前倉庫.git目錄中獲取 是jiaozi2 jiaozi2 [root@node3 .git]# git config --system --get user.name 通過指定 --system取系統 jiaozi [root@node3 .git]# git config --global --get user.name jiaozi1 [root@node3 .git]# cd .. 到達.git上一級目錄也就是倉庫根目錄 也是jiaozi2 [root@node3 myproject]# git config --get user.name jiaozi2 [root@node3 myproject]# cd .. 去到別的目錄 獲取的是用戶 切換成其他用戶是jiaozi [root@node3 git]# git config --get user.name jiaozi1查看具體的文件
[root@node3 .git]# more config [core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = true [user]name = jiaozi2 [root@node3 .git]# more ~/.gitconfig [user]name = jiaozi1 [root@node3 .git]# more /etc/gitconfig [user]name = jiaozi?3。git添加文件到暫存區(git add)? ? ?git倉庫也是使用類文件系統這樣的目錄結構管理文件 所以工作區文件使用git add命令后 該文件被加入objects的對象庫中index目錄樹也及時暫存區 會添加一個引用到該對象表示只是 暫存(圖片引用自菜鳥教程)
舉例:
[root@node3 myproject]# echo hello>a.txt [root@node3 myproject]# ll total 4 -rw-r--r-- 1 root root 6 Nov 3 03:19 a.txt [root@node3 myproject]# git add a.txt [root@node3 myproject]# tree .git .git ├── branches ├── config ├── description ├── HEAD ├── hooks │?? ├── applypatch-msg.sample │?? ├── commit-msg.sample │?? ├── post-update.sample │?? ├── pre-applypatch.sample │?? ├── pre-commit.sample │?? ├── prepare-commit-msg.sample │?? ├── pre-push.sample │?? ├── pre-rebase.sample │?? └── update.sample ├── index ├── info │?? └── exclude ├── objects │?? ├── ce │?? │?? └── 013625030ba8dba906f756967f9e9ca394464a │?? ├── info │?? └── pack └── refs├── heads└── tags10 directories, 15 filesbjects目錄每個文件都會生成一個2位隨機字符串的目錄 和該目錄下 隨機字符串的二進制文件 該文件是個壓縮文件 可以通過
?git cat-file -p 2位目錄+32位目錄 查看內容
在添加一個試試
[root@node3 myproject]# echo test>b.txt [root@node3 myproject]# git add b.txt [root@node3 myproject]# tree .git .git ├── branches ├── config ├── description ├── HEAD ├── hooks │?? ├── applypatch-msg.sample │?? ├── commit-msg.sample │?? ├── post-update.sample │?? ├── pre-applypatch.sample │?? ├── pre-commit.sample │?? ├── prepare-commit-msg.sample │?? ├── pre-push.sample │?? ├── pre-rebase.sample │?? └── update.sample ├── index ├── info │?? └── exclude ├── objects │?? ├── 9d │?? │?? └── aeafb9864cf43055ae93beb0afd6c7d144bfa4 │?? ├── ce │?? │?? └── 013625030ba8dba906f756967f9e9ca394464a │?? ├── info │?? └── pack └── refs├── heads└── tags11 directories, 16 files查看內容
[root@node3 myproject]# git cat-file -p 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 test [root@node3 myproject]# git cat-file -p ce013625030ba8dba906f756967f9e9ca394464a Hello?4。git暫存區添加到版本庫(git commit 文件名 -m “注釋”)
實例演示
[root@node3 myproject]# ll total 8 -rw-r--r-- 1 root root 11 Nov 3 04:03 a.txt -rw-r--r-- 1 root root 7 Nov 3 03:58 b.txt [root@node3 myproject]# git status -s ?? a.txt ??表示未添加到暫存區 ?? b.txt [root@node3 myproject]# git add a.txt [root@node3 myproject]# git status -s A a.txt A表示添加到暫存區 ?? b.txt [root@node3 myproject]# git commit a.txt -m "a" 如果需要提交必須先設置用戶名和郵箱*** Please tell me who you are.Rungit config --global user.email "you@example.com"git config --global user.name "Your Name"to set your account's default identity. Omit --global to set the identity only in this repository.fatal: unable to auto-detect email address (got 'root@node3.(none)') [root@node3 myproject]# git config --global user.email "you@example.com" [root@node3 myproject]# git commit a.txt -m "a" [master (root-commit) 930c52d] a1 file changed, 2 insertions(+)create mode 100644 a.txt [root@node3 myproject]# git status -s 提交后status中看不到a.txt了 ?? b.txt?5。Git比較文件修改(git diff文件名 )? ?Git可以查看工作區的文件修改和暫存區的文件變化
root@node3 myproject]# more b.txt tttttt [root@node3 myproject]# git add b.txt修改文件 添加一行 1234 [root@node3 myproject]# git status -s AM b.txt [root@node3 myproject]# git diff 查看工作區文件和暫存區文件不一致 diff --git a/b.txt b/b.txt index 33262c0..dfd0efb 100644 --- a/b.txt +++ b/b.txt @@ -1 +1,2 @@tttttt +1234 添加了1234 [root@node3 myproject]# git diff --cached diff --git a/b.txt b/b.txt new file mode 100644 index 0000000..33262c0 --- /dev/null +++ b/b.txt @@ -0,0 +1 @@ +tttttt?6。Git暫存區移除(git rm --cached 文件名 )[root@node3 myproject]# echo dd>c.txt [root@node3 myproject]# git status -s D b.txt ?? b.txt ?? c.txt [root@node3 myproject]# git add c.txt [root@node3 myproject]# git status -s D b.txt A c.txt ?? b.txt [root@node3 myproject]# git rm --cached c.txt rm 'c.txt' [root@node3 myproject]# git status -s D b.txt ?? b.txt ?? c.txt?7。Git重置(git reset 版本庫 )
Git reset將制定版本庫的目錄樹重置到暫存區 ?比如版本庫中有個A.?txt內容是aaa ?工作區下有個文件a.txt內容是bbb ?如果git add a.txt
暫存區的a.txt就是bbb執行git rest HEAD此時暫存區被重置為版本庫的aaa
狀態就是M
??這里注意HEAD是個指針 指向master版本庫
root@node3 myproject]# cat .git/HEAD ref: refs/heads/master?8。Git重命名(git mv舊文件名 新文件名 )Git添加到暫存區的文件可以重命名為其他文件名 比如
[root@node3 myproject]# git mv b.txt b1.txt [root@node3 myproject]# git status -s RM b.txt -> b1.txt ?? c.txt?9。Git查看文件狀態(git status )Git status 查看工作區和暫存區的文件的狀態
-s 是指定簡潔輸出
文件狀態
A 表示 git add 新加入到暫存區的文件D 表示從暫存區刪除M 表示已經添加到暫存區但是工作區有修改的文件RM 表示工作區文件被重命名?10。Git查看文件狀態(git status )
? 查看提交日志信息
[root@node3 myproject]# git log commit e35fe0a7e8511f9e4966025b681f1803f5b090fb Author: jiaozi1 <you@example.com> Date: Fri Nov 3 05:15:54 2017 -0700d.txt 四。 git版本庫分支
? 分支 每個項目都存在一個主線有些功能必須從主線中分支出來而不影響主線同時繼續工作 ?比如 mysql被oracle收購后 分支出mariadb(獨立)和oraclemysql(主分支)
? 分支常用命令:?1。查看所有分支(默認只有master)
[root@node3 myproject]# git branch * master?2。查看所有分支(默認只有master)? 創建分支(該分支被創建 就相當于拷貝了當前分支(默認master)的一份快照 以后修改了 不會影響分支 分支修改不影響主分支)
[root@node3 myproject]# git branch * master [root@node3 myproject]# git branch test [root@node3 myproject]# git branch * master * test?3。查看所有分支(默認只有master)
切換分支(切換到哪個分支 提交的代碼就提交到哪個分支 默認是master)
[root@node3 myproject]# git branch test 創建分區 test [root@node3 myproject]# echo 3>c.txt 默認工作區創建一個c.txt[root@node3 myproject]# git add c.txt 添加到暫存區 [root@node3 myproject]# git commit -m "c.txt" 提交到當前分支master [master cc8f260] c.txt1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 c.txt [root@node3 myproject]# ll total 4 -rw-r--r-- 1 root root 1 Nov 3 05:02 a.txt -rw-r--r-- 1 root root 0 Nov 3 05:02 b.txt -rw-r--r-- 1 root root 0 Nov 3 05:03 c.txt [root@node3 myproject]# git status -s [root@node3 myproject]# git checkout test 切換到test分支 test是沒有創建c.txt之前創建 所有應該沒有c.txt Switched to branch 'test' [root@node3 myproject]# ll total 4 -rw-r--r-- 1 root root 1 Nov 3 05:02 a.txt -rw-r--r-- 1 root root 0 Nov 3 05:02 b.txt [root@node3 myproject]# git checkout master 切換回master c.txt回來了 Switched to branch 'master' [root@node3 myproject]# ll total 4 -rw-r--r-- 1 root root 1 Nov 3 05:02 a.txt -rw-r--r-- 1 root root 0 Nov 3 05:02 b.txt -rw-r--r-- 1 root root 0 Nov 3 05:04 c.txt可以使用 git checkout -b (branchname) 命令來創建新分支并立即切換到該分支下
4。合并分支(當某幾個分支有了獨立內后 就可以合并到當前分支 分支過程中可能存在沖突 解決過程不列出)
?
[root@node3 myproject]# git checkout -b test 創建分區并切換 Switched to a new branch 'test' [root@node3 myproject]# echo 4>d.txt 分支中添加d.txt[root@node3 myproject]# git add d.txt [root@node3 myproject]# git commit -m "d.txt" 提交到test當前分區 [test e35fe0a] d.txt1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 d.txt [root@node3 myproject]# ll 查看分區 total 4 -rw-r--r-- 1 root root 1 Nov 3 05:02 a.txt -rw-r--r-- 1 root root 0 Nov 3 05:02 b.txt -rw-r--r-- 1 root root 0 Nov 3 05:04 c.txt -rw-r--r-- 1 root root 0 Nov 3 05:15 d.txt [root@node3 myproject]# git checkout master 切換到master 將test合并到當前分支master Switched to branch 'master' [root@node3 myproject]# git merge test 合并test 合并到哪個就切換回哪個 Updating cc8f260..e35fe0a Fast-forwardd.txt | 01 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 d.txt5。刪除分支(刪除分支后 不能切換) [root@node3 myproject]# git branch -d test Deleted branch test (was 611f3e1). [root@node3 myproject]# git checkout test error: pathspec 'test' did not match any file(s) known to git.轉載于:https://www.cnblogs.com/liaomin416100569/p/9331172.html
總結
以上是生活随笔為你收集整理的git仓库相关知识01-安装和基本命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷—— P3807 【模板】卢卡斯定理
- 下一篇: vuex数据管理-数据适配