{"id":23777,"date":"2023-05-10T03:32:11","date_gmt":"2023-05-10T03:32:11","guid":{"rendered":"https:\/\/www.booksofall.com\/cn\/?post_type=product&#038;p=23777"},"modified":"2023-05-10T03:32:12","modified_gmt":"2023-05-10T03:32:12","slug":"a-visual-git-reference","status":"publish","type":"product","link":"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/","title":{"rendered":"A Visual Git Reference"},"content":{"rendered":"<h2 id=\"basic-usage\">Basic Usage<\/h2>\n<div class=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/marklodato.github.io\/visual-git-guide\/basic-usage.svg\" width=\"728\" height=\"312\" \/><\/div>\n<p>The four commands above copy files between the working directory, the stage (also called the index), and the history (in the form of commits).<\/p>\n<ul>\n<li><code>git add\u00a0<em>files<\/em><\/code>\u00a0copies\u00a0<em>files<\/em>\u00a0(at their current state) to the stage.<\/li>\n<li><code>git commit<\/code>\u00a0saves a snapshot of the stage as a commit.<\/li>\n<li><code>git reset --\u00a0<em>files<\/em><\/code>\u00a0unstages files; that is, it copies\u00a0<em>files<\/em>\u00a0from the latest commit to the stage. Use this command to &#8220;undo&#8221; a\u00a0<code>git add\u00a0<em>files<\/em><\/code>. You can also\u00a0<code>git reset<\/code>\u00a0to unstage everything.<\/li>\n<li><code>git checkout --\u00a0<em>files<\/em><\/code>\u00a0copies\u00a0<em>files<\/em>\u00a0from the stage to the working directory. Use this to throw away local changes.<\/li>\n<\/ul>\n<p>You can use\u00a0<code>git reset -p<\/code>,\u00a0<code>git checkout -p<\/code>, or\u00a0<code>git add -p<\/code>\u00a0instead of (or in addition to) specifying particular files to interactively choose which hunks copy.<\/p>\n<p>It is also possible to jump over the stage and check out files directly from the history or commit files without staging first.<\/p>\n<div class=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/marklodato.github.io\/visual-git-guide\/basic-usage-2.svg\" width=\"866\" height=\"312\" \/><\/div>\n<ul>\n<li><code>git commit -a\u00a0<\/code>is equivalent to running\u00a0<tt>git add<\/tt>\u00a0on all filenames that existed in the latest commit, and then running\u00a0<tt>git commit<\/tt>.<\/li>\n<li><code>git commit\u00a0<em>files<\/em><\/code>\u00a0creates a new commit containing the contents of the latest commit, plus a snapshot of\u00a0<em>files<\/em>\u00a0taken from the working directory. Additionally,\u00a0<em>files<\/em>\u00a0are copied to the stage.<\/li>\n<li><code>git checkout HEAD --\u00a0<em>files<\/em><\/code>\u00a0copies\u00a0<em>files<\/em>\u00a0from the latest commit to both the stage and the working directory.<\/li>\n<\/ul>\n<h2 id=\"conventions\">Conventions<\/h2>\n<p>In the rest of this document, we will use graphs of the following form.<\/p>\n<div class=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/marklodato.github.io\/visual-git-guide\/conventions.svg\" width=\"831\" height=\"485\" \/><\/div>\n<p>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\u00a0<em>HEAD<\/em>, which is &#8220;attached&#8221; to that branch. In this image, the five latest commits are shown, with\u00a0<em>ed489<\/em>\u00a0being the most recent.\u00a0<em>main<\/em>\u00a0(the current branch) points to this commit, while\u00a0<em>stable<\/em>\u00a0(another branch) points to an ancestor of\u00a0<em>main<\/em>&#8216;s commit.<\/p>\n<h2 id=\"commands-in-detail\">Commands in Detail<\/h2>\n<h3 id=\"diff\">Diff<\/h3>\n<p>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.<\/p>\n<div class=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/marklodato.github.io\/visual-git-guide\/diff.svg\" width=\"831\" height=\"485\" \/><\/div>\n<h3 id=\"commit\">Commit<\/h3>\n<p>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\u00a0<em>main<\/em>. Before the command was run,\u00a0<em>main<\/em>\u00a0pointed to\u00a0<em>ed489<\/em>. Afterward, a new commit,\u00a0<em>f0cec<\/em>, was created, with parent\u00a0<em>ed489<\/em>, and then\u00a0<em>main<\/em>\u00a0was moved to the new commit.<\/p>\n<div class=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/marklodato.github.io\/visual-git-guide\/commit-main.svg\" width=\"831\" height=\"485\" \/><\/div>\n<p>This same process happens even when the current branch is an ancestor of another. Below, a commit occurs on branch\u00a0<em>stable<\/em>, which was an ancestor of\u00a0<em>main<\/em>, resulting in\u00a0<em>1800b<\/em>. Afterward,\u00a0<em>stable<\/em>\u00a0is no longer an ancestor of\u00a0<em>main<\/em>. To join the two histories, a\u00a0<a href=\"http:\/\/marklodato.github.io\/visual-git-guide\/index-en.html#merge\">merge<\/a>\u00a0(or\u00a0<a href=\"http:\/\/marklodato.github.io\/visual-git-guide\/index-en.html#rebase\">rebase<\/a>) will be necessary.<\/p>\n<div class=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/marklodato.github.io\/visual-git-guide\/commit-stable.svg\" width=\"831\" height=\"485\" \/><\/div>\n<p>Sometimes a mistake is made in a commit, but this is easy to correct with\u00a0<code>git commit --amend<\/code>. 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.)<\/p>\n<div class=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/marklodato.github.io\/visual-git-guide\/commit-amend.svg\" width=\"831\" height=\"485\" \/><\/div>\n<p>A fourth case is committing with a\u00a0<a href=\"http:\/\/marklodato.github.io\/visual-git-guide\/index-en.html#detached\">detached HEAD<\/a>, as explained later.<\/p>\n<h3 id=\"checkout\">Checkout<\/h3>\n<p>The checkout command is used to copy files from the history (or stage) to the working directory, and to optionally switch branches.<\/p>\n<p>When a filename (and\/or\u00a0<code>-p<\/code>) is given, git copies those files from the given commit to the stage and the working directory. For example,\u00a0<code>git checkout HEAD~ foo.c<\/code>\u00a0copies the file\u00a0<code>foo.c<\/code>\u00a0from the commit called\u00a0<em>HEAD~<\/em>\u00a0(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.<\/p>\n<div class=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/marklodato.github.io\/visual-git-guide\/checkout-files.svg\" width=\"831\" height=\"485\" \/><\/div>\n<p>When a filename is\u00a0<em>not<\/em>\u00a0given but the reference is a (local) branch,\u00a0<em>HEAD<\/em>\u00a0is moved to that branch (that is, we &#8220;switch to&#8221; 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 (<em>a47c3<\/em>\u00a0below) is copied; any file that exists in the old commit (<em>ed489<\/em>) but not in the new one is deleted; and any file that exists in neither is ignored.<\/p>\n<div class=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/marklodato.github.io\/visual-git-guide\/checkout-branch.svg\" width=\"831\" height=\"485\" \/><\/div>\n<p>When a filename is\u00a0<em>not<\/em>\u00a0given and the reference is\u00a0<em>not<\/em>\u00a0a (local) branch \u2014 say, it is a tag, a remote branch, a SHA-1 ID, or something like\u00a0<em>main~3<\/em>\u00a0\u2014 we get an anonymous branch, called a\u00a0<em>detached HEAD<\/em>. This is useful for jumping around the history. Say you want to compile version 1.6.6.1 of git. You can\u00a0<code>git checkout v1.6.6.1<\/code>\u00a0(which is a tag, not a branch), compile, install, and then switch back to another branch, say\u00a0<code>git checkout main<\/code>. However, committing works slightly differently with a detached HEAD; this is covered\u00a0<a href=\"http:\/\/marklodato.github.io\/visual-git-guide\/index-en.html#detached\">below<\/a>.<\/p>\n<div class=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/marklodato.github.io\/visual-git-guide\/checkout-detached.svg\" width=\"831\" height=\"485\" \/><\/div>\n","protected":false},"excerpt":{"rendered":"<p><iframe style=\"width: 100%; height: 750px; border: none;\" src=\"https:\/\/online.visual-paradigm.com\/share\/book\/a-visual-git-reference-1cmyxfndyk?p=1\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n","protected":false},"featured_media":23781,"template":"","meta":{"_yoast_wpseo_title":"","_yoast_wpseo_metadesc":"Feeling bored when learning Git with text? Try reading this Visual Git Reference and learn Git in an interesting way! Start learning now."},"product_brand":[],"product_cat":[386],"product_tag":[],"class_list":{"0":"post-23777","1":"product","2":"type-product","3":"status-publish","4":"has-post-thumbnail","6":"product_cat-version-control-systems","8":"first","9":"instock","10":"shipping-taxable","11":"product-type-simple"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>A Visual Git Reference - BooksOfAll Simplified Chinese<\/title>\n<meta name=\"description\" content=\"Feeling bored when learning Git with text? Try reading this Visual Git Reference and learn Git in an interesting way! Start learning now.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Visual Git Reference - BooksOfAll Simplified Chinese\" \/>\n<meta property=\"og:description\" content=\"Feeling bored when learning Git with text? Try reading this Visual Git Reference and learn Git in an interesting way! Start learning now.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/\" \/>\n<meta property=\"og:site_name\" content=\"BooksOfAll Simplified Chinese\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-10T03:32:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/05\/A-Visual-Git-Reference.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/05\/A-Visual-Git-Reference.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/\",\"url\":\"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/\",\"name\":\"A Visual Git Reference - BooksOfAll Simplified Chinese\",\"isPartOf\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/05\/A-Visual-Git-Reference.jpg\",\"datePublished\":\"2023-05-10T03:32:11+00:00\",\"dateModified\":\"2023-05-10T03:32:12+00:00\",\"description\":\"Feeling bored when learning Git with text? Try reading this Visual Git Reference and learn Git in an interesting way! Start learning now.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/#primaryimage\",\"url\":\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/05\/A-Visual-Git-Reference.jpg\",\"contentUrl\":\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/05\/A-Visual-Git-Reference.jpg\",\"width\":\"827\",\"height\":\"1169\",\"caption\":\"A Visual Git Reference\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.booksofall.com\/cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Categories\",\"item\":\"https:\/\/www.booksofall.com\/cn\/categories\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"A Visual Git Reference\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/#website\",\"url\":\"https:\/\/www.booksofall.com\/cn\/\",\"name\":\"BooksOfAll Simplified Chinese\",\"description\":\"Biggest IT eBooks library and learning resources - Free eBooks for programming, computing, artificial intelligence and more.\",\"publisher\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.booksofall.com\/cn\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/#organization\",\"name\":\"BooksOfAll Simplified Chinese\",\"url\":\"https:\/\/www.booksofall.com\/cn\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2022\/06\/booksofall-logo-2.png\",\"contentUrl\":\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2022\/06\/booksofall-logo-2.png\",\"width\":166,\"height\":30,\"caption\":\"BooksOfAll Simplified Chinese\"},\"image\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A Visual Git Reference - BooksOfAll Simplified Chinese","description":"Feeling bored when learning Git with text? Try reading this Visual Git Reference and learn Git in an interesting way! Start learning now.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/","og_locale":"zh_CN","og_type":"article","og_title":"A Visual Git Reference - BooksOfAll Simplified Chinese","og_description":"Feeling bored when learning Git with text? Try reading this Visual Git Reference and learn Git in an interesting way! Start learning now.","og_url":"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/","og_site_name":"BooksOfAll Simplified Chinese","article_modified_time":"2023-05-10T03:32:12+00:00","og_image":[{"url":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/05\/A-Visual-Git-Reference.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_image":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/05\/A-Visual-Git-Reference.jpg","twitter_misc":{"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/","url":"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/","name":"A Visual Git Reference - BooksOfAll Simplified Chinese","isPartOf":{"@id":"https:\/\/www.booksofall.com\/cn\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/#primaryimage"},"image":{"@id":"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/#primaryimage"},"thumbnailUrl":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/05\/A-Visual-Git-Reference.jpg","datePublished":"2023-05-10T03:32:11+00:00","dateModified":"2023-05-10T03:32:12+00:00","description":"Feeling bored when learning Git with text? Try reading this Visual Git Reference and learn Git in an interesting way! Start learning now.","breadcrumb":{"@id":"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/"]}]},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/#primaryimage","url":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/05\/A-Visual-Git-Reference.jpg","contentUrl":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/05\/A-Visual-Git-Reference.jpg","width":"827","height":"1169","caption":"A Visual Git Reference"},{"@type":"BreadcrumbList","@id":"https:\/\/www.booksofall.com\/cn\/a-visual-git-reference\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.booksofall.com\/cn\/"},{"@type":"ListItem","position":2,"name":"Categories","item":"https:\/\/www.booksofall.com\/cn\/categories\/"},{"@type":"ListItem","position":3,"name":"A Visual Git Reference"}]},{"@type":"WebSite","@id":"https:\/\/www.booksofall.com\/cn\/#website","url":"https:\/\/www.booksofall.com\/cn\/","name":"BooksOfAll Simplified Chinese","description":"Biggest IT eBooks library and learning resources - Free eBooks for programming, computing, artificial intelligence and more.","publisher":{"@id":"https:\/\/www.booksofall.com\/cn\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.booksofall.com\/cn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":"Organization","@id":"https:\/\/www.booksofall.com\/cn\/#organization","name":"BooksOfAll Simplified Chinese","url":"https:\/\/www.booksofall.com\/cn\/","logo":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.booksofall.com\/cn\/#\/schema\/logo\/image\/","url":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2022\/06\/booksofall-logo-2.png","contentUrl":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2022\/06\/booksofall-logo-2.png","width":166,"height":30,"caption":"BooksOfAll Simplified Chinese"},"image":{"@id":"https:\/\/www.booksofall.com\/cn\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/product\/23777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/product"}],"about":[{"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/types\/product"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/media\/23781"}],"wp:attachment":[{"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/media?parent=23777"}],"wp:term":[{"taxonomy":"product_brand","embeddable":true,"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/product_brand?post=23777"},{"taxonomy":"product_cat","embeddable":true,"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/product_cat?post=23777"},{"taxonomy":"product_tag","embeddable":true,"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/product_tag?post=23777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}