码迷,mamicode.com
首页 > 其他好文 > 详细

Git 常用命令

时间:2017-09-28 22:29:54      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:blog   nbsp   去除   lin   col   自动   name   com   line   

git中fetch和pull的区别

 

分支命令

基于远程跟踪分支创建本地分支

$ git checkout -b refactored origin/refactored

 

列出所有本地分支
$ git branch

列出所有远程分支
$ git branch -r
.git/refs/head/[本地分支] .git/refs/remotes/[正在跟踪的分支]

查看所有本地分支和远程分支
$ git branch -a

查看分支,带提交信息
$ git branch -v

查看跟踪分支
$ git branch -vv

删除分支
$ git branch -d [branch-name]

删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

 

代码回滚

本地代码库回滚
git reset --hard commit-id :回滚到commit-id,将commit-id之后提交的commit都去除
git reset --hard HEAD~3:将最近3次的提交回滚

 

远程代码库回滚
应用场景:自动部署系统发布后发现问题,需要回滚到某一个commit,再重新发布
原理:先将本地分支退回到某个commit,删除远程分支,再重新push本地分支
操作步骤:
1、git checkout the_branch
2、git pull
3、git branch the_branch_backup //备份一下这个分支当前的情况
4、git reset --hard the_commit_id //把the_branch本地回滚到the_commit_id
5、git push origin :the_branch //删除远程 the_branch
6、git push origin the_branch //用回滚后的本地分支重新建立远程分支
7、git push origin :the_branch_backup //如果前面都成功了,删除这个备份分支

 

暂存命令

$ git stash
 
查看暂存内容
$ git stash list
 
恢复的同时把stash内容也删了
$ git stash pop

 

撤销

# 恢复某个commit的指定文件到暂存区和工作区
$ git checkout [commit] [file]

# 恢复暂存区的所有文件到工作区
$ git checkout .

 

其他

设定命令别名
$ git config --global alias.<aliasname> <command>

 

Git 常用命令

标签:blog   nbsp   去除   lin   col   自动   name   com   line   

原文地址:http://www.cnblogs.com/tonyq/p/7608643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!