how-to-use-git-a-reference-guide.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. </button> How To Use Git: A Reference Guide
  4. </h1>
  5. <span class="meta-section timestamp"><span class="tutorial-date-text"></span><span class="tutorial-date">October 10, 2018</span></span>
  6. <div class="author-and-date">
  7. <div class="tutorial-author">
  8. Lisa Tagliaferri
  9. </div>
  10. </div>
  11. <div class="content-body tutorial-content" data-growable-markdown>
  12. <h2 id="git-cheat-sheet">Git Cheat Sheet</h2>
  13. <h3 id="introduction">Introduction</h3>
  14. <p>Teams of developers and open-source software maintainers typically manage their projects through Git, a distributed version control system that supports collaboration. </p>
  15. <p>This cheat sheet-style guide provides a quick reference to commands that are useful for working and collaborating in a Git repository. To install and configure Git, be sure to read “<a href="https://www.digitalocean.com/community/tutorials/how-to-contribute-to-open-source-getting-started-with-git">How To Contribute to Open Source: Getting Started with Git</a>.”</p>
  16. <p><strong>How to Use This Guide:</strong></p>
  17. <ul>
  18. <li>This guide is in cheat sheet format with self-contained command-line snippets.</li>
  19. <li>Jump to any section that is relevant to the task you are trying to complete.</li>
  20. <li>When you see <code><span class="highlight">highlighted text</span></code> in this guide’s commands, keep in mind that this text should refer to the commits and files in <em>your own</em> repository.</li>
  21. </ul>
  22. <h2 id="set-up-and-initialization">Set Up and Initialization</h2>
  23. <p>Check your Git version with the following command, which will also confirm that Git is installed.</p>
  24. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git --version
  25. </li></ul></code></pre>
  26. <p>You can initialize your current working directory as a Git repository with <code>init</code>.</p>
  27. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git init
  28. </li></ul></code></pre>
  29. <p>To copy an existing Git repository hosted remotely, you’ll use <code>git clone</code> with the repo’s URL or server location (in the latter case you will use <code>ssh</code>).</p>
  30. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git clone <span class="highlight">https://www.github.com/username/repo-name</span>
  31. </li></ul></code></pre>
  32. <p>Show your current Git directory’s remote repository.</p>
  33. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git remote
  34. </li></ul></code></pre>
  35. <p>For a more verbose output, use the <code>-v</code> flag.</p>
  36. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git remote -v
  37. </li></ul></code></pre>
  38. <p>Add the Git upstream, which can be a URL or can be hosted on a server (in the latter case, connect with <code>ssh</code>).</p>
  39. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git remote add upstream <span class="highlight">https://www.github.com/username/repo-name</span>
  40. </li></ul></code></pre>
  41. <h2 id="staging">Staging</h2>
  42. <p>When you’ve modified a file and have marked it to go in your next commit, it is considered to be a staged file.</p>
  43. <p>Check the status of your Git repository, including files added that are not staged, and files that are staged. </p>
  44. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git status
  45. </li></ul></code></pre>
  46. <p>To stage modified files, use the <code>add</code> command, which you can run multiple times before a commit. If you make subsequent changes that you want included in the next commit, you must run <code>add</code> again.</p>
  47. <p>You can specify the specific file with <code>add</code>.</p>
  48. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git add <span class="highlight">my_script.py</span>
  49. </li></ul></code></pre>
  50. <p>With <code>.</code> you can add all files in the current directory including files that begin with a <code>.</code>.</p>
  51. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git add .
  52. </li></ul></code></pre>
  53. <p>You can remove a file from staging while retaining changes within your working directory with <code>reset</code>.</p>
  54. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git reset <span class="highlight">my_script.py</span>
  55. </li></ul></code></pre>
  56. <h2 id="committing">Committing</h2>
  57. <p>Once you have staged your updates, you are ready to commit them, which will record changes you have made to the repository.</p>
  58. <p>To commit staged files, you’ll run the <code>commit</code> command with your meaningful commit message so that you can track commits. </p>
  59. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git commit -m "Commit message"
  60. </li></ul></code></pre>
  61. <p>You can condense staging all tracked files with committing them in one step.</p>
  62. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git commit -am "Commit message"
  63. </li></ul></code></pre>
  64. <p>If you need to modify your commit message, you can do so with the <code>--amend</code> flag.</p>
  65. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git commit --amend -m "New commit message"
  66. </li></ul></code></pre>
  67. <h2 id="branches">Branches</h2>
  68. <p>A branch in Git is a movable pointer to one of the commits in the repository, it allows you to isolate work and manage feature development and integrations. You can learn more about branches by reading the <a href="https://git-scm.com/book/en/v1/Git-Branching-What-a-Branch-Is">Git documentation</a>.</p>
  69. <p>List all current branches with the <code>branch</code> command. An asterisk (<code>*</code>) will appear next to your currently active branch.</p>
  70. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git branch
  71. </li></ul></code></pre>
  72. <p>Create a new branch. You will remain on your currently active branch until you switch to the new one.</p>
  73. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git branch <span class="highlight">new-branch</span>
  74. </li></ul></code></pre>
  75. <p>Switch to any existing branch and check it out into your current working directory.</p>
  76. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git checkout <span class="highlight">another-branch</span>
  77. </li></ul></code></pre>
  78. <p>You can consolidate the creation and checkout of a new branch by using the <code>-b</code> flag.</p>
  79. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git checkout -b <span class="highlight">new-branch</span>
  80. </li></ul></code></pre>
  81. <p>Rename your branch name.</p>
  82. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git branch -m <span class="highlight">current-branch-name</span> <span class="highlight">new-branch-name</span>
  83. </li></ul></code></pre>
  84. <p>Merge the specified branch’s history into the one you’re currently working in. </p>
  85. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git merge <span class="highlight">branch-name</span>
  86. </li></ul></code></pre>
  87. <p>Abort the merge, in case there are conflicts. </p>
  88. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git merge --abort
  89. </li></ul></code></pre>
  90. <p>You can also select a particular commit to merge with <code>cherry-pick</code> with the string that references the specific commit. </p>
  91. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git cherry-pick <span class="highlight">f7649d0</span>
  92. </li></ul></code></pre>
  93. <p>When you have merged a branch and no longer need the branch, you can delete it.</p>
  94. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git branch -d <span class="highlight">branch-name</span>
  95. </li></ul></code></pre>
  96. <p>If you have not merged a branch to master, but are sure you want to delete it, you can <strong>force</strong> delete a branch.</p>
  97. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git branch -D <span class="highlight">branch-name</span>
  98. </li></ul></code></pre>
  99. <h2 id="collaborate-and-update">Collaborate and Update</h2>
  100. <p>To download changes from another repository, such as the remote upstream, you’ll use <code>fetch</code>. </p>
  101. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git fetch <span class="highlight">upstream</span>
  102. </li></ul></code></pre>
  103. <p>Merge the fetched commits.</p>
  104. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git merge upstream/master
  105. </li></ul></code></pre>
  106. <p>Push or transmit your local branch commits to the remote repository branch.</p>
  107. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git push origin master
  108. </li></ul></code></pre>
  109. <p>Fetch and merge any commits from the tracking remote branch.</p>
  110. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git pull
  111. </li></ul></code></pre>
  112. <h2 id="inspecting">Inspecting</h2>
  113. <p>Display the commit history for the currently active branch.</p>
  114. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git log
  115. </li></ul></code></pre>
  116. <p>Show the commits that changed a particular file. This follows the file regardless of file renaming.</p>
  117. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git log --follow <span class="highlight">my_script.py</span>
  118. </li></ul></code></pre>
  119. <p>Show the commits that are on one branch and not on the other. This will show commits on <code><span class="highlight">a-branch</span></code> that are not on <code><span class="highlight">b-branch</span></code>.</p>
  120. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git log <span class="highlight">a-branch</span>..<span class="highlight">b-branch</span>
  121. </li></ul></code></pre>
  122. <p>Look at reference logs (<code>reflog</code>) to see when the tips of branches and other references were last updated within the repository.</p>
  123. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git reflog
  124. </li></ul></code></pre>
  125. <p>Show any object in Git via its commit string or hash in a more human-readable format.</p>
  126. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git show <span class="highlight">de754f5</span>
  127. </li></ul></code></pre>
  128. <h2 id="show-changes">Show Changes</h2>
  129. <p>The <code>git diff</code> command shows changes between commits, branches, and more. You can read more fully about it through the <a href="https://git-scm.com/docs/git-diff">Git documentation</a>. </p>
  130. <p>Compare modified files that are on the staging area.</p>
  131. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git diff --staged
  132. </li></ul></code></pre>
  133. <p>Display the diff of what is in <code><span class="highlight">a-branch</span></code> but is not in <code><span class="highlight">b-branch</span></code>.</p>
  134. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git diff <span class="highlight">a-branch</span>..<span class="highlight">b-branch</span>
  135. </li></ul></code></pre>
  136. <p>Show the diff between two specific commits.</p>
  137. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git diff <span class="highlight">61ce3e6</span>..<span class="highlight">e221d9c</span>
  138. </li></ul></code></pre>
  139. <h2 id="stashing">Stashing</h2>
  140. <p>Sometimes you’ll find that you made changes to some code, but before you finish you have to begin working on something else. You’re not quite ready to commit the changes you have made so far, but you don’t want to lose your work. The <code>git stash</code> command will allow you to save your local modifications and revert back to the working directory that is in line with the most recent <code>HEAD</code> commit.</p>
  141. <p>Stash your current work.</p>
  142. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git stash
  143. </li></ul></code></pre>
  144. <p>See what you currently have stashed.</p>
  145. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git stash list
  146. </li></ul></code></pre>
  147. <p>Your stashes will be named <code>stash@{0}</code>, <code>stash@{1}</code>, and so on.</p>
  148. <p>Show information about a particular stash.</p>
  149. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git stash show stash@{<span class="highlight">0</span>}
  150. </li></ul></code></pre>
  151. <p>To bring the files in a current stash out of the stash while still retaining the stash, use <code>apply</code>.</p>
  152. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git stash apply stash@{<span class="highlight">0</span>}
  153. </li></ul></code></pre>
  154. <p>If you want to bring files out of a stash, and no longer need the stash, use <code>pop</code>.</p>
  155. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git stash pop stash@{<span class="highlight">0</span>}
  156. </li></ul></code></pre>
  157. <p>If you no longer need the files saved in a particular stash, you can <code>drop</code> the stash.</p>
  158. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git stash drop stash@{<span class="highlight">0</span>}
  159. </li></ul></code></pre>
  160. <p>If you have multiple stashes saved and no longer need to use any of them, you can use <code>clear</code> to remove them.</p>
  161. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git stash clear
  162. </li></ul></code></pre>
  163. <h2 id="ignoring-files">Ignoring Files</h2>
  164. <p>If you want to keep files in your local Git directory, but do not want to commit them to the project, you can add these files to your <code>.gitignore</code> file so that they do not cause conflicts.</p>
  165. <p>Use a text editor such as nano to add files to the <code>.gitignore</code> file.</p>
  166. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">nano .gitignore
  167. </li></ul></code></pre>
  168. <p>To see examples of <code>.gitignore</code> files, you can look at GitHub’s <a href="https://github.com/github/gitignore"><code>.gitignore</code> template repo</a>.</p>
  169. <h2 id="rebasing">Rebasing</h2>
  170. <p>A rebase allows us to move branches around by changing the commit that they are based on. With rebasing, you can squash or reword commits.</p>
  171. <p>You can start a rebase by either calling the number of commits you have made that you want to rebase (<code>5</code> in the case below).</p>
  172. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git rebase -i HEAD~<span class="highlight">5</span>
  173. </li></ul></code></pre>
  174. <p>Alternatively, you can rebase based on a particular commit string or hash.</p>
  175. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git rebase -i <span class="highlight">074a4e5</span>
  176. </li></ul></code></pre>
  177. <p>Once you have squashed or reworded commits, you can complete the rebase of your branch on top of the latest version of the project’s upstream code. </p>
  178. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git rebase <span class="highlight">upstream/master</span>
  179. </li></ul></code></pre>
  180. <p>To learn more about rebasing and updating, you can read <a href="https://www.digitalocean.com/community/tutorials/how-to-rebase-and-update-a-pull-request">How To Rebase and Update a Pull Request</a>, which is also applicable to any type of commit.</p>
  181. <h2 id="resetting">Resetting</h2>
  182. <p>Sometimes, including after a rebase, you need to reset your working tree. You can reset to a particular commit, and <strong>delete all changes</strong>, with the following command.</p>
  183. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git reset --hard <span class="highlight">1fc6665</span>
  184. </li></ul></code></pre>
  185. <p>To force push your last known non-conflicting commit to the origin repository, you’ll need to use <code>--force</code>. </p>
  186. <p><span class='warning'><strong>Warning</strong>: Force pushing to master is often frowned upon unless there is a really important reason for doing it. Use sparingly when working on your own repositories, and work to avoid this when you’re collaborating.<br></span></p>
  187. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git push --force origin master
  188. </li></ul></code></pre>
  189. <p>To remove local untracked files and subdirectories from the Git directory for a clean working branch, you can use <code>git clean</code>.</p>
  190. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git clean -f -d
  191. </li></ul></code></pre>
  192. <p>If you need to modify your local repository so that it looks like the current upstream master (that is, there are too many conflicts), you can perform a hard reset.</p>
  193. <p><span class='note'><strong>Note</strong>: Performing this command will make your local repository look exactly like the upstream. Any commits you have made but that were not pulled into the upstream <strong>will be destroyed</strong>.<br></span></p>
  194. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git reset --hard upstream/master
  195. </li></ul></code></pre>
  196. <h2 id="conclusion">Conclusion</h2>
  197. <p>This guide covers some of the more common Git commands you may use when managing repositories and collaborating on software. </p>
  198. <p>You can learn more about open-source software and collaboration in our <a href="https://www.digitalocean.com/community/tutorial_series/an-introduction-to-open-source">Introduction to Open Source tutorial series</a>:</p>
  199. <ul>
  200. <li><a href="https://www.digitalocean.com/community/tutorials/how-to-contribute-to-open-source-getting-started-with-git">How To Contribute to Open Source: Getting Started with Git</a></li>
  201. <li><a href="https://www.digitalocean.com/community/tutorials/how-to-create-a-pull-request-on-github">How To Create a Pull Request on GitHub</a></li>
  202. <li><a href="https://www.digitalocean.com/community/tutorials/how-to-rebase-and-update-a-pull-request">How To Rebase and Update a Pull Request</a></li>
  203. <li><a href="https://www.digitalocean.com/community/tutorials/how-to-maintain-open-source-software-projects">How To Maintain Open-Source Software Projects</a></li>
  204. </ul>
  205. <p>There are many more commands and variations that you may find useful as part of your work with Git. To learn more about all of your available options, you can run:</p>
  206. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">git --help
  207. </li></ul></code></pre>
  208. <p>To receive useful information. You can also read more about Git and look at Git’s documentation from the <a href="https://git-scm.com/">official Git website</a>.</p>
  209. </div>
  210. </div>
  211. </body>
  212. </html>