Git 常用命令

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 local_branch:remote_branch

local_branch必须为你本地存在的分支,remote_branch为远程分支,如果remote_branch不存在则会自动创建分支。


删除远程分支
$ 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 .

 

标签

 

    # 列出所有tag
    $ git tag

    # 新建一个tag在当前commit
    $ git tag [tag]

    # 新建一个tag在指定commit
    $ git tag [tag] [commit]

    # 删除本地tag
    $ git tag -d [tag]

    # 删除远程tag
    $ git push origin :refs/tags/[tagName]

    # 查看tag信息
    $ git show [tag]

    # 提交指定tag
    $ git push [remote] [tag]

    # 提交所有tag
    $ git push [remote] --tags

    # 新建一个分支,指向某个tag
    $ git checkout -b [branch] [tag]

 

 

 

设置命令别名

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

或者在~/.gitconfig 中添加

[alias]
    co = checkout
    ss = status
    cm = commit -m
    br = branch
    bm = branch -m
    unstage = reset HEAD --
    bd = branch -D
    cb = checkout -b
    df = diff
    ls = log --stat
    lp = log -p
    plo = pull origin
    plode = pull origin develop
    pho = push origin
    mg = merge

 

posted @ 2017-09-28 21:20  NewQ  阅读(171)  评论(0编辑  收藏  举报