git的一些操作

~/rails_projects/first_app$
git config --global core.editor "subl -w"
如果使用其他編輯器,請使用以下代碼替換 subl -w:TextMate 用mate -w,gVim 用 gvim -f,MacVim 用 mvim -f。

$ git config --global alias.co checkout 創建別名( 可以不做)

下面的步驟你每次新建一個倉庫時都要執行。首先進入剛創建的應用程序的根目錄,然後初始化一個新倉庫:
$ git init
Initialized empty Git repository in/Users/mhartl/rails_projects/first_app/.git/
最後我們要把 Rails 項目中的文件添加到 git 中,然後提交結果。你可以使用下述命令添加所有的文件(除了 .gitignore 中忽略的文件):
$ git add .
#察看所有的分支,並且當前分支前面有*號
git branch
$ git status
用 commit 命令告訴 git 你想保存這些改動:
$ git commit -m "Initial commit"
順便說一下,你可以使用 log 命令查看提交的歷史信息:
$ git log
such As----》
刪除了 app/controllers/ 文件夾:
$ ls app/controllers/
application_controller.rb
$ rm -rf app/controllers/
$ ls app/controllers/
ls: app/controllers/: No such file or directory
查看一下狀態看看發生了什麼:
$ git status
# On branch master
# Changed but not updated:
#   (use "git add/rm<file>..." to update what will be committed)
#   (use "git checkout --<file>..." to discard changes in working directory)
#
#       deleted:    app/controllers/application_controller.rb
#
no changes added to commit (use "git add" and/or "git commit-a")
可以看到一個文件被刪除了,但是這個改動只發生在工作區,還沒有提交。這樣我們就可以使用 checkout 命令切換到前一個提交記錄來撤銷這次改動(其中旗標 -f 意思是覆蓋當前的改動):
$ git checkout -f
$ git status
# On branch master
nothing to commit (working directory clean)
$ ls app/controllers/
application_controller.rb
刪除的文件夾和文件又回來了,這下放心了!
將第一個應用程序推送上去:
$ git remote add origin [email protected]:<username>/first_app.git
$ git push -u origin master

分支
Git 中的分支功能很強大,分支是對倉庫的複製,在分支中所做的改動(或許是實驗性質的)不會影響父級文件。大多數情況下,父級倉庫是 master 分支。我們可以使用 checkout 命令,並指定 -b 旗標創建一個新分支:
$ git checkout -b modify-README
Switched to a new branch 'modify-README'
$ git branch
master
* modify-README
第二個命令,git branch,會將本地所有的分支列出來,分支名前面的星號(*)指明當前所在的分支。注意,git checkout -b modify-README會創建一個新分支,然後切換到這個分支,modify-README 前面的星號證明了這一點。
編輯
創建了從分支後,我們要編輯文件讓其更好的描述我們的項目。較之默認的 RDoc 格式,我更喜歡 Markdown 標記語言,如果文件擴展名是 .md,GitHub 會自動爲你排版。首先我們使用 Unix 命令 mv(移動,move)的 git 版本來修改文件名,然後寫入代碼 1.8 所示的內容:
$ git mv README.rdoc README.md
$ subl README.md
但是 Git 提供了旗標 -a,它的意思是將現有文件的所有改動(包括使用 git mv 創建的文件,對 Git 來說這並不是新的文件)添加進來:
$ git commit -a -m "Improve the README file"
2 files changed, 5 insertions(+), 243 deletions(-)
delete mode 100644 README.rdoc
create mode 100644 README.md
如前面提到的,你可以使用 git branch -D 放棄對從分支所做的修改:
# For illustration only; don't do this unless you mess up a branch
$ git checkout -b topic-branch
$ <really screw up the branch>
$ git add .
$ git commit -a -m "Major screw up"
$ git checkout master

git push origin --delete second_period刪除遠端分支
$ git branch -D topic-branch
分支的切換
   # 切換到Master分支
git checkout master
# 對Develop分支進行合併
git merge --no-ff develop


推送
我們已經更新了 README 文件,可以將改動推送到GitHub 看看改動的結果。因爲之前我們已經推送過一次了(1.3.4 節),在大多數系統中我們都可以省略 origin master,只要運行 git push:
$ git push





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章