您的位置:首页 > 其它

git常用操作命令

2016-03-24 18:55 344 查看
自动换到新部门后,每晚九点到十点的正常下班,也没多少时间做整理。虽然事情多,空闲时间少了,但这不是不做总结的借口。废话不多说,先从一篇简单的开始吧!关于git命令原理相关的介绍网上 一大堆,这里只是整理下自己平常工作中用到的最多的几个命令,做做笔记。

1.新建分支

新建分支并切换到新建的分支

git checkout -b hotfix_newbranch_20150417
推送本地的分支到远端
git push -u origin hotfix_newbranch_20150417
git push --set-upstream origin hotfix_newbranch_20150417
切换到指定分支

git checkout develop_new

2.添加与提交

将代码添加到暂存区(index)

git add *

将代码提交到本地仓库的 HEAD,但是还未提交到远端仓库

git commit -m "备注代码提交信息"

3.推送改动到远端

将代码推送到远端的仓库

git push origin hotfix_newbranch_20150417 (后面的分支号不填的话会推送到当前分支的远端)

4.删除分支

删除本地分支

git branch -d hotfix_newbranch_20150417

删远程分支 (本地没有对应分支的情况)

git branch -r -d origin/hotfix_20150320

git push origin :hotfix_20150320

删远程分支 (本地已有分支的情况)

git push origin --delete feature_appActivity_20150423

5.合并与更新

分支更新

git pull

合并分支

比如,如果要将开发中的分支(develop),合并到稳定分支(master)

首先切换的master分支:git checkout master

然后执行合并操作:git merge origin/develop或 git merge origin/develop -X renormalize

如果合并有冲突,解决冲突,执行 git add . 然后执行 git push origin develop_new提交到远端。

6.替换本地改动

使用 HEAD 中的最新内容替换掉你的工作目录中的指定文件

git checkout -- <file>

假如你想丢弃你在本地的所有改动与提交,可以到服务器上获取最新的版本历史,并将你本地主分支指向它:

git fetch origin

git reset --hard origin/master

分支回退到指定的提交版本,0c509beee4a8fc3f14a90cf4b为代码提交的commit id

git reset --hard 0c509beee4a8fc3f14a90cf4b

git push -f

7.其他

随时查看当前分支的状态

git status

git 提交记录

git log

内建的图形化 git

git gitk

备份当前工作区的内容

git stash

git stash pop: 从Git栈中读取最近一次保存的内容

git stash list: 显示Git栈内的所有备份

git stash clear: 清空Git栈

git stash apply stash@{1}:恢复指定版本号的内容

参考链接:http://rogerdudler.github.io/git-guide/index.zh.html
http://www.kancloud.cn/kancloud/igit/46729
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: