您的位置:首页 > 其它

git仓库的基本操作

2015-11-09 14:24 239 查看
1.创建仓库
git init 一般创建本地仓库使用
git init - -bare 初始化裸仓库,服务器上残酷推荐建立裸仓库,不能执行一些基本的git操作
2. 拷贝远程仓库
git clone [仓库地址] -b [远程分支名称](可选)
clone之后,git会讲远程仓库的名称用别名 origin 表示
3. git add
git commit
git push origin [本地分支名称]
比如: git push origin branch-test ( branch-test为本地分支名称)

origin只相当于一个别名,代表的是远程的仓库,不是分支。运行git remote –v或者查看.git/config可以看到origin的含义

git push 的完整语法为: git push [<repository>] [<refspec......>]

repository 为远程仓库, 上面的例子中为: origin

refspec.... 为分支信息 +<src>:<dst> src为本地分支。dst为远程分支。 git push origin master中省略了 dst。默认时和local branch名字一样。

4. 创建分支
创建本地分支
git branch [local name] 跟踪默认的远程分支
git branch[local name] [remote name] 创建跟踪指定远程分支的本地分支
删除本地分支
git branch -D [本地分支名称]
创建远程分支
git push origin[本地分支名称] (将本地分支推送到远程分支,也就创建了远程)
删除远程分支
git push origin :[远程分支名称]
git push origin :refs/for/test-remote (:test-remote为远程分支)注意分号的位置

参考:Git 的origin和master分析

master其实是一个“refspec”,正常的“refspec”的形式为”+<src>:<dst>”,冒号前表示local
branch的名字,冒号后表示remote repository下 branch的名字。注意,如果你省略了<dst>,git就认为你想push到remote
repository下和local branch相同名字的branch。听起来有点拗口,再解释下,push是怎么个push法,就是把本地branch指向的commit
push到remote repository下的branch,比如:

$git push origin master:master (在local repository中找到名字为master的branch,使用它去更新remote
repository下名字为master的branch,如果remote repository下不存在名字是master的branch,那么新建一个)

$git push origin master (省略了<dst>,等价于“git push origin master:master”)

$git push origin master:refs/for/mybranch (在local repository中找到名字为master的branch,用他去更新remote
repository下面名字为mybranch的branch)

$git push origin HEAD:refs/for/mybranch
(HEAD指向当前工作的branch,master不一定指向当前工作的branch,所以我觉得用HEAD还比master好些)

$git push origin :mybranch (再origin repository里面查找mybranch,删除它。用一个空的去更新它,就相当于删除了)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: