CINXE.COM

Git - Recording Changes to the Repository

<!DOCTYPE html> <html lang="ms"> <head> <meta charset='utf-8'> <meta content='IE=edge,chrome=1' http-equiv='X-UA-Compatible'> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Git - Recording Changes to the Repository</title> <link href="/favicon.ico" rel='shortcut icon' type='image/x-icon'> <link rel="stylesheet" href="/application.min.css"> <script src="/js/modernizr.js"></script> <script src="/js/modernize.js"></script> </head> <body id="documentation"> <div class="inner"> <header> <a href="/"><img src="/images/logo@2x.png" width="110" height="46" alt="Git" /></a> <span id="tagline"></span> <script type="text/javascript"> const taglines = [ "fast-version-control", "everything-is-local", "distributed-even-if-your-workflow-isnt", "local-branching-on-the-cheap", "distributed-is-the-new-centralized" ]; var tagline = taglines[Math.floor(Math.random() * taglines.length)]; document.getElementById('tagline').innerHTML = '--' + tagline; </script> <form id="search" action="/search/results"> <input id="search-text" name="search" placeholder="Type / to search entire site…" autocomplete="off" type="text" /> </form> <div id="search-results"></div> </header> </div> <div class="inner"> <div id="content-wrapper"> <div tabindex="1" class="sidebar-btn"></div> <aside class="sidebar" id="sidebar"> <nav> <ul> <li> <a href="/about">About</a> <ul> </ul> </li> <li> <a href="/doc" class="active">Documentation</a> <ul class="expanded"> <li> <a href="/docs">Reference</a> </li> <li> <a href="/book" class="active">Book</a> </li> <li> <a href="/videos">Videos</a> </li> <li> <a href="/doc/ext">External Links</a> </li> </ul> </li> <li> <a href="/downloads">Downloads</a> <ul > <li> <a href="/downloads/guis">GUI Clients</a> </li> <li> <a href="/downloads/logos">Logos</a> </li> </ul> </li> <li> <a href="/community">Community</a> </li> </ul> <hr class="sidebar"> <p> This book is available in <a href="/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository">English</a>. </p> <p> Full translation available in <table> <tr><td><a href="/book/az/v2/Git%e2%80%99in-%c6%8fsaslar%c4%b1-Depoda-D%c9%99yi%c5%9fiklikl%c9%99rin-Qeyd-Edilm%c9%99si">azərbaycan dili</a>,</td></tr> <tr><td><a href="/book/bg/v2/%d0%9e%d1%81%d0%bd%d0%be%d0%b2%d0%b8-%d0%bd%d0%b0-Git-%d0%97%d0%b0%d0%bf%d0%b8%d1%81-%d0%bd%d0%b0-%d0%bf%d1%80%d0%be%d0%bc%d0%b5%d0%bd%d0%b8-%d0%b2-%d1%85%d1%80%d0%b0%d0%bd%d0%b8%d0%bb%d0%b8%d1%89%d0%b5%d1%82%d0%be">български език</a>,</td></tr> <tr><td><a href="/book/de/v2/Git-Grundlagen-%c3%84nderungen-nachverfolgen-und-im-Repository-speichern">Deutsch</a>,</td></tr> <tr><td><a href="/book/es/v2/Fundamentos-de-Git-Guardando-cambios-en-el-Repositorio">Español</a>,</td></tr> <tr><td><a href="/book/fr/v2/Les-bases-de-Git-Enregistrer-des-modifications-dans-le-d%c3%a9p%c3%b4t">Français</a>,</td></tr> <tr><td><a href="/book/gr/v2/%ce%a4%ce%b1-%ce%b8%ce%b5%ce%bc%ce%b5%ce%bb%ce%b9%cf%8e%ce%b4%ce%b7-%cf%83%cf%84%ce%bf%ce%b9%cf%87%ce%b5%ce%af%ce%b1-%cf%84%ce%bf%cf%85-Git-%ce%9a%ce%b1%cf%84%ce%b1%ce%b3%cf%81%ce%b1%cf%86%ce%ae-%ce%b1%ce%bb%ce%bb%ce%b1%ce%b3%cf%8e%ce%bd-%cf%83%cf%84%ce%bf-%ce%b1%cf%80%ce%bf%ce%b8%ce%b5%cf%84%ce%ae%cf%81%ce%b9%ce%bf">Ελληνικά</a>,</td></tr> <tr><td><a href="/book/ja/v2/Git-%e3%81%ae%e5%9f%ba%e6%9c%ac-%e5%a4%89%e6%9b%b4%e5%86%85%e5%ae%b9%e3%81%ae%e3%83%aa%e3%83%9d%e3%82%b8%e3%83%88%e3%83%aa%e3%81%b8%e3%81%ae%e8%a8%98%e9%8c%b2">日本語</a>,</td></tr> <tr><td><a href="/book/ko/v2/Git%ec%9d%98-%ea%b8%b0%ec%b4%88-%ec%88%98%ec%a0%95%ed%95%98%ea%b3%a0-%ec%a0%80%ec%9e%a5%ec%86%8c%ec%97%90-%ec%a0%80%ec%9e%a5%ed%95%98%ea%b8%b0">한국어</a>,</td></tr> <tr><td><a href="/book/nl/v2/Git-Basics-Wijzigingen-aan-de-repository-vastleggen">Nederlands</a>,</td></tr> <tr><td><a href="/book/ru/v2/%d0%9e%d1%81%d0%bd%d0%be%d0%b2%d1%8b-Git-%d0%97%d0%b0%d0%bf%d0%b8%d1%81%d1%8c-%d0%b8%d0%b7%d0%bc%d0%b5%d0%bd%d0%b5%d0%bd%d0%b8%d0%b9-%d0%b2-%d1%80%d0%b5%d0%bf%d0%be%d0%b7%d0%b8%d1%82%d0%be%d1%80%d0%b8%d0%b9">Русский</a>,</td></tr> <tr><td><a href="/book/sl/v2/Osnove-Git-Snemanje-sprememb-v-repozitorij">Slovenščina</a>,</td></tr> <tr><td><a href="/book/tl/v2/Mga-Pangunahing-Kaalaman-sa-Git-Pagtatala-ng-mga-Pagbabago-sa-Repositoryo">Tagalog</a>,</td></tr> <tr><td><a href="/book/uk/v2/%d0%9e%d1%81%d0%bd%d0%be%d0%b2%d0%b8-Git-%d0%97%d0%b0%d0%bf%d0%b8%d1%81-%d0%b7%d0%bc%d1%96%d0%bd-%d0%b4%d0%be-%d1%80%d0%b5%d0%bf%d0%be%d0%b7%d0%b8%d1%82%d0%be%d1%80%d1%96%d1%8f">Українська</a></td></tr> <tr><td><a href="/book/zh/v2/Git-%e5%9f%ba%e7%a1%80-%e8%ae%b0%e5%bd%95%e6%af%8f%e6%ac%a1%e6%9b%b4%e6%96%b0%e5%88%b0%e4%bb%93%e5%ba%93">简体中文</a>,</td></tr> </table> </p> <p> Partial translations available in <table> <tr><td><a href="/book/cs/v2/Z%c3%a1klady-pr%c3%a1ce-se-syst%c3%a9mem-Git-Nahr%c3%a1v%c3%a1n%c3%ad-zm%c4%9bn-do-repozit%c3%a1%c5%99e">Čeština</a>,</td></tr> <tr><td><a href="/book/mk/v2/%d0%9e%d1%81%d0%bd%d0%be%d0%b2%d0%b8%d1%82%d0%b5-%d0%bd%d0%b0-Git-%d0%a1%d0%bd%d0%b8%d0%bc%d0%b0%d1%9a%d0%b5-%d0%bd%d0%b0-%d0%bf%d1%80%d0%be%d0%bc%d0%b5%d0%bd%d0%b8-%d0%b2%d0%be-%d1%81%d0%ba%d0%bb%d0%b0%d0%b4%d0%b8%d1%88%d1%82%d0%b5%d1%82%d0%be">Македонски</a>,</td></tr> <tr><td><a href="/book/pl/v2/Podstawy-Gita-Rejestrowanie-zmian-w-repozytorium">Polski</a>,</td></tr> <tr><td><a href="/book/sr/v2/%d0%9e%d1%81%d0%bd%d0%be%d0%b2%d0%b5-%d0%bf%d1%80%d0%be%d0%b3%d1%80%d0%b0%d0%bc%d0%b0-%d0%93%d0%b8%d1%82-%d0%a1%d0%bd%d0%b8%d0%bc%d0%b0%d1%9a%d0%b5-%d0%bf%d1%80%d0%be%d0%bc%d0%b5%d0%bd%d0%b0-%d0%bd%d0%b0%d0%b4-%d1%80%d0%b5%d0%bf%d0%be%d0%b7%d0%b8%d1%82%d0%be%d1%80%d0%b8%d1%98%d1%83%d0%bc%d0%be%d0%bc">Српски</a>,</td></tr> <tr><td><a href="/book/uz/v2/Git-%d0%b0%d1%81%d0%be%d1%81%d0%bb%d0%b0%d1%80%d0%b8-%d0%8e%d0%b7%d0%b3%d0%b0%d1%80%d0%b8%d1%88%d0%bb%d0%b0%d1%80%d0%bd%d0%b8-%d0%be%d0%bc%d0%b1%d0%be%d1%80%d0%b3%d0%b0-%d1%91%d0%b7%d0%b8%d1%88">Ўзбекча</a>,</td></tr> <tr><td><a href="/book/zh-tw/v2/Git-%e5%9f%ba%e7%a4%8e-%e7%b4%80%e9%8c%84%e8%ae%8a%e6%9b%b4%e5%88%b0%e7%89%88%e6%9c%ac%e5%ba%ab%e4%b8%ad">繁體中文</a>,</td></tr> </table> </p> <p> Translations started for <table> <tr><td><a href="/book/be/v2/Git-Basics-Recording-Changes-to-the-Repository">Беларуская</a>,</td></tr> <tr><td><a href="/book/fa/v2/%d9%85%d9%82%d8%af%d9%85%d8%a7%d8%aa-%da%af%db%8c%d8%aa-%d8%ab%d8%a8%d8%aa-%d8%aa%d8%ba%db%8c%db%8c%d8%b1%d8%a7%d8%aa-%d8%af%d8%b1-%d9%85%d8%ae%d8%b2%d9%86" dir="rtl">فارسی</a>,</td></tr> <tr><td><a href="/book/id/v2/Git-Basics-Recording-Changes-to-the-Repository">Indonesian</a>,</td></tr> <tr><td><a href="/book/it/v2/Git-Basics-Recording-Changes-to-the-Repository">Italiano</a>,</td></tr> <tr><td><a href="/book/ms/v2/Git-Basics-Recording-Changes-to-the-Repository">Bahasa Melayu</a>,</td></tr> <tr><td><a href="/book/pt-br/v2/Fundamentos-de-Git-Gravando-Altera%c3%a7%c3%b5es-em-Seu-Reposit%c3%b3rio">Português (Brasil)</a>,</td></tr> <tr><td><a href="/book/pt-pt/v2/No%c3%a7%c3%b5es-B%c3%a1sicas-do-Git-Recording-Changes-to-the-Repository">Português (Portugal)</a>,</td></tr> <tr><td><a href="/book/sv/v2/Grunder-i-Git-Spara-%c3%a4ndringar-till-f%c3%b6rvaret">Svenska</a>,</td></tr> <tr><td><a href="/book/tr/v2/Git-Temelleri-De%c4%9fi%c5%9fikliklerin-Repoya-Kaydedilmesi">Türkçe</a>.</td></tr> </table> </p> <hr class="sidebar"/> <p> The source of this book is <a href="https://github.com/progit2-ms/progit2">hosted on GitHub.</a></br> Patches, suggestions and comments are welcome. </p> </nav> </aside> <div id="content"> <div id="book-chapters"> <a class="dropdown-trigger" id="book-chapters-trigger" data-panel-id="chapters-dropdown" href="#">Chapters ▾</a> <div class='dropdown-panel' id='chapters-dropdown'> <div class='three-column'> <div class="column-left"> <ol class='book-toc'> <li class='chapter'> <h2>1. <a href="/book/ms/v2/Getting-Started-About-Version-Control">Getting Started</a></h2> <ol> <li> 1.1 <a href="/book/ms/v2/Getting-Started-About-Version-Control">About Version Control</a> </li> <li> 1.2 <a href="/book/ms/v2/Getting-Started-A-Short-History-of-Git">A Short History of Git</a> </li> <li> 1.3 <a href="/book/ms/v2/Getting-Started-What-is-Git%3F">What is Git?</a> </li> <li> 1.4 <a href="/book/ms/v2/Getting-Started-The-Command-Line">The Command Line</a> </li> <li> 1.5 <a href="/book/ms/v2/Getting-Started-Installing-Git">Installing Git</a> </li> <li> 1.6 <a href="/book/ms/v2/Getting-Started-First-Time-Git-Setup">First-Time Git Setup</a> </li> <li> 1.7 <a href="/book/ms/v2/Getting-Started-Getting-Help">Getting Help</a> </li> <li> 1.8 <a href="/book/ms/v2/Getting-Started-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>2. <a href="/book/ms/v2/Git-Basics-Getting-a-Git-Repository">Git Basics</a></h2> <ol> <li> 2.1 <a href="/book/ms/v2/Git-Basics-Getting-a-Git-Repository">Getting a Git Repository</a> </li> <li> 2.2 <a href="/book/ms/v2/Git-Basics-Recording-Changes-to-the-Repository" class="active">Recording Changes to the Repository</a> </li> <li> 2.3 <a href="/book/ms/v2/Git-Basics-Viewing-the-Commit-History">Viewing the Commit History</a> </li> <li> 2.4 <a href="/book/ms/v2/Git-Basics-Undoing-Things">Undoing Things</a> </li> <li> 2.5 <a href="/book/ms/v2/Git-Basics-Working-with-Remotes">Working with Remotes</a> </li> <li> 2.6 <a href="/book/ms/v2/Git-Basics-Tagging">Tagging</a> </li> <li> 2.7 <a href="/book/ms/v2/Git-Basics-Git-Aliases">Git Aliases</a> </li> <li> 2.8 <a href="/book/ms/v2/Git-Basics-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>3. <a href="/book/ms/v2/Git-Branching-Branches-in-a-Nutshell">Git Branching</a></h2> <ol> <li> 3.1 <a href="/book/ms/v2/Git-Branching-Branches-in-a-Nutshell">Branches in a Nutshell</a> </li> <li> 3.2 <a href="/book/ms/v2/Git-Branching-Basic-Branching-and-Merging">Basic Branching and Merging</a> </li> <li> 3.3 <a href="/book/ms/v2/Git-Branching-Branch-Management">Branch Management</a> </li> <li> 3.4 <a href="/book/ms/v2/Git-Branching-Branching-Workflows">Branching Workflows</a> </li> <li> 3.5 <a href="/book/ms/v2/Git-Branching-Remote-Branches">Remote Branches</a> </li> <li> 3.6 <a href="/book/ms/v2/Git-Branching-Rebasing">Rebasing</a> </li> <li> 3.7 <a href="/book/ms/v2/Git-Branching-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>4. <a href="/book/ms/v2/Git-on-the-Server-The-Protocols">Git on the Server</a></h2> <ol> <li> 4.1 <a href="/book/ms/v2/Git-on-the-Server-The-Protocols">The Protocols</a> </li> <li> 4.2 <a href="/book/ms/v2/Git-on-the-Server-Getting-Git-on-a-Server">Getting Git on a Server</a> </li> <li> 4.3 <a href="/book/ms/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key">Generating Your SSH Public Key</a> </li> <li> 4.4 <a href="/book/ms/v2/Git-on-the-Server-Setting-Up-the-Server">Setting Up the Server</a> </li> <li> 4.5 <a href="/book/ms/v2/Git-on-the-Server-Git-Daemon">Git Daemon</a> </li> <li> 4.6 <a href="/book/ms/v2/Git-on-the-Server-Smart-HTTP">Smart HTTP</a> </li> <li> 4.7 <a href="/book/ms/v2/Git-on-the-Server-GitWeb">GitWeb</a> </li> <li> 4.8 <a href="/book/ms/v2/Git-on-the-Server-GitLab">GitLab</a> </li> <li> 4.9 <a href="/book/ms/v2/Git-on-the-Server-Third-Party-Hosted-Options">Third Party Hosted Options</a> </li> <li> 4.10 <a href="/book/ms/v2/Git-on-the-Server-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>5. <a href="/book/ms/v2/Distributed-Git-Distributed-Workflows">Distributed Git</a></h2> <ol> <li> 5.1 <a href="/book/ms/v2/Distributed-Git-Distributed-Workflows">Distributed Workflows</a> </li> <li> 5.2 <a href="/book/ms/v2/Distributed-Git-Contributing-to-a-Project">Contributing to a Project</a> </li> <li> 5.3 <a href="/book/ms/v2/Distributed-Git-Maintaining-a-Project">Maintaining a Project</a> </li> <li> 5.4 <a href="/book/ms/v2/Distributed-Git-Summary">Summary</a> </li> </ol> </li> </ol> </div> <div class='column-middle'> <ol class='book-toc'> <li class='chapter'> <h2>6. <a href="/book/ms/v2/GitHub-Account-Setup-and-Configuration">GitHub</a></h2> <ol> <li> 6.1 <a href="/book/ms/v2/GitHub-Account-Setup-and-Configuration">Account Setup and Configuration</a> </li> <li> 6.2 <a href="/book/ms/v2/GitHub-Contributing-to-a-Project">Contributing to a Project</a> </li> <li> 6.3 <a href="/book/ms/v2/GitHub-Maintaining-a-Project">Maintaining a Project</a> </li> <li> 6.4 <a href="/book/ms/v2/GitHub-Managing-an-organization">Managing an organization</a> </li> <li> 6.5 <a href="/book/ms/v2/GitHub-Scripting-GitHub">Scripting GitHub</a> </li> <li> 6.6 <a href="/book/ms/v2/GitHub-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>7. <a href="/book/ms/v2/Git-Tools-Revision-Selection">Git Tools</a></h2> <ol> <li> 7.1 <a href="/book/ms/v2/Git-Tools-Revision-Selection">Revision Selection</a> </li> <li> 7.2 <a href="/book/ms/v2/Git-Tools-Interactive-Staging">Interactive Staging</a> </li> <li> 7.3 <a href="/book/ms/v2/Git-Tools-Stashing-and-Cleaning">Stashing and Cleaning</a> </li> <li> 7.4 <a href="/book/ms/v2/Git-Tools-Signing-Your-Work">Signing Your Work</a> </li> <li> 7.5 <a href="/book/ms/v2/Git-Tools-Searching">Searching</a> </li> <li> 7.6 <a href="/book/ms/v2/Git-Tools-Rewriting-History">Rewriting History</a> </li> <li> 7.7 <a href="/book/ms/v2/Git-Tools-Reset-Demystified">Reset Demystified</a> </li> <li> 7.8 <a href="/book/ms/v2/Git-Tools-Advanced-Merging">Advanced Merging</a> </li> <li> 7.9 <a href="/book/ms/v2/Git-Tools-Rerere">Rerere</a> </li> <li> 7.10 <a href="/book/ms/v2/Git-Tools-Debugging-with-Git">Debugging with Git</a> </li> <li> 7.11 <a href="/book/ms/v2/Git-Tools-Submodules">Submodules</a> </li> <li> 7.12 <a href="/book/ms/v2/Git-Tools-Bundling">Bundling</a> </li> <li> 7.13 <a href="/book/ms/v2/Git-Tools-Replace">Replace</a> </li> <li> 7.14 <a href="/book/ms/v2/Git-Tools-Credential-Storage">Credential Storage</a> </li> <li> 7.15 <a href="/book/ms/v2/Git-Tools-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>8. <a href="/book/ms/v2/Customizing-Git-Git-Configuration">Customizing Git</a></h2> <ol> <li> 8.1 <a href="/book/ms/v2/Customizing-Git-Git-Configuration">Git Configuration</a> </li> <li> 8.2 <a href="/book/ms/v2/Customizing-Git-Git-Attributes">Git Attributes</a> </li> <li> 8.3 <a href="/book/ms/v2/Customizing-Git-Git-Hooks">Git Hooks</a> </li> <li> 8.4 <a href="/book/ms/v2/Customizing-Git-An-Example-Git-Enforced-Policy">An Example Git-Enforced Policy</a> </li> <li> 8.5 <a href="/book/ms/v2/Customizing-Git-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>9. <a href="/book/ms/v2/Git-and-Other-Systems-Git-as-a-Client">Git and Other Systems</a></h2> <ol> <li> 9.1 <a href="/book/ms/v2/Git-and-Other-Systems-Git-as-a-Client">Git as a Client</a> </li> <li> 9.2 <a href="/book/ms/v2/Git-and-Other-Systems-Migrating-to-Git">Migrating to Git</a> </li> <li> 9.3 <a href="/book/ms/v2/Git-and-Other-Systems-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>10. <a href="/book/ms/v2/Git-Internals-Plumbing-and-Porcelain">Git Internals</a></h2> <ol> <li> 10.1 <a href="/book/ms/v2/Git-Internals-Plumbing-and-Porcelain">Plumbing and Porcelain</a> </li> <li> 10.2 <a href="/book/ms/v2/Git-Internals-Git-Objects">Git Objects</a> </li> <li> 10.3 <a href="/book/ms/v2/Git-Internals-Git-References">Git References</a> </li> <li> 10.4 <a href="/book/ms/v2/Git-Internals-Packfiles">Packfiles</a> </li> <li> 10.5 <a href="/book/ms/v2/Git-Internals-The-Refspec">The Refspec</a> </li> <li> 10.6 <a href="/book/ms/v2/Git-Internals-Transfer-Protocols">Transfer Protocols</a> </li> <li> 10.7 <a href="/book/ms/v2/Git-Internals-Maintenance-and-Data-Recovery">Maintenance and Data Recovery</a> </li> <li> 10.8 <a href="/book/ms/v2/Git-Internals-Environment-Variables">Environment Variables</a> </li> <li> 10.9 <a href="/book/ms/v2/Git-Internals-Summary">Summary</a> </li> </ol> </li> </ol> </div> <div class='column-right'> <ol class='book-toc'> <li class='chapter'> <h2>A1. <a href="/book/ms/v2/Appendix-A:-Git-in-Other-Environments-Graphical-Interfaces">Appendix A: Git in Other Environments</a></h2> <ol> <li> A1.1 <a href="/book/ms/v2/Appendix-A:-Git-in-Other-Environments-Graphical-Interfaces">Graphical Interfaces</a> </li> <li> A1.2 <a href="/book/ms/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Visual-Studio">Git in Visual Studio</a> </li> <li> A1.3 <a href="/book/ms/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Visual-Studio-Code">Git in Visual Studio Code</a> </li> <li> A1.4 <a href="/book/ms/v2/Appendix-A:-Git-in-Other-Environments-Git-in-IntelliJ-/-PyCharm-/-WebStorm-/-PhpStorm-/-RubyMine">Git in IntelliJ / PyCharm / WebStorm / PhpStorm / RubyMine</a> </li> <li> A1.5 <a href="/book/ms/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Sublime-Text">Git in Sublime Text</a> </li> <li> A1.6 <a href="/book/ms/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Bash">Git in Bash</a> </li> <li> A1.7 <a href="/book/ms/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Zsh">Git in Zsh</a> </li> <li> A1.8 <a href="/book/ms/v2/Appendix-A:-Git-in-Other-Environments-Git-in-PowerShell">Git in PowerShell</a> </li> <li> A1.9 <a href="/book/ms/v2/Appendix-A:-Git-in-Other-Environments-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>A2. <a href="/book/ms/v2/Appendix-B:-Embedding-Git-in-your-Applications-Command-line-Git">Appendix B: Embedding Git in your Applications</a></h2> <ol> <li> A2.1 <a href="/book/ms/v2/Appendix-B:-Embedding-Git-in-your-Applications-Command-line-Git">Command-line Git</a> </li> <li> A2.2 <a href="/book/ms/v2/Appendix-B:-Embedding-Git-in-your-Applications-Libgit2">Libgit2</a> </li> <li> A2.3 <a href="/book/ms/v2/Appendix-B:-Embedding-Git-in-your-Applications-JGit">JGit</a> </li> <li> A2.4 <a href="/book/ms/v2/Appendix-B:-Embedding-Git-in-your-Applications-go-git">go-git</a> </li> <li> A2.5 <a href="/book/ms/v2/Appendix-B:-Embedding-Git-in-your-Applications-Dulwich">Dulwich</a> </li> </ol> </li> <li class='chapter'> <h2>A3. <a href="/book/ms/v2/Appendix-C:-Git-Commands-Setup-and-Config">Appendix C: Git Commands</a></h2> <ol> <li> A3.1 <a href="/book/ms/v2/Appendix-C:-Git-Commands-Setup-and-Config">Setup and Config</a> </li> <li> A3.2 <a href="/book/ms/v2/Appendix-C:-Git-Commands-Getting-and-Creating-Projects">Getting and Creating Projects</a> </li> <li> A3.3 <a href="/book/ms/v2/Appendix-C:-Git-Commands-Basic-Snapshotting">Basic Snapshotting</a> </li> <li> A3.4 <a href="/book/ms/v2/Appendix-C:-Git-Commands-Branching-and-Merging">Branching and Merging</a> </li> <li> A3.5 <a href="/book/ms/v2/Appendix-C:-Git-Commands-Sharing-and-Updating-Projects">Sharing and Updating Projects</a> </li> <li> A3.6 <a href="/book/ms/v2/Appendix-C:-Git-Commands-Inspection-and-Comparison">Inspection and Comparison</a> </li> <li> A3.7 <a href="/book/ms/v2/Appendix-C:-Git-Commands-Debugging">Debugging</a> </li> <li> A3.8 <a href="/book/ms/v2/Appendix-C:-Git-Commands-Patching">Patching</a> </li> <li> A3.9 <a href="/book/ms/v2/Appendix-C:-Git-Commands-Email">Email</a> </li> <li> A3.10 <a href="/book/ms/v2/Appendix-C:-Git-Commands-External-Systems">External Systems</a> </li> <li> A3.11 <a href="/book/ms/v2/Appendix-C:-Git-Commands-Administration">Administration</a> </li> <li> A3.12 <a href="/book/ms/v2/Appendix-C:-Git-Commands-Plumbing-Commands">Plumbing Commands</a> </li> </ol> </li> </ol> </div> </div> </div> <span class="light" id="edition"> 2nd Edition </span> </div> <div id="main" data-pagefind-filter="category:book" data-pagefind-meta="category:Book" data-pagefind-weight="0.05" data-pagefind-body class="book edition2"> <h1>2.2 Git Basics - Recording Changes to the Repository</h1> <div> <h2 id="_recording_changes_to_the_repository">Recording Changes to the Repository</h2> <div class="paragraph"> <p>At this point, you should have a <em>bona fide</em> Git repository on your local machine, and a checkout or <em>working copy</em> of all of its files in front of you. Typically, you’ll want to start making changes and committing snapshots of those changes into your repository each time the project reaches a state you want to record.</p> </div> <div class="paragraph"> <p>Remember that each file in your working directory can be in one of two states: <em>tracked</em> or <em>untracked</em>. Tracked files are files that were in the last snapshot, as well as any newly staged files; they can be unmodified, modified, or staged. In short, tracked files are files that Git knows about.</p> </div> <div class="paragraph"> <p>Untracked files are everything else — any files in your working directory that were not in your last snapshot and are not in your staging area. When you first clone a repository, all of your files will be tracked and unmodified because Git just checked them out and you haven’t edited anything.</p> </div> <div class="paragraph"> <p>As you edit files, Git sees them as modified, because you’ve changed them since your last commit. As you work, you selectively stage these modified files and then commit all those staged changes, and the cycle repeats.</p> </div> <div class="imageblock"> <div class="content"> <img src="/book/ms/v2/images/lifecycle.png" alt="The lifecycle of the status of your files"> </div> <div class="title">Figure 8. The lifecycle of the status of your files</div> </div> <div class="sect3"> <h3 id="_checking_status">Checking the Status of Your Files</h3> <div class="paragraph"> <p>The main tool you use to determine which files are in which state is the <code>git status</code> command. If you run this command directly after a clone, you should see something like this:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean</code></pre> </div> </div> <div class="paragraph"> <p>This means you have a clean working directory; in other words, none of your tracked files are modified. Git also doesn’t see any untracked files, or they would be listed here. Finally, the command tells you which branch you’re on and informs you that it has not diverged from the same branch on the server. For now, that branch is always <code>master</code>, which is the default; you won’t worry about it here. <a href="/book/ms/v2/ch00/ch03-git-branching">Git Branching</a> will go over branches and references in detail.</p> </div> <div class="admonitionblock note"> <table> <tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content"> <div class="paragraph"> <p>GitHub changed the default branch name from <code>master</code> to <code>main</code> in mid-2020, and other Git hosts followed suit. So you may find that the default branch name in some newly created repositories is <code>main</code> and not <code>master</code>. In addition, the default branch name can be changed (as you have seen in <a href="/book/ms/v2/ch00/_new_default_branch">Your default branch name</a>), so you may see a different name for the default branch.</p> </div> <div class="paragraph"> <p>However, Git itself still uses <code>master</code> as the default, so we will use it throughout the book.</p> </div> </td> </tr> </table> </div> <div class="paragraph"> <p>Let’s say you add a new file to your project, a simple <code>README</code> file. If the file didn’t exist before, and you run <code>git status</code>, you see your untracked file like so:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ echo 'My Project' &gt; README $ git status On branch master Your branch is up-to-date with 'origin/master'. Untracked files: (use "git add &lt;file&gt;..." to include in what will be committed) README nothing added to commit but untracked files present (use "git add" to track)</code></pre> </div> </div> <div class="paragraph"> <p>You can see that your new <code>README</code> file is untracked, because it’s under the “Untracked files” heading in your status output. Untracked basically means that Git sees a file you didn’t have in the previous snapshot (commit), and which hasn’t yet been staged; Git won’t start including it in your commit snapshots until you explicitly tell it to do so. It does this so you don’t accidentally begin including generated binary files or other files that you did not mean to include. You do want to start including <code>README</code>, so let’s start tracking the file.</p> </div> </div> <div class="sect3"> <h3 id="_tracking_files">Tracking New Files</h3> <div class="paragraph"> <p>In order to begin tracking a new file, you use the command <code>git add</code>. To begin tracking the <code>README</code> file, you can run this:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git add README</code></pre> </div> </div> <div class="paragraph"> <p>If you run your status command again, you can see that your <code>README</code> file is now tracked and staged to be committed:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git restore --staged &lt;file&gt;..." to unstage) new file: README</code></pre> </div> </div> <div class="paragraph"> <p>You can tell that it’s staged because it’s under the “Changes to be committed” heading. If you commit at this point, the version of the file at the time you ran <code>git add</code> is what will be in the subsequent historical snapshot. You may recall that when you ran <code>git init</code> earlier, you then ran <code>git add &lt;files&gt;</code> — that was to begin tracking files in your directory. The <code>git add</code> command takes a path name for either a file or a directory; if it’s a directory, the command adds all the files in that directory recursively.</p> </div> </div> <div class="sect3"> <h3 id="_staging_modified_files">Staging Modified Files</h3> <div class="paragraph"> <p>Let’s change a file that was already tracked. If you change a previously tracked file called <code>CONTRIBUTING.md</code> and then run your <code>git status</code> command again, you get something that looks like this:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD &lt;file&gt;..." to unstage) new file: README Changes not staged for commit: (use "git add &lt;file&gt;..." to update what will be committed) (use "git checkout -- &lt;file&gt;..." to discard changes in working directory) modified: CONTRIBUTING.md</code></pre> </div> </div> <div class="paragraph"> <p>The <code>CONTRIBUTING.md</code> file appears under a section named “Changes not staged for commit” — which means that a file that is tracked has been modified in the working directory but not yet staged. To stage it, you run the <code>git add</code> command. <code>git add</code> is a multipurpose command — you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved. It may be helpful to think of it more as “add precisely this content to the next commit” rather than “add this file to the project”. Let’s run <code>git add</code> now to stage the <code>CONTRIBUTING.md</code> file, and then run <code>git status</code> again:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git add CONTRIBUTING.md $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD &lt;file&gt;..." to unstage) new file: README modified: CONTRIBUTING.md</code></pre> </div> </div> <div class="paragraph"> <p>Both files are staged and will go into your next commit. At this point, suppose you remember one little change that you want to make in <code>CONTRIBUTING.md</code> before you commit it. You open it again and make that change, and you’re ready to commit. However, let’s run <code>git status</code> one more time:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ vim CONTRIBUTING.md $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD &lt;file&gt;..." to unstage) new file: README modified: CONTRIBUTING.md Changes not staged for commit: (use "git add &lt;file&gt;..." to update what will be committed) (use "git checkout -- &lt;file&gt;..." to discard changes in working directory) modified: CONTRIBUTING.md</code></pre> </div> </div> <div class="paragraph"> <p>What the heck? Now <code>CONTRIBUTING.md</code> is listed as both staged <em>and</em> unstaged. How is that possible? It turns out that Git stages a file exactly as it is when you run the <code>git add</code> command. If you commit now, the version of <code>CONTRIBUTING.md</code> as it was when you last ran the <code>git add</code> command is how it will go into the commit, not the version of the file as it looks in your working directory when you run <code>git commit</code>. If you modify a file after you run <code>git add</code>, you have to run <code>git add</code> again to stage the latest version of the file:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git add CONTRIBUTING.md $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD &lt;file&gt;..." to unstage) new file: README modified: CONTRIBUTING.md</code></pre> </div> </div> </div> <div class="sect3"> <h3 id="_short_status">Short Status</h3> <div class="paragraph"> <p>While the <code>git status</code> output is pretty comprehensive, it’s also quite wordy. Git also has a short status flag so you can see your changes in a more compact way. If you run <code>git status -s</code> or <code>git status --short</code> you get a far more simplified output from the command:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git status -s M README MM Rakefile A lib/git.rb M lib/simplegit.rb ?? LICENSE.txt</code></pre> </div> </div> <div class="paragraph"> <p>New files that aren’t tracked have a <code>??</code> next to them, new files that have been added to the staging area have an <code>A</code>, modified files have an <code>M</code> and so on. There are two columns to the output — the left-hand column indicates the status of the staging area and the right-hand column indicates the status of the working tree. So for example in that output, the <code>README</code> file is modified in the working directory but not yet staged, while the <code>lib/simplegit.rb</code> file is modified and staged. The <code>Rakefile</code> was modified, staged and then modified again, so there are changes to it that are both staged and unstaged.</p> </div> </div> <div class="sect3"> <h3 id="_ignoring">Ignoring Files</h3> <div class="paragraph"> <p>Often, you’ll have a class of files that you don’t want Git to automatically add or even show you as being untracked. These are generally automatically generated files such as log files or files produced by your build system. In such cases, you can create a file listing patterns to match them named <code>.gitignore</code>. Here is an example <code>.gitignore</code> file:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ cat .gitignore *.[oa] *~</code></pre> </div> </div> <div class="paragraph"> <p>The first line tells Git to ignore any files ending in “.o” or “.a” — object and archive files that may be the product of building your code. The second line tells Git to ignore all files whose names end with a tilde (<code>~</code>), which is used by many text editors such as Emacs to mark temporary files. You may also include a log, tmp, or pid directory; automatically generated documentation; and so on. Setting up a <code>.gitignore</code> file for your new repository before you get going is generally a good idea so you don’t accidentally commit files that you really don’t want in your Git repository.</p> </div> <div class="paragraph"> <p>The rules for the patterns you can put in the <code>.gitignore</code> file are as follows:</p> </div> <div class="ulist"> <ul> <li> <p>Blank lines or lines starting with <code>#</code> are ignored.</p> </li> <li> <p>Standard glob patterns work, and will be applied recursively throughout the entire working tree.</p> </li> <li> <p>You can start patterns with a forward slash (<code>/</code>) to avoid recursivity.</p> </li> <li> <p>You can end patterns with a forward slash (<code>/</code>) to specify a directory.</p> </li> <li> <p>You can negate a pattern by starting it with an exclamation point (<code>!</code>).</p> </li> </ul> </div> <div class="paragraph"> <p>Glob patterns are like simplified regular expressions that shells use. An asterisk (<code>*</code>) matches zero or more characters; <code>[abc]</code> matches any character inside the brackets (in this case a, b, or c); a question mark (<code>?</code>) matches a single character; and brackets enclosing characters separated by a hyphen (<code>[0-9]</code>) matches any character between them (in this case 0 through 9). You can also use two asterisks to match nested directories; <code>a/**/z</code> would match <code>a/z</code>, <code>a/b/z</code>, <code>a/b/c/z</code>, and so on.</p> </div> <div class="paragraph"> <p>Here is another example <code>.gitignore</code> file:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code># ignore all .a files *.a # but do track lib.a, even though you're ignoring .a files above !lib.a # only ignore the TODO file in the current directory, not subdir/TODO /TODO # ignore all files in any directory named build build/ # ignore doc/notes.txt, but not doc/server/arch.txt doc/*.txt # ignore all .pdf files in the doc/ directory and any of its subdirectories doc/**/*.pdf</code></pre> </div> </div> <div class="admonitionblock tip"> <table> <tr> <td class="icon"> <div class="title">Tip</div> </td> <td class="content"> <div class="paragraph"> <p>GitHub maintains a fairly comprehensive list of good <code>.gitignore</code> file examples for dozens of projects and languages at <a href="https://github.com/github/gitignore" class="bare" target="_blank" rel="noopener">https://github.com/github/gitignore</a> if you want a starting point for your project.</p> </div> </td> </tr> </table> </div> <div class="admonitionblock note"> <table> <tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content"> <div class="paragraph"> <p>In the simple case, a repository might have a single <code>.gitignore</code> file in its root directory, which applies recursively to the entire repository. However, it is also possible to have additional <code>.gitignore</code> files in subdirectories. The rules in these nested <code>.gitignore</code> files apply only to the files under the directory where they are located. The Linux kernel source repository has 206 <code>.gitignore</code> files.</p> </div> <div class="paragraph"> <p>It is beyond the scope of this book to get into the details of multiple <code>.gitignore</code> files; see <code>man gitignore</code> for the details.</p> </div> </td> </tr> </table> </div> </div> <div class="sect3"> <h3 id="_git_diff_staged">Viewing Your Staged and Unstaged Changes</h3> <div class="paragraph"> <p>If the <code>git status</code> command is too vague for you — you want to know exactly what you changed, not just which files were changed — you can use the <code>git diff</code> command. We’ll cover <code>git diff</code> in more detail later, but you’ll probably use it most often to answer these two questions: What have you changed but not yet staged? And what have you staged that you are about to commit? Although <code>git status</code> answers those questions very generally by listing the file names, <code>git diff</code> shows you the exact lines added and removed — the patch, as it were.</p> </div> <div class="paragraph"> <p>Let’s say you edit and stage the <code>README</code> file again and then edit the <code>CONTRIBUTING.md</code> file without staging it. If you run your <code>git status</code> command, you once again see something like this:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD &lt;file&gt;..." to unstage) modified: README Changes not staged for commit: (use "git add &lt;file&gt;..." to update what will be committed) (use "git checkout -- &lt;file&gt;..." to discard changes in working directory) modified: CONTRIBUTING.md</code></pre> </div> </div> <div class="paragraph"> <p>To see what you’ve changed but not yet staged, type <code>git diff</code> with no other arguments:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git diff diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8ebb991..643e24f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -65,7 +65,8 @@ branch directly, things can get messy. Please include a nice description of your changes when you submit your PR; if we have to read the whole diff to figure out why you're contributing in the first place, you're less likely to get feedback and have your change -merged in. +merged in. Also, split your changes into comprehensive chunks if your patch is +longer than a dozen lines. If you are starting to work on a particular area, feel free to submit a PR that highlights your work in progress (and note in the PR title that it's</code></pre> </div> </div> <div class="paragraph"> <p>That command compares what is in your working directory with what is in your staging area. The result tells you the changes you’ve made that you haven’t yet staged.</p> </div> <div class="paragraph"> <p>If you want to see what you’ve staged that will go into your next commit, you can use <code>git diff --staged</code>. This command compares your staged changes to your last commit:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git diff --staged diff --git a/README b/README new file mode 100644 index 0000000..03902a1 --- /dev/null +++ b/README @@ -0,0 +1 @@ +My Project</code></pre> </div> </div> <div class="paragraph"> <p>It’s important to note that <code>git diff</code> by itself doesn’t show all changes made since your last commit — only changes that are still unstaged. If you’ve staged all of your changes, <code>git diff</code> will give you no output.</p> </div> <div class="paragraph"> <p>For another example, if you stage the <code>CONTRIBUTING.md</code> file and then edit it, you can use <code>git diff</code> to see the changes in the file that are staged and the changes that are unstaged. If our environment looks like this:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git add CONTRIBUTING.md $ echo '# test line' &gt;&gt; CONTRIBUTING.md $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD &lt;file&gt;..." to unstage) modified: CONTRIBUTING.md Changes not staged for commit: (use "git add &lt;file&gt;..." to update what will be committed) (use "git checkout -- &lt;file&gt;..." to discard changes in working directory) modified: CONTRIBUTING.md</code></pre> </div> </div> <div class="paragraph"> <p>Now you can use <code>git diff</code> to see what is still unstaged:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git diff diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 643e24f..87f08c8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -119,3 +119,4 @@ at the ## Starter Projects See our [projects list](https://github.com/libgit2/libgit2/blob/development/PROJECTS.md). +# test line</code></pre> </div> </div> <div class="paragraph"> <p>and <code>git diff --cached</code> to see what you’ve staged so far (<code>--staged</code> and <code>--cached</code> are synonyms):</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git diff --cached diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8ebb991..643e24f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -65,7 +65,8 @@ branch directly, things can get messy. Please include a nice description of your changes when you submit your PR; if we have to read the whole diff to figure out why you're contributing in the first place, you're less likely to get feedback and have your change -merged in. +merged in. Also, split your changes into comprehensive chunks if your patch is +longer than a dozen lines. If you are starting to work on a particular area, feel free to submit a PR that highlights your work in progress (and note in the PR title that it's</code></pre> </div> </div> <div class="admonitionblock note"> <table> <tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content"> <div class="title">Git Diff in an External Tool</div> <div class="paragraph"> <p>We will continue to use the <code>git diff</code> command in various ways throughout the rest of the book. There is another way to look at these diffs if you prefer a graphical or external diff viewing program instead. If you run <code>git difftool</code> instead of <code>git diff</code>, you can view any of these diffs in software like emerge, vimdiff and many more (including commercial products). Run <code>git difftool --tool-help</code> to see what is available on your system.</p> </div> </td> </tr> </table> </div> </div> <div class="sect3"> <h3 id="_committing_changes">Committing Your Changes</h3> <div class="paragraph"> <p>Now that your staging area is set up the way you want it, you can commit your changes. Remember that anything that is still unstaged — any files you have created or modified that you haven’t run <code>git add</code> on since you edited them — won’t go into this commit. They will stay as modified files on your disk. In this case, let’s say that the last time you ran <code>git status</code>, you saw that everything was staged, so you’re ready to commit your changes. The simplest way to commit is to type <code>git commit</code>:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git commit</code></pre> </div> </div> <div class="paragraph"> <p>Doing so launches your editor of choice.</p> </div> <div class="admonitionblock note"> <table> <tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content"> <div class="paragraph"> <p>This is set by your shell’s <code>EDITOR</code> environment variable — usually vim or emacs, although you can configure it with whatever you want using the <code>git config --global core.editor</code> command as you saw in <a href="/book/ms/v2/ch00/ch01-getting-started">Getting Started</a>.</p> </div> </td> </tr> </table> </div> <div class="paragraph"> <p>The editor displays the following text (this example is a Vim screen):</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code># Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # Your branch is up-to-date with 'origin/master'. # # Changes to be committed: # new file: README # modified: CONTRIBUTING.md # ~ ~ ~ ".git/COMMIT_EDITMSG" 9L, 283C</code></pre> </div> </div> <div class="paragraph"> <p>You can see that the default commit message contains the latest output of the <code>git status</code> command commented out and one empty line on top. You can remove these comments and type your commit message, or you can leave them there to help you remember what you’re committing.</p> </div> <div class="admonitionblock note"> <table> <tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content"> <div class="paragraph"> <p>For an even more explicit reminder of what you’ve modified, you can pass the <code>-v</code> option to <code>git commit</code>. Doing so also puts the diff of your change in the editor so you can see exactly what changes you’re committing.</p> </div> </td> </tr> </table> </div> <div class="paragraph"> <p>When you exit the editor, Git creates your commit with that commit message (with the comments and diff stripped out).</p> </div> <div class="paragraph"> <p>Alternatively, you can type your commit message inline with the <code>commit</code> command by specifying it after a <code>-m</code> flag, like this:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git commit -m "Story 182: fix benchmarks for speed" [master 463dc4f] Story 182: fix benchmarks for speed 2 files changed, 2 insertions(+) create mode 100644 README</code></pre> </div> </div> <div class="paragraph"> <p>Now you’ve created your first commit! You can see that the commit has given you some output about itself: which branch you committed to (<code>master</code>), what SHA-1 checksum the commit has (<code>463dc4f</code>), how many files were changed, and statistics about lines added and removed in the commit.</p> </div> <div class="paragraph"> <p>Remember that the commit records the snapshot you set up in your staging area. Anything you didn’t stage is still sitting there modified; you can do another commit to add it to your history. Every time you perform a commit, you’re recording a snapshot of your project that you can revert to or compare to later.</p> </div> </div> <div class="sect3"> <h3 id="_skipping_the_staging_area">Skipping the Staging Area</h3> <div class="paragraph"> <p> Although it can be amazingly useful for crafting commits exactly how you want them, the staging area is sometimes a bit more complex than you need in your workflow. If you want to skip the staging area, Git provides a simple shortcut. Adding the <code>-a</code> option to the <code>git commit</code> command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the <code>git add</code> part:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add &lt;file&gt;..." to update what will be committed) (use "git checkout -- &lt;file&gt;..." to discard changes in working directory) modified: CONTRIBUTING.md no changes added to commit (use "git add" and/or "git commit -a") $ git commit -a -m 'Add new benchmarks' [master 83e38c7] Add new benchmarks 1 file changed, 5 insertions(+), 0 deletions(-)</code></pre> </div> </div> <div class="paragraph"> <p>Notice how you don’t have to run <code>git add</code> on the <code>CONTRIBUTING.md</code> file in this case before you commit. That’s because the <code>-a</code> flag includes all changed files. This is convenient, but be careful; sometimes this flag will cause you to include unwanted changes.</p> </div> </div> <div class="sect3"> <h3 id="_removing_files">Removing Files</h3> <div class="paragraph"> <p> To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit. The <code>git rm</code> command does that, and also removes the file from your working directory so you don’t see it as an untracked file the next time around.</p> </div> <div class="paragraph"> <p>If you simply remove the file from your working directory, it shows up under the “Changes not staged for commit” (that is, <em>unstaged</em>) area of your <code>git status</code> output:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ rm PROJECTS.md $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add/rm &lt;file&gt;..." to update what will be committed) (use "git checkout -- &lt;file&gt;..." to discard changes in working directory) deleted: PROJECTS.md no changes added to commit (use "git add" and/or "git commit -a")</code></pre> </div> </div> <div class="paragraph"> <p>Then, if you run <code>git rm</code>, it stages the file’s removal:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git rm PROJECTS.md rm 'PROJECTS.md' $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD &lt;file&gt;..." to unstage) deleted: PROJECTS.md</code></pre> </div> </div> <div class="paragraph"> <p>The next time you commit, the file will be gone and no longer tracked. If you modified the file or had already added it to the staging area, you must force the removal with the <code>-f</code> option. This is a safety feature to prevent accidental removal of data that hasn’t yet been recorded in a snapshot and that can’t be recovered from Git.</p> </div> <div class="paragraph"> <p>Another useful thing you may want to do is to keep the file in your working tree but remove it from your staging area. In other words, you may want to keep the file on your hard drive but not have Git track it anymore. This is particularly useful if you forgot to add something to your <code>.gitignore</code> file and accidentally staged it, like a large log file or a bunch of <code>.a</code> compiled files. To do this, use the <code>--cached</code> option:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git rm --cached README</code></pre> </div> </div> <div class="paragraph"> <p>You can pass files, directories, and file-glob patterns to the <code>git rm</code> command. That means you can do things such as:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git rm log/\*.log</code></pre> </div> </div> <div class="paragraph"> <p>Note the backslash (<code>\</code>) in front of the <code>*</code>. This is necessary because Git does its own filename expansion in addition to your shell’s filename expansion. This command removes all files that have the <code>.log</code> extension in the <code>log/</code> directory. Or, you can do something like this:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git rm \*~</code></pre> </div> </div> <div class="paragraph"> <p>This command removes all files whose names end with a <code>~</code>.</p> </div> </div> <div class="sect3"> <h3 id="_git_mv">Moving Files</h3> <div class="paragraph"> <p> Unlike many other VCSs, Git doesn’t explicitly track file movement. If you rename a file in Git, no metadata is stored in Git that tells it you renamed the file. However, Git is pretty smart about figuring that out after the fact — we’ll deal with detecting file movement a bit later.</p> </div> <div class="paragraph"> <p>Thus it’s a bit confusing that Git has a <code>mv</code> command. If you want to rename a file in Git, you can run something like:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git mv file_from file_to</code></pre> </div> </div> <div class="paragraph"> <p>and it works fine. In fact, if you run something like this and look at the status, you’ll see that Git considers it a renamed file:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git mv README.md README $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD &lt;file&gt;..." to unstage) renamed: README.md -&gt; README</code></pre> </div> </div> <div class="paragraph"> <p>However, this is equivalent to running something like this:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ mv README.md README $ git rm README.md $ git add README</code></pre> </div> </div> <div class="paragraph"> <p>Git figures out that it’s a rename implicitly, so it doesn’t matter if you rename a file that way or with the <code>mv</code> command. The only real difference is that <code>git mv</code> is one command instead of three — it’s a convenience function. More importantly, you can use any tool you like to rename a file, and address the <code>add</code>/<code>rm</code> later, before you commit.</p> </div> </div> <div id="nav"><a href="/book/ms/v2/Git-Basics-Getting-a-Git-Repository">prev</a> | <a href="/book/ms/v2/Git-Basics-Viewing-the-Commit-History">next</a></div> </div> </div> </div> </div> <footer> <div class="site-source"> <a href="/site">About this site</a><br> Patches, suggestions, and comments are welcome. </div> <div class="sfc-member"> Git is a member of <a href="/sfc">Software Freedom Conservancy</a> </div> </footer> <a href="#top" class="no-js scrollToTop" id="scrollToTop" data-label="Scroll to top"> <img src="/images/icons/chevron-up@2x.png" width="20" height="20" alt="scroll-to-top"/> </a> <script src="/js/jquery-1.7.1.min.js"></script> <script src="/js/jquery-ui-1.8.18.custom.min.js"></script> <script src="/js/jquery.defaultvalue.js"></script> <script src="/js/session.min.js"></script> <script src="/js/application.min.js"></script> </div> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10