文档章节

A Visual Git Reference

SVD
 SVD
发布于 2015/12/05 10:45
字数 1555
阅读 113
收藏 0

This page gives brief, visual reference for the most common commands in git. Once you know a bit about how git works, it may solidify your understanding.

Basic Usage

The four commands above copy files between the working directory, the stage (also called the index), and the history (in the form of commits).

  • git add files copies files (at their current state) to the stage.

  • git commit saves a snapshot of the stage as a commit.

  • git reset -- files unstages files; that is, it copies files from the latest commit to the stage. Use this command to "undo" a git add files. You can also git reset to unstage everything.

  • git checkout -- files copies files from the stage to the working directory. Use this to throw away local changes.

You can use git reset -p, git checkout -p, or git add -p instead of (or in addition to) specifying particular files to interactively choose which hunks copy.

It is also possible to jump over the stage and check out files directly from the history or commit files without staging first.

  • git commit -a is equivalent to running git add on all filenames that existed in the latest commit, and then running git commit.

  • git commit files creates a new commit containing the contents of the latest commit, plus a snapshot of files taken from the working directory. Additionally, files are copied to the stage.

  • git checkout HEAD -- files copies files from the latest commit to both the stage and the working directory.

Conventions

In the rest of this document, we will use graphs of the following form.

Commits are shown in green as 5-character IDs, and they point to their parents. Branches are shown in orange, and they point to particular commits. The current branch is identified by the special reference HEAD, which is "attached" to that branch. In this image, the five latest commits are shown, with ed489 being the most recent. master (the current branch) points to this commit, while maint (another branch) points to an ancestor of master's commit.

Commands in Detail

Diff

There are various ways to look at differences between commits. Below are some common examples. Any of these commands can optionally take extra filename arguments that limit the differences to the named files.

Commit

When you commit, git creates a new commit object using the files from the stage and sets the parent to the current commit. It then points the current branch to this new commit. In the image below, the current branch is master. Before the command was run, master pointed to ed489. Afterward, a new commit, f0cec, was created, with parent ed489, and then master was moved to the new commit.

This same process happens even when the current branch is an ancestor of another. Below, a commit occurs on branch maint, which was an ancestor of master, resulting in 1800b. Afterward, maint is no longer an ancestor of master. To join the two histories, a merge (or rebase) will be necessary.

Sometimes a mistake is made in a commit, but this is easy to correct with git commit --amend. When you use this command, git creates a new commit with the same parent as the current commit. (The old commit will be discarded if nothing else references it.)


A fourth case is committing with a detached HEAD, as explained later.

Checkout

The checkout command is used to copy files from the history (or stage) to the working directory, and to optionally switch branches.

When a filename (and/or -p) is given, git copies those files from the given commit to the stage and the working directory. For example, git checkout HEAD~ foo.c copies the file foo.c from the commit called HEAD~ (the parent of the current commit) to the working directory, and also stages it. (If no commit name is given, files are copied from the stage.) Note that the current branch is not changed.

When a filename is not given but the reference is a (local) branch, HEAD is moved to that branch (that is, we "switch to" that branch), and then the stage and working directory are set to match the contents of that commit. Any file that exists in the new commit (a47c3 below) is copied; any file that exists in the old commit (ed489) but not in the new one is deleted; and any file that exists in neither is ignored.

When a filename is not given and the reference is not a (local) branch — say, it is a tag, a remote branch, a SHA-1 ID, or something like master~3 — we get an anonymous branch, called a detached HEAD. This is useful for jumping around the history. Say you want to compile version 1.6.6.1 of git. You can git checkout v1.6.6.1 (which is a tag, not a branch), compile, install, and then switch back to another branch, say git checkout master. However, committing works slightly differently with a detached HEAD; this is covered below.

Committing with a Detached HEAD

When HEAD is detached, commits work like normal, except no named branch gets updated. (You can think of this as an anonymous branch.)

Once you check out something else, say master, the commit is (presumably) no longer referenced by anything else, and gets lost. Note that after the command, there is nothing referencing 2eecb.

If, on the other hand, you want to save this state, you can create a new named branch using git checkout -b name.

Reset

The reset command moves the current branch to another position, and optionally updates the stage and the working directory. It also is used to copy files from the history to the stage without touching the working directory.

If a commit is given with no filenames, the current branch is moved to that commit, and then the stage is updated to match this commit. If --hard is given, the working directory is also updated. If --soft is given, neither is updated.

If a commit is not given, it defaults to HEAD. In this case, the branch is not moved, but the stage (and optionally the working directory, if --hard is given) are reset to the contents of the last commit.

If a filename (and/or -p) is given, then the command works similarly to checkout with a filename, except only the stage (and not the working directory) is updated. (You may also specify the commit from which to take files, rather than HEAD.)

Merge

A merge creates a new commit that incorporates changes from other commits. Before merging, the stage must match the current commit. The trivial case is if the other commit is an ancestor of the current commit, in which case nothing is done. The next most simple is if the current commit is an ancestor of the other commit. This results in a fast-forward merge. The reference is simply moved, and then the new commit is checked out.

Otherwise, a "real" merge must occur. You can choose other strategies, but the default is to perform a "recursive" merge, which basically takes the current commit (ed489 below), the other commit (33104), and their common ancestor (b325c), and performs a three-way merge. The result is saved to the working directory and the stage, and then a commit occurs, with an extra parent (33104) for the new commit.

Cherry Pick

The cherry-pick command "copies" a commit, creating a new commit on the current branch with the same message and patch as another commit.

Rebase

A rebase is an alternative to a merge for combining multiple branches. Whereas a merge creates a single commit with two parents, leaving a non-linear history, a rebase replays the commits from the current branch onto another, leaving a linear history. In essence, this is an automated way of performing several cherry-picks in a row.

The above command takes all the commits that exist in topic but not in master (namely 169a6 and 2c33a), replays them onto master, and then moves the branch head to the new tip. Note that the old commits will be garbage collected if they are no longer referenced.

To limit how far back to go, use the --onto option. The following command replays onto master the most recent commits on the current branch since 169a6 (exclusive), namely 2c33a.

Technical Notes

The contents of files are not actually stored in the index (.git/index) or in commit objects. Rather, each file is stored in the object database (.git/objects) as a blob, identified by its SHA-1 hash. The index file lists the filenames along with the identifier of the associated blob, as well as some other data. For commits, there is an additional data type, a tree, also identified by its hash. Trees correspond to directories in the working directory, and contain a list of trees and blobs corresponding to each filename within that directory. Each commit stores the identifier of its top-level tree, which in turn contains all of the blobs and other trees associated with that commit.

If you make a commit using a detached HEAD, the last commit really is referenced by something: the reflog for HEAD. However, this will expire after a while, so the commit will eventually be garbage collected, similar to commits discarded with git commit --amend or git rebase.


本文转载自:https://marklodato.github.io/visual-git-guide/index-en.html

上一篇: 张量
SVD

SVD

粉丝 34
博文 220
码字总数 116831
作品 0
海淀
私信 提问
加载中

评论(1)

SVD
SVD 博主
我用git add file添加文件时出现这样错误:

fatal: Not a git repository (or any of the parent directories): .git

提示说没有.git这样一个目录,解决办法如下:

git init就可以了!
来自 Github 的图形化 Git 使用教程

这是来自 Github 上对 Git 常用操作进行简短介绍以及可视化图形操作说明的文档,如果你已经了解了 Git 工作原理,那么这个文档会加强你的理解。 该文档都是使用如下图形方式进行介绍: 现在访...

oschina
2012/06/28
9.7K
5
git-for-windows 2.16.0 发布,添加新特性

git-for-windows 2.16.0 发布。 更新内容: 新特性: Comes with Git Credential Manager v1.14.0. The Git for Windows installer now offers to configure Visual Studio Code as default......

淡漠悠然
2018/01/19
1K
2
libgit2 v0.21.3 发布,Git 开发包

libgit2 v0.21.3 发布,此版本是个维护版本,现已提供下载。 此版本包括一个关键的安全问题修复,强烈建议所有用户都更新! e476e7b index tests: test capitalization before mkdir 90fad0...

oschina
2014/12/19
928
1
Visual Studio Code 0.5.0 版本发布

微软的编程软件 Visual Studio Code 由 0.3.0 更新到 0.5.0 版本,下载地址: https://code.visualstudio.com/Download 详细改进内容请看: https://code.visualstudio.com/updates 值得关注...

黄者
2015/07/07
4.7K
19
Visual Studio 外部工具中添加 git-bash

引言 旧版的 Visual Studio 没有集成 工具,并且自己也习惯用 命令行来操作, 那么如何在旧版的 Visual Studio 快速打开 终端呢? 使用外部工具 Tips: 请确保你已经安装了[git],并添加到 Window...

taadis
2018/09/30
0
0

没有更多内容

加载失败,请刷新页面

加载更多

聊聊spring cloud的ConsulServiceRegistry

序 本文主要研究一下spring cloud的ConsulServiceRegistry ServiceRegistry spring-cloud-commons-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/client/serviceregistry/ServiceR......

go4it
9分钟前
0
0
Nextjs 学习 —— hooks

22

lemos
18分钟前
0
0
如何在spring mvc restful接口中定制化类型转换和格式化

1.痛点 最近小胖哥搞了个小程序,有几个spring mvc 接口传递了时间,时间用java 8 time 相关的api 来直接接收: 当使用根据ISO 8601格式化的参数向任何这些方法发送POST请求时,报出了如下异...

码农小胖哥
32分钟前
2
0
docker日志监控

日志处理机制 我们先来了解一下docker日志处理的机制,当启动一个容器的时候,它其实是docker daemon的一个子进程,docker daemon可以拿到你容器里面进程的标准输出,拿到标准输出后,它会通...

爱宝贝丶
33分钟前
2
0
域名已备案,但jsapi添加支付授权目录依然提示支付授权目录未通过ICP备案!!!

问题描述: 一网站域名xxx.cn已备案成功,在全国工业和信息化部(http://beian.miit.gov.cn/publish/query/indexFirst.action)查询到备案成功的,但是在微信商户平台配置jsapi添加支付授权目录...

闊苡訆涐囍醣
38分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部