歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Git基本操作

Git基本操作

日期:2017/2/28 13:59:34   编辑:Linux教程

git clone [-b 分支名] chenlong@http://project/admin.git

不帶-b參數默認master分支

git clone http://project/admin.git
git clone –progress -v http://project/admin.git

git.exe clone –progress -v “http://project/admin.git” “\\192.168.0.105\www\other_user\chenlong\admin”

設置 -> git 編輯本地 .git/config 增加
[credential]
helper = store

git clone @http://project/admin.git admin2

git checkout — application/core/MY_Exceptions.php

git reset HEAD(commit) file

git reset –hard HEAD~3
會將最新的3次提交全部重置,就像沒有提交過一樣。

git reset 版本號 — b.txt

在Windows平台下面就有這個該死的行分隔符的問題,一般推薦

git config –global core.autocrlf false
另外還可以

git config –global core.safecrlf true
保證文件的行分隔符不會混雜。 然後以防萬一重新git checkout或者git reset –hard一下

================================================================================================================================
查看blob
git show 6ff87c4664 查看blob

查看樹
git ls-tree fb3a8bdd0ce 查看tree
注意:所有的文件的mode位都是644 或 755,這意味著Git只關心文件的可執行位.

查看某個提交
git show -s –pretty=raw 2be7fcb476

查看tag
git cat-file tag v1.5.0

每個目錄都創建了 tree對象 (包括根目錄), 每個文件都創建了一個對應的 blob對象 . 最後有一個 commit對象 來指向根tree對象(root of trees), 這樣我們就可以追蹤項目每一項提交內容.
一個commit包含
一個tree
父對象 (parent(s))
作者author
提交者committer

|– HEAD # 這個git項目當前處在哪個分支裡
|– config # 項目的配置信息,git config命令會改動它
|– description # 項目的描述信息
|– hooks/ # 系統默認鉤子腳本目錄
|– index # 索引文件
|– logs/ # 各個refs的歷史信息
|– objects/ # Git本地倉庫的所有對象 (commits, trees, blobs, tags)
`– refs/ # 標識你項目裡的每個分支指向了哪個提交(commit)。

.git/refs/heads/master

.git/refs/remotes/origin/master

//add了沒有commit
Changes to be committed:
(use “git reset HEAD…” to unstage)

new file: yyy

//原來的文件修改了 沒有add
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git checkout –…” to discard changes in working directory)

modified: test

//新文件沒有add過的
Untracked files:
(use “git add…” to include in what will be committed)

xxx

git config –global user.name “Scott Chacon”
git config –global user.email “[email protected]

Clone一個倉庫
git clone http://www.kernel.org/pub/scm/git/git.git [dir]

初始化一個新的倉庫
git init

除了用git add 命令,我還可以用
$ git commit -a
這會自動把所有內容被修改的文件(不包括新創建的文件)都添加到索引中,並且同時把它們提交。相當於add + commit

Git跟蹤的是內容不是文件
git add 不但是用來添加不在版本控制中的新文件,也用於添加已在版本控制中但是剛修改過的文件;
在這兩種情況下, Git都會獲得當前文件的快照並且把內容暫存(stage)到索引中,為下一次commit做好准備。

例如:
修改了test ,git add 後 看
Changes to be committed:
modified: mmm

再修改test git status看
Changes to be committed:
modified: mmm

Changes not staged for commit:
modified: mmm

分支與合並@基礎
查看當前分支
git branch
創建
git branch test
切換到某個分支
git checkout branch
合並
git merge test

Git鼓勵大量使用分支:

查看分支:git branch

創建分支:git branch name

切換分支:git checkout name

創建+切換分支:git checkout -b name

合並某分支到當前分支:git merge name 合並後的操作2個分支看起來都是一樣的

刪除分支:git branch -d name

修改的還沒add的為 a版本,修改了add了的為 index版本,已經commit過的為 c版本,已經push的為 r版本。

git diff
來找你當前工作目錄和上次提交與本地索引間的差異 (即a版本和c版本)
git diff –cached
看在下次提交時要提交的內容(staged,添加到索引中) (即index版本和c版本)

git diff master..test
git diff master…test 比父分支

.gitignore 忽略文件不提示在Untracked files裡

git stash 存儲某個時間狀態

http://gitbook.liuhui998.com/3_6.html

http://gitbook.liuhui998.com/4_9.html

Git的撤消操作 – 重置, 簽出 和 撤消

===================================9.24==================================================================
版本回退(未push出去之前):

首先,Git必須知道當前版本是哪個版本,在Git中,用HEAD表示當前版本,也就是最新的提交“ 3628164…882e1e0”(注意我的提交ID和你的肯定不一樣),上一個版本就是HEAD^,上上一個版本就是HEAD^^,當然往上100個版本寫100個^比較容易數不過來,所以寫成HEAD~100。

$ git reset –hard HEAD^
HEAD is now at ea34578 add distributed

日志 git log 會清除掉

又想回到最新的 git reflog查看歷史 找到最新的 id
再回來
$ git reset –hard 3628164
HEAD is now at 3628164 append GPL

命令git checkout — readme.txt意思就是,把readme.txt文件在工作區的修改全部撤銷,這裡有兩種情況:
一種是readme.txt自修改後還沒有被放到暫存區,現在,撤銷修改就回到和版本庫一模一樣的狀態;
一種是readme.txt已經添加到暫存區後,又作了修改,現在,撤銷修改就回到添加到暫存區後的狀態。
總之,就是讓這個文件回到最近一次git commit或git add時的狀態。

撤銷修改:
場景1:當你改亂了工作區某個文件的內容,想直接丟棄工作區的修改時,用命令git checkout — file。
場景2:當你不但改亂了工作區某個文件的內容,還添加到了暫存區時,想丟棄修改,分兩步,第一步用命令git reset HEAD file,就回到了場景1,第二步按場景1操作。
場景3:已經提交了不合適的修改到版本庫時,想要撤銷本次提交,參考版本回退一節,不過前提是沒有推送到遠程庫。

git checkout其實是用版本庫裡的版本替換工作區的版本,無論工作區是修改還是刪除,都可以“一鍵還原”。

請問git rm –cache 和 git reset HEAD 的區別到底在哪裡呢?

在 git add <somefile> 之後,有的時候提示用“git rm –cache”來unstage,有的時候卻是提示用“git reset HEAD”。

如果要刪除文件,最好用git rm file_name,而不應該直接在工作區直接rm file_name。
如果一個文件已經add到暫存區,還沒有commit,此時如果不想要這個文件了,有兩種方法:
1,用版本庫內容清空暫存區,git reset HEAD
2,只把特定文件從暫存區刪除,git rm –cache file

>> 有的時候提示用“git rm –cache”來unstage
HEAD 裡沒有 <somefile> 時提示

>> 有的時候卻是提示用“git reset HEAD”。
HEAD 裡有 <somefile> 時提示

添加遠端repo:
git remote add upstream https://github.com/linuxidc/first.git

要關聯一個遠程庫,使用命令git remote add origin git@server-name:path/repo-name.git;
關聯後,使用命令git push -u origin master第一次推送master分支的所有內容;
此後,每次本地提交後,只要有必要,就可以使用命令git push origin master推送最新修改;
分布式版本系統的最大好處之一是在本地工作完全不需要考慮遠程庫的存在,也就是有沒有聯網都可以正常工作,而SVN在沒有聯網的時候是拒絕干活的!當有網絡的時候,再把本地提交推送一下就完成了同步,真是太方便了!

git push
-u, –set-upstream

https://github.com/linuxidc/first.git

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "upstream"]
url = https://github.com/linuxidc/first.git
fetch = +refs/heads/*:refs/remotes/upstream/*

執行git push –set-upstream upstream master後

[branch "master"]
remote = upstream
merge = refs/heads/master

git remote add upstream https://github.com/linuxidc/first.git
git push -u upstream master

git push –set-upstream upstream master


Branch master set up to track remote branch master from upstream.

查看文件 cat .git/config 多了內容
[branch "master"]
remote = upstream
merge = refs/heads/master

[root@chenlong first]# git push -u upstream master
Username for ‘https://github.com’: zxsz
Password for ‘https://[email protected]’:
fatal: Authentication failed

git config –global push.default simple/matching

下面說一下push.default matching和push.default simple的區別:

push.default設置maching的意思是:git push 會把你本地所有分支push到名稱相對應的遠程主機上。這意味著可能你會在不經意間push一些你原本沒打算push的分支。
push.default設置simple的意思是:git push僅僅把當前所在分支push到從當初git pull pull下來的那個對應分支上,另外,這個過程也會同時檢查各個分支的名稱是否相對應。

[root@VM_44_174_CentOS first]# git push github master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/linuxidc/first.git/info/refs

git remote set-url github https://github.com/linuxidc/first.git

ssh-keygen -t rsa -C “chenlongtest_txyun”

[remote "origin"]
fetch = + refs/heads/*:refs/remotes/origin/*
url = https://github.com/linuxidc/first.git

url = [email protected]:linuxidc/first.git

git log –graph –pretty=oneline

https://www.kernel.org/pub/software/scm/git/

centos 安裝報錯
make[1]: *** Error 2
yum install perl-ExtUtils-MakeMaker package
/bin/sh: msgfmt: command not found
yum install gettext-devel

git branch –merged
git branch –no-merged 查看尚未合並的工作
git branch -D test 強制刪除分支

用於有修改未commit的情況
git stash 存儲當前現場
git stash list

git stash apply stash@{0}
git stash drop來刪除
等同於
git stash pop

當手頭工作沒有完成時,先把工作現場git stash一下,然後去修復bug,修復後,再git stash pop,回到工作現場。

git merge –no-ff -m “merged bug fix 101″ issue-101

因此,多人協作的工作模式通常是這樣:

首先,可以試圖用git push origin branch-name推送自己的修改;

如果推送失敗,則因為遠程分支比你的本地更新,需要先用git pull試圖合並;

如果合並有沖突,則解決沖突,並在本地提交;

沒有沖突或者解決掉沖突後,再用git push origin branch-name推送就能成功!

如果git pull提示“no tracking information”,則說明本地分支和遠程分支的鏈接關系沒有創建,用命令git branch –set-upstream branch-name origin/branch-name。

這就是多人協作的工作模式,一旦熟悉了,就非常簡單。

建議遠程的 分支
git checkout -b branch-name origin/branch-name
建立本地分支和遠程分支的關聯,使用git branch –set-upstream branch-name origin/branch-name

[chenlong@MD101 first]$git checkout -b dev3 origin/dev3
fatal: Cannot update paths and switch to branch ‘dev3′ at the same time.
Did you intend to checkout ‘origin/dev3′ which can not be resolved as commit?
[chenlong@MD101 first]$git branch dev3
git checkout dev3

git push origin –delete

git log –pretty=oneline –abbrev-commit

標簽:
git tag 查看標簽名稱列表
git tag 標簽名
git tag 標簽名 commit版本號
git show 標簽名

還可以創建帶有說明的標簽,用-a指定標簽名,-m指定說明文字:

$ git tag -a v0.1 -m “version 0.1 released” 3628164
git tag -s v0.2 -m “version 0.1 released” 3628164 -s gpl加密

git tag -d v0.1
git push origin v1.0
git push origin –tags 推送所有

如果標簽已經推送到遠程,要刪除遠程標簽就麻煩一點,先從本地刪除:

[chenlong@MD101 first]$git tag -d v0.7
Deleted tag ‘v0.7′ (was feb7a94)
然後,從遠程刪除。刪除命令也是push,但是格式如下:

[chenlong@MD101 first]$git push origin :refs/tags/v0.7 有空格
To [email protected]:linuxidc/first.git
- [deleted] v0.7

自定義
git config –global color.ui true
.gitignore文件

git config –global alias.unstage ‘reset HEAD’
git unstage test.py 等同於
git reset HEAD test.py

git config –global alias.lg “log –color –graph –pretty=format:’%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)%Creset’ –abbrev-commit”

搭建git服務器

如果希望ssh公鑰生效,.ssh目錄的權限必須是0700, .ssh/authorized_keys文件權限必須是0600。

git:x:1000:1000::/home/git:/usr/bin/git-shell

git clone [email protected]:/data/git_res/test.git

Ubuntu完美安裝搭建Git服務器 http://www.linuxidc.com/Linux/2015-07/120616.htm

GitHub 教程系列文章

GitHub 使用教程圖文詳解 http://www.linuxidc.com/Linux/2014-09/106230.htm

Git 標簽管理詳解 http://www.linuxidc.com/Linux/2014-09/106231.htm

Git 分支管理詳解 http://www.linuxidc.com/Linux/2014-09/106232.htm

Git 遠程倉庫詳解 http://www.linuxidc.com/Linux/2014-09/106233.htm

Git 本地倉庫(Repository)詳解 http://www.linuxidc.com/Linux/2014-09/106234.htm

Git 服務器搭建與客戶端安裝 http://www.linuxidc.com/Linux/2014-05/101830.htm

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm

分享實用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm

Ubuntu下Git服務器的搭建與使用指南 http://www.linuxidc.com/Linux/2015-07/120617.htm

Git 的詳細介紹:請點這裡
Git 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved