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





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