git 的使用

git 似乎比svn 更简单方便了

************************************************************

git 的使用
************************************************************


-------------------------------------------------
1. 创建版本库 Creating a Repository
-------------------------------------------------
prompt> mkdir mysite
prompt> cd mysite
prompt> git init

生成 .git 目录


-------------------------------------------------
2. 代码修改
-------------------------------------------------
prompt> git add index.html
prompt> git status
prompt> git commit –m "add index.html"
prompt> git log

prompt> git commit --amend   // 可以对刚刚提交的说明进行修改

-------------------------------------------------
3. 理解并使用分支
-------------------------------------------------
prompt> git branch RB_1.0 master   ## 创建分支
prompt> git commit –a               ## 继续主分支上提交
prompt> git checkout RB_1.0           ## 检出分支
prompt> git status

prompt> git tag 1.0 RB_1.0           ## 为分支打标签
prompt> git tag

prompt> git checkout master            ## 检出主分支
prompt> git rebase RB_1.0            ## 与rb_1.0 分支合并
prompt> git branch –d RB_1.0        ## 删除分支


另外:
git clone带有两个参数:远程版本库的位置和存放该版本库的本地目录。
  第二个参数是可选的
 

注意: git 初始化时在远程仓库上要用git init --bare, 要加--bare 选项,否则本地仓库将不能推送到远程,提示如下错误:

remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.


这么多的错误提示! 解决办法,只要重新初始化远程库,加上--bare 选项, 这样本地仓库push时服务器会创建相应目录

而不会只是不加--bare 时的一个.git 隐含目录了.

总之,服务器端初始化加--bare选项, 客户端不用加该选项

-------------------------------------------------

与远程建立关联: git remote add

例如:

$ git remote add  origin [email protected]:/home/dell1/srv/integrate.git

然后可以push 代码

$ git push origin master

----------------------------------------
4. git 把某个分支的文件合并到另一个分支
----------------------------------------
场景:
git 上有两个分支由于IO不同这两个分支不能合并(不考虑用宏分开等方式),
在一个分支上修改了bug, 想把这个修改应用到另一个分支上.

用法:
1. 切换到目标分支
2. 输入命令:git checkout --patch    来源分支名称 文件路径

举例: 我要把master 分支 modButtonTbl/modButtonTbl.cpp 文件的修改应用到newIO分支
git checkout newIO
git checkout --patch master  modButtonTbl/modButtonTable.cpp

它会用diff命令列出现在文件和修改文件之间的差异,然后问是否合并.
Apply this hunk to index and worktree [y,n,q,a,d,j,J,g,/,e,?]?

输入y 确认修改,
可能会有多处差异, 由你确定是否应用更改.
这样就把文件更新了.

然后你就可以用git st查看状态,确认修改及是否提交等其它操作.


----------------------------------------
5: 查看未暂存或已经暂存的文件变化.
----------------------------------------
git diff
git diff --cached

补充一下我的一个工作git 仓库 allinOne.git  它在如下这个位置,用git clone 来取出, 查看其分支.

 git clone [email protected]:/home/dell1/srv/allInOne.git

 git branch -r
  origin/HEAD -> origin/master
  origin/master
  origin/mobis
  origin/motorWindow
  origin/newIO

git 查看上游分支位置: $ git remote -v

git 查看所有分支: $ git branch --all

你可能感兴趣的:(#,svn和git,git,svn)