Git學習筆記

 

1. Git介紹

Git是一種版本管理工具(VCS),可用於分佈式版本控制。經常使用命令:html

a) Git init        //初始化本地倉庫git

b) Git add <file>  //添加文件到隊列中github

c) Git status    //查看狀態apache

d) Git commit    //提交vim

e) Git push          //推送到倉庫app

f) Git pull            //從遠程倉庫拉取數據分佈式

g) Git clone         //從遠程倉庫拷貝數據工具

2. Git的安裝<略>

查看安裝狀態:git --version編碼

3. 提交文件到本地倉庫

3.1 新建html和js文件,執行添加文件到隊列、從隊列移除文件等操做spa

     wangrongdeMBP:Desktop Eric$ mkdir demo
wangrongdeMBP:Desktop Eric$ cd demo/
wangrongdeMBP:demo Eric$ touch index.html
wangrongdeMBP:demo Eric$ touch app.js
wangrongdeMBP:demo Eric$ git init

Initialized empty Git repository in /Users/Eric/Desktop/demo/.git/

wangrongdeMBP:demo Eric$ git config --global user.name 'z'
wangrongdeMBP:demo Eric$ git config --global user.email '86779708@qq.com'
wangrongdeMBP:demo Eric$ git add index.html
wangrongdeMBP:demo Eric$ git status

On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html
Untracked files:
(use "git add <file>..." to include in what will be committed)
app.js

wangrongdeMBP:demo Eric$ git rm --cached index.html

rm 'index.html'

wangrongdeMBP:demo Eric$ git status

On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
app.js
index.html
nothing added to commit but untracked files present (use "git add" to track)

wangrongdeMBP:demo Eric$ git add *.html
wangrongdeMBP:demo Eric$ git rm --cached index.html

rm 'index.html'

wangrongdeMBP:demo Eric$ git add .
wangrongdeMBP:demo Eric$ git status

On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: app.js
new file: index.html

3.2 將demo拖入sublime,打開並編輯index.html(編輯框 輸入html,按下Tab鍵可快速生成模板)

3.3 執行git status,顯示文件修改了,以下:

     wangrongdeMBP:demo Eric$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: app.js
new file: index.html

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: index.html

3.4 文件有修改,要從新添加到隊列:git add .

     wangrongdeMBP:demo Eric$ git add .
wangrongdeMBP:demo Eric$ git status
On branch master

No commits yet

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

new file: app.js
new file: index.html

3.5 執行git commit提交到本地。會先到達vim頁面,輸入提交內容描述,後:wq保存,以下:

     wangrongdeMBP:demo Eric$ git commit
[master (root-commit) 56602db] 第一次提交
2 files changed, 10 insertions(+)
create mode 100644 app.js
create mode 100644 index.html
wangrongdeMBP:demo Eric$

3.6 提交後執行git status:

     wangrongdeMBP:demo Eric$ git status
On branch master
nothing to commit, working tree clean

3.7 編輯app.js,輸入內容,再次執行git status,發現文件修改了(另:注意看sublime中的本地文件,修改後多了淺藍色標識)

     wangrongdeMBP:demo Eric$ 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: app.js

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

3.8 此時須要執行git add加到序列,並git commit -m '修改了app.js",這裏加上-m至關於加了vim描述的步驟

     wangrongdeMBP:demo Eric$ git add .
wangrongdeMBP:demo Eric$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: app.js

wangrongdeMBP:demo Eric$ git commit -m "修改了app.js"
[master 6efcc1c] 修改了app.js
1 file changed, 1 insertion(+)

4. 提交本地代碼到遠程

4.1 提交到遠程倉庫時,能夠忽略一些不用提交的文件

4.1.1 新建一個不須要提交到遠程的日誌文件  

4.1.2 經過terminal在當前目錄新建ignore文件,經過sublime編輯,輸入要忽略的文件名(注意左側目錄log.txt旁的小圓圈會消失),若是忽略文件夾(如文件夾名爲dir1),則在.gitignore編輯框中另起一行輸入/dir1

     wangrongdeMBP:demo Eric$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)

.gitignore
log.txt

nothing added to commit but untracked files present (use "git add" to track)
wangrongdeMBP:demo Eric$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)

.gitignore

nothing added to commit but untracked files present (use "git add" to track)

4.2 建立分支

4.2.1 分支做用:在分支上進行編碼,不用影響主線的代碼

4.2.2 使用"git branch 分支名"命令建立分支dev1

 

     wangrongdeMBP:demo Eric$ git branch dev1
wangrongdeMBP:demo Eric$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)

.gitignore

nothing added to commit but untracked files present (use "git add" to track)

 

  查看status得知仍處於master上,可以使用git checkout來切換到新建的分支上

 

     wangrongdeMBP:demo Eric$ git checkout dev1
Switched to branch 'dev1'

4.2.3 切換到分支後,新建login.html文件,而且修改master上index.html內容,在分支上查看status狀態

 
     wangrongdeMBP:demo Eric$ git status
On branch dev1
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: index.html

Untracked files:
(use "git add <file>..." to include in what will be committed)

.gitignore
login.html

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

4.2.4 提交到本地

 

     wangrongdeMBP:demo Eric$ git add .
wangrongdeMBP:demo Eric$ git commit -m '分支上新建文件'
[dev1 964f3fe] 分支上新建文件
3 files changed, 11 insertions(+)
create mode 100644 .gitignore
create mode 100644 login.html
wangrongdeMBP:demo Eric$ git status
On branch dev1
nothing to commit, working tree clean

4.2.5 切換回master,目錄裏dev1分支的login.html文件沒了。查看master上的index.html,內容並未改變,說明分支操做不影響主線

4.3 主線及分支的合併

4.3.1 回到主線master

4.3.2 執行合併操做

 

     wangrongdeMBP:demo Eric$ git checkout master
Switched to branch 'master'
wangrongdeMBP:demo Eric$ git merge dev1
Updating b890eb3..964f3fe
Fast-forward
.gitignore | 1 +
index.html | 1 +
login.html | 9 +++++++++
3 files changed, 11 insertions(+)
create mode 100644 .gitignore
create mode 100644 login.html

4.4 操做遠程倉庫

4.4.1 登陸github,添加倉庫

 

4.4.2 終端查看remote對應的地址,發現沒有

  wangrongdeMBP:demo Eric$ git remote
  wangrongdeMBP:demo Eric$

4.4.3 複製github上倉庫的路徑,在本地終端執行命令

     wangrongdeMBP:demo Eric$ git remote add origin https://github.com/wangrong-cn/MyFirstProgram.git
wangrongdeMBP:demo Eric$ git remote
origin

4.4.4 執行推送指令

     wangrongdeMBP:demo Eric$ git push -u origin master
fatal: unable to access 'https://githubhttps://github.com/wangrong-cn/MyFirstProgram.git/': Could not resolve host: githubhttps

     上面的提示Could not resolve host: githubhttps,是由於git remote add origin xxx路徑錯誤所致。解決:

 

    1. git remote rm origin
    2. git remote add origin https://github.com/wangrong-cn/MyFirstProgram.git

 

    從新git remote,能查看到有origin路徑了。

  

4.5 從遠程倉庫拉取項目到本地

4.5.1 在github上找到項目,點擊「克隆/下載」

4.5.2 克隆項目到本地,git clone https://github.com/apache/incubator-dubbo.git

4.5.3 克隆完成後,本地切換到某個分支,就能看到該分支下的內容。不切換是看不到的

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

X.QiTa注意事項

.1 Git遠程倉庫地址變動本地如何修改

相關文章
相關標籤/搜索