美文网首页GitAndroid知识
Git 常用命令小结

Git 常用命令小结

作者: craneyuan | 来源:发表于2018-05-08 07:47 被阅读13次

前言

阅读本篇文章需要读者了解 Git 的基本用法,如果你对 Git 完全不了解,请先行移步了解 Git 基础。
下面是几份本人觉得不错的 Git 入门教程,新手可以参考。

创建新分支,并且切换到新分支

> git checkout -b <new_branch_name> # 根据当前所在分支,创建新分支
> git checkout -b <new_branch_name> <remote_name>/<remote_branch_name> # 根据远程分支,创建分支

切换分支

> git checkout <branch_name>

删除分支(本地/远程)

> git branch -d <branch_name> # 删除本地分支,当该分支没有关联远程分支或者关联的远程分支已经合并过,才会被允许删除
> git branch -D <branch_name> # 强制删除本地分支
> git push <remote_name> -d <branch_name> # 删除远程分支,git v1.7.0(2010年的版本)之后支持
> git push <remote_name> :<branch_name> # 删除远程分支,旧方式,新的也兼容这种

分支重命名

> git branch (-m | -M) [<oldbranch>] <newbranch> # 重命名分支语法,-M 强制重命名,具体参见 git branch --help
> git branch -m <newbranch> # 重命名当前分支
> git branch -m <oldbranch> <newbranch> # 重命名指定分支

重写 commit 信息

> git rebase -i HEAD~<num> # 交互式地重写 commit 信息,将会用终端默认的编辑器进行操作

下面的例子中,保存之后,将会使得[328f67b Update Rust]这一条合并到[f55b189 Update cookbook]

pick 164bf1c Update cookbook
pick f55b189 Update cookbook
f 328f67b Update Rust
pick 9834843 Update cookbook

# Rebase 0b6762c..9834843 onto 0b6762c (4 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

注意:最好不要重写公共的 commit 信息,这会给协作者带来不必要的困惑,推荐仅仅重写本地没有提交的 commit 信息。假设,你非得重写远程 commit 信息,请使用 git push -f 来强制更新远程代码。

从一个分支摘取 commit 到另一个分支

> git checkout <target-branch> # 切换到目标分支
> git cherry-pick <commit_id> # 将源分支的 commit 摘取到目的分支中

想要切换分支时,发现本地有一些写了一半的代码

> git stash # 将当前工作目录内容储藏
> git stash --include-untracked # 如果新添加了文件,将其一并储藏
> git stash pop # 将储藏的内容恢复到当前分支

回版、撤销commit

> git reset --hard <commit_id> # 彻底回退到指定 commit
git 回版图解

丢弃本地新添加的文件

> git clean 

丢弃新的改动

> git checkout . # 注意末尾有个句号

参考资料

相关文章

  • Git&Github入门教程笔记(4)之分支管理一

    八 、分支管理(一) 先列出以此小结常用命令: git branch #查看分支git branch + 名 #创...

  • GIT常用命令

    GIT常用命令(小结) 谈及源码管理工具,首选SVN和GIT,在工作中也都有所接触,但个人更喜欢使用GIT,下面就...

  • git相关教程汇总

    1. git常用命令 git常用命令总结git常用命令讲解 2. git教程相关网站 廖雪峰的git教程猴子都能懂...

  • git命令整理

    git常用命令 GIT常用命令备忘:http://stormzhang.com/git/2014/01/27/gi...

  • git操作

    Git原理 Git常用命令

  • Git 常用命令及应用这一篇就够了(新手向)

    1. git 常用命令 1.1 常用命令 1.2 git remote 管理远程仓库 1.3 git r...

  • Git 常用操作

    常用命令图: 常用命令 查看本地、远端、全部分支 git branch git branch -r git bra...

  • Git 常用命令详解

    @[TOC](Git 常用命令详解) 1. Git 常用命令 1.1 常用git 命令图表汇总 1.2 配置个人信...

  • git 命令

    Git常用命令总结Git常用命令总结 git init 在本地新建一个repo,进入一个项目目录,执行git ...

  • 实习日记2:git代码管理

    mac:brew install git创建 常用命令:git常用命令及详解[https://blog.csdn....

网友评论

    本文标题:Git 常用命令小结

    本文链接:https://www.haomeiwen.com/subject/yixirftx.html