好久以前在 http://git.oschina.net/ 上建立了一個私有項目 modb ,目的主要是用來學習如何使用 GIT 來開源本身寫的東東,中間因爲種種緣由停頓了很長時間,可是今天,我下定決心必定要將這個事情完成,因而乎,探索之旅又開始了……
(本文以 windows 平臺上的操做進行說明)
最初建立 modb 項目時,默認會產生以下 3 個文件:
- .gitignore
- LICENSE
- README.md
其中 .gitignore 文件的做用能夠參考:
《
.gitignore 文件使用說明
》
接下來只要從官網下載了最新的 Git 客戶端安裝使用就能夠了,我安裝的是最新的 Git-1.9.2-preview20140411.exe 。
首先,將 modb.git 獲取到本地。
D:\myGIT>git clone https://git.oschina.net/moooofly/modb.git
Cloning into 'modb'...
Username for 'https://git.oschina.net': moooofly
Password for 'https://moooofly@git.oschina.net':
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
Checking connectivity... done.
D:\myGIT>
以後,經過編輯器建立文件 helloworld.txt。
D:\myGIT>cd modb
D:\myGIT\modb>
D:\myGIT\modb>dir
驅動器 D 中的卷是 DSK1_VOL2
卷的序列號是 121D-11F5
D:\myGIT\modb 的目錄
2014-04-28 14:05 <DIR> .
2014-04-28 14:05 <DIR> ..
2014-04-28 14:05 156 .gitignore
2014-04-28 14:05 1,094 LICENSE
2014-04-28 14:05 7 README.md
2014-04-28 14:14 0 helloworld.txt
4 個文件 1,257 字節
2 個目錄 6,756,630,528 可用字節
D:\myGIT\modb>
經過 add 命令添加新建的文件,經過 status 命令查看此時的狀態信息,經過 commit 命令在本地提交變動狀態。
D:\myGIT\modb>git add .
D:\myGIT\modb>
D:\myGIT\modb>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: helloworld.txt
D:\myGIT\modb>
D:\myGIT\modb>git commit -m "add helloworld.txt"
[master 8576fc3] add helloworld.txt
Committer: unknown <sunfei@sunfei.kdcrd.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 helloworld.txt
D:\myGIT\modb>
看上面的提示,以系統默認的用戶名和密碼來進行代碼的管理彷佛不妥。按照 oschina/git-osc 和《Git初體驗》的說法,應該使用在 GIT@OSC 上註冊的用戶名和郵箱。
D:\myGIT\modb>
D:\myGIT\modb>git config --global user.name "moooofly"
D:\myGIT\modb>git config --global user.email "centos.sf@gmail.com"
D:\myGIT\modb>
D:\myGIT\modb>git commit --amend --reset-author
[master 12699ba] add helloworld.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 helloworld.txt
D:\myGIT\modb>
在輸入命令 git commit --amend --reset-author 時,會以 VIM 編輯器的形式打開以下內容的文件。
add helloworld.txt
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# new file: helloworld.txt
#
~
~
~
直接執行 wq 保存後退出便可。
從新執行 status 命令查看狀態,並使用 push 命令向服務器提交。
D:\myGIT\modb>
D:\myGIT\modb>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
D:\myGIT\modb>git push origin master
Username for 'https://git.oschina.net': moooofly
Password for 'https://moooofly@git.oschina.net':
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://git.oschina.net/moooofly/modb.git
8fb8c63..136a5da master -> master
D:\myGIT\modb>
此時刷新項目網址,能夠看到新的文件已經成功提交了(項目爲私有,目前只有我本身能看到)。
接着測試修改文件內容的狀況,在 helloworld.txt 文件中增長
hello world! haha!
以後查看狀態
D:\myGIT\modb>git status
On branch master
Your branch is up-to-date with 'origin/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: helloworld.txt
no changes added to commit (use "git add" and/or "git commit -a")
從輸出信息中能夠得知,個人修改 git 是感知的,但在我未執行 add 前,git 認爲我本地代碼的狀態仍舊是 up-to-date with 'origin/master' 。同時 git 提示,個人修改還沒有 staged for commit ,由於只有 add 後才能 commit ,因此 git 給出的結論爲 no changes added to commit 。
D:\myGIT\modb>
D:\myGIT\modb>git log
commit 136a5da602fbba228c51cb7f680f1784bea1e6af
Author: moooofly <centos.sf@gmail.com>
Date: Mon Apr 28 15:11:53 2014 +0800
add helloworld.txt
commit 8fb8c6358323d3213b244355fa0e9df0e28a3b0d
Author: 摩雲飛 <centos.sf@gmail.com>
Date: Thu Jan 2 18:23:10 2014 +0800
Initial commit
D:\myGIT\modb>
此時查看 log 信息,能夠看到僅有最初建立和剛剛添加 helloworld.txt 文件時的日誌內容。
再次執行 add 和 commit 命令,並查看相關狀態信息。
D:\myGIT\modb>
D:\myGIT\modb>git add .
D:\myGIT\modb>git commit -m "add string in helloworld.txt"
[master 1c01bff] add string in helloworld.txt
1 file changed, 1 insertion(+)
D:\myGIT\modb>
D:\myGIT\modb>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
D:\myGIT\modb>
上述打印能夠看出,個人本地代碼版本已經超前 'origin/master' 分支 1 個 commit 了。此時已經沒有其餘須要 commit 的修改,只須要執行 push 操做將本地修改推到 GIT 服務器端。
D:\myGIT\modb>git log
commit 1c01bff84483507b428eecd4fff7bbe89467dcce
Author: moooofly <centos.sf@gmail.com>
Date: Mon Apr 28 16:38:16 2014 +0800
add string in helloworld.txt
commit 136a5da602fbba228c51cb7f680f1784bea1e6af
Author: moooofly <centos.sf@gmail.com>
Date: Mon Apr 28 15:11:53 2014 +0800
add helloworld.txt
commit 8fb8c6358323d3213b244355fa0e9df0e28a3b0d
Author: 摩雲飛 <centos.sf@gmail.com>
Date: Thu Jan 2 18:23:10 2014 +0800
Initial commit
D:\myGIT\modb>
從上述打印能夠知道,只要執行過 commit 就會在 log 中體現出來。
此時不執行 push 動做,而是再次修改文件的內容,增長
hello moooofly! haha!
以後查看狀態
D:\myGIT\modb>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
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: helloworld.txt
no changes added to commit (use "git add" and/or "git commit -a")
D:\myGIT\modb>
果真……同時出現了讓我將(前面的)commit 進行 push 和將(後面的)修改 staged for commit 的建議。
這裏我選擇執行 add 命令,結果出現了關於行結束的警告,這個暫時跳過不處理。
D:\myGIT\modb>git add .
warning: LF will be replaced by CRLF in helloworld.txt.
The file will have its original line endings in your working directory.
D:\myGIT\modb>
執行 status 命令,發現有新的修改須要 be committed ,或者也可使用 git reset HEAD helloworld.txt 將已經處於 staged 狀態的修改回退到 unstage 狀態。
D:\myGIT\modb>git status
warning: LF will be replaced by CRLF in helloworld.txt.
The file will have its original line endings in your working directory.
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: helloworld.txt
D:\myGIT\modb>
這裏執行 reset 操做進行回退。
D:\myGIT\modb>git reset HEAD helloworld.txt
warning: LF will be replaced by CRLF in helloworld.txt.
The file will have its original line endings in your working directory.
Unstaged changes after reset:
M helloworld.txt
D:\myGIT\modb>
能夠看到,文件 helloworld.txt 已經回退到 Unstaged 狀態。
從新查看 status 信息,發現狀態回到了讓我將 commit 進行 push 和將修改 staged for commit 的狀態。
D:\myGIT\modb>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
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: helloworld.txt
no changes added to commit (use "git add" and/or "git commit -a")
D:\myGIT\modb>
D:\myGIT\modb>git add .
warning: LF will be replaced by CRLF in helloworld.txt.
The file will have its original line endings in your working directory.
D:\myGIT\modb>
D:\myGIT\modb>git commit -m "add string 2 in helloworld.txt"
[master warning: LF will be replaced by CRLF in helloworld.txt.
The file will have its original line endings in your working directory.
793216b] add string 2 in helloworld.txt
warning: LF will be replaced by CRLF in helloworld.txt.
The file will have its original line endings in your working directory.
1 file changed, 2 insertions(+), 1 deletion(-)
D:\myGIT\modb>
D:\myGIT\modb>git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
D:\myGIT\modb>
能夠看出,此時本地版本已經處於領先於 origin/master 2 次 commit 的狀態。
執行 push 命令將 2 次 commit 進行提交。
D:\myGIT\modb>git push origin master
注:關於 「warning: LF will be replaced by CRLF」 的問題能夠參考《GIT 使用時遇到的行結束符設置問題》。