learngit/common_commands/index.html

323 lines
16 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="../img/favicon.ico">
<title>common commands - learngit</title>
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/font-awesome.min.css" rel="stylesheet">
<link href="../css/base.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/darcula.min.css">
<script src="../js/jquery-1.10.2.min.js" defer></script>
<script src="../js/bootstrap.min.js" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<div class="navbar fixed-top navbar-expand-lg navbar-dark bg-primary">
<div class="container">
<a class="navbar-brand" href="..">learngit</a>
<!-- Expander button -->
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbar-collapse">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Expanded navigation -->
<div id="navbar-collapse" class="navbar-collapse collapse">
<!-- Main navigation -->
<ul class="nav navbar-nav">
<li class="navitem">
<a href=".." class="nav-link">learngit</a>
</li>
<li class="navitem">
<a href="../branching_strategies/" class="nav-link">branching</a>
</li>
<li class="navitem active">
<a href="./" class="nav-link">common commands</a>
</li>
<li class="navitem">
<a href="../glossary/" class="nav-link">glossary</a>
</li>
<li class="navitem">
<a href="../ssh_setup/" class="nav-link">ssh key setup</a>
</li>
</ul>
<ul class="nav navbar-nav ml-auto">
<li class="nav-item">
<a href="#" class="nav-link" data-toggle="modal" data-target="#mkdocs_search_modal">
<i class="fa fa-search"></i> Search
</a>
</li>
<li class="nav-item">
<a rel="prev" href="../branching_strategies/" class="nav-link">
<i class="fa fa-arrow-left"></i> Previous
</a>
</li>
<li class="nav-item">
<a rel="next" href="../glossary/" class="nav-link">
Next <i class="fa fa-arrow-right"></i>
</a>
</li>
<li class="nav-item">
<a href="https://github.com/benharri/learngit/blob/master/docs/common_commands.md" class="nav-link"><i class="fa fa-github"></i> Edit on GitHub</a>
</li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-3"><div class="navbar-light navbar-expand-md bs-sidebar hidden-print affix" role="complementary">
<div class="navbar-header">
<button type="button" class="navbar-toggler collapsed" data-toggle="collapse" data-target="#toc-collapse" title="Table of Contents">
<span class="fa fa-angle-down"></span>
</button>
</div>
<div id="toc-collapse" class="navbar-collapse collapse card bg-light">
<ul class="nav flex-column">
<li class="nav-item" data-level="1"><a href="#common-commands" class="nav-link">common commands</a>
<ul class="nav flex-column">
<li class="nav-item" data-level="2"><a href="#add" class="nav-link">add</a>
<ul class="nav flex-column">
</ul>
</li>
<li class="nav-item" data-level="2"><a href="#branch" class="nav-link">branch</a>
<ul class="nav flex-column">
</ul>
</li>
<li class="nav-item" data-level="2"><a href="#checkout" class="nav-link">checkout</a>
<ul class="nav flex-column">
</ul>
</li>
<li class="nav-item" data-level="2"><a href="#clone" class="nav-link">clone</a>
<ul class="nav flex-column">
</ul>
</li>
<li class="nav-item" data-level="2"><a href="#commit" class="nav-link">commit</a>
<ul class="nav flex-column">
</ul>
</li>
<li class="nav-item" data-level="2"><a href="#diff" class="nav-link">diff</a>
<ul class="nav flex-column">
</ul>
</li>
<li class="nav-item" data-level="2"><a href="#fetch" class="nav-link">fetch</a>
<ul class="nav flex-column">
</ul>
</li>
<li class="nav-item" data-level="2"><a href="#init" class="nav-link">init</a>
<ul class="nav flex-column">
</ul>
</li>
<li class="nav-item" data-level="2"><a href="#log" class="nav-link">log</a>
<ul class="nav flex-column">
</ul>
</li>
<li class="nav-item" data-level="2"><a href="#merge" class="nav-link">merge</a>
<ul class="nav flex-column">
</ul>
</li>
<li class="nav-item" data-level="2"><a href="#pull" class="nav-link">pull</a>
<ul class="nav flex-column">
</ul>
</li>
<li class="nav-item" data-level="2"><a href="#push" class="nav-link">push</a>
<ul class="nav flex-column">
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div></div>
<div class="col-md-9" role="main">
<h1 id="common-commands">common commands<a class="headerlink" href="#common-commands" title="Permanent link">&para;</a></h1>
<p>These are commands and options that I use frequently. See the linked documentation for more options and information.</p>
<hr />
<h2 id="add"><a href="https://git-scm.com/docs/git-add">add</a><a class="headerlink" href="#add" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git add [--all] [&lt;files&gt;]</code></p>
</blockquote>
<p>Add files from the working tree to the staging area. <a href="#commit"><code>commit</code></a> operates on the staging area.</p>
<p>Try interactive staging: <code>git add -i</code></p>
<h2 id="branch"><a href="https://git-scm.com/docs/git-commit">branch</a><a class="headerlink" href="#branch" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git branch [&lt;branch&gt;]</code></p>
</blockquote>
<p>Show branches and create new ones.</p>
<h2 id="checkout"><a href="https://git-scm.com/docs/git-checkout">checkout</a><a class="headerlink" href="#checkout" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git checkout [&lt;branch&gt;]</code></p>
</blockquote>
<p>Switch branches or restore working tree files.</p>
<p>The <code>-b</code> switch creates the branch if it doesn't exist and switches to it.</p>
<h2 id="clone"><a href="https://git-scm.com/docs/git-clone">clone</a><a class="headerlink" href="#clone" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git clone [&lt;remote_url&gt;] [directory_to_clone_into]</code></p>
</blockquote>
<p>Download a copy of a remote repository from a server.</p>
<h2 id="commit"><a href="https://git-scm.com/docs/git-commit">commit</a><a class="headerlink" href="#commit" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git commit [-a] [-m "commit message here"]</code></p>
</blockquote>
<p>Create a snapshot of the staging area. Record staged changes.</p>
<p>The <code>-a</code> switch will add all modified and deleted files (will not pick up new files that have been added to the repo) to the staging area before committing.</p>
<h2 id="diff"><a href="https://git-scm.com/docs/git-diff">diff</a><a class="headerlink" href="#diff" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git diff [&lt;from_path-spec&gt;]..[&lt;to_path-spec&gt;]</code></p>
</blockquote>
<p>Show changes by line. With no arguments, <code>diff</code> shows all unstaged changes.</p>
<p>Other uses would be comparing changes between branches or between a range of commits (referred to by their hashes, which you can see with <a href="#log"><code>log</code></a>).</p>
<h2 id="fetch"><a href="https://git-scm.com/docs/git-fetch">fetch</a><a class="headerlink" href="#fetch" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git fetch [&lt;repository&gt; [&lt;refspec&gt;]]</code></p>
</blockquote>
<p>Download objects and refs from another (usually remote) repository.</p>
<h2 id="init"><a href="https://git-scm.com/docs/git-init">init</a><a class="headerlink" href="#init" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git init [--bare] [--shared] [&lt;directory&gt;]</code></p>
</blockquote>
<p>Create an empty repo or reinitialize an existing one.</p>
<h2 id="log"><a href="https://git-scm.com/docs/git-log">log</a><a class="headerlink" href="#log" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git log [&lt;revision range&gt;]</code></p>
</blockquote>
<p>Show commit logs. There are a lot of options here. Check out the documentation.</p>
<p>Here are some of the log options that I use often. Some tips were taken from <a href="https://github.com/git-tips/tips">git-tips</a></p>
<p>All commits since forking from master
* <code>git log --no-merges --stat --reverse master..</code></p>
<p>Visualize the history as a tree
* <code>git log --pretty=oneline --graph --decorate --all</code></p>
<p><img alt="pretty oneline" src="../img/pretty-oneline.png" /></p>
<p>List changes specific to a certain file
* <code>git log --follow -p -- &lt;file_path&gt;</code></p>
<h2 id="merge"><a href="https://git-scm.com/docs/git-merge">merge</a><a class="headerlink" href="#merge" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git merge [&lt;branch&gt;]</code></p>
</blockquote>
<p>Join two or more development histories together.</p>
<p><code>merge</code> incorporates changes from the named commits provided as arguments into the current branch.</p>
<p>Sometimes you will have changed the same line as someone else. This will result in a merge conflict. Some GUI clients have tools to help resolve merge conflicts, but it is good to know how to do it manually.</p>
<pre><code>Here are lines that are either unchanged from the common
ancestor, or cleanly resolved because only one side changed.
&lt;&lt;&lt;&lt;&lt;&lt;&lt; yours:sample.txt
Conflict resolution is hard;
let's go shopping.
=======
Git makes conflict resolution easy.
&gt;&gt;&gt;&gt;&gt;&gt;&gt; theirs:sample.txt
And here is another line that is cleanly resolved or unmodified.
</code></pre>
<ul>
<li>This is what you will see as the result of a merge conflict. Simply keep the lines you would like, remove the conflict markers, and commit the result.</li>
</ul>
<p>See the documentation for more info on how to resolve merge conflicts.</p>
<h2 id="pull"><a href="https://git-scm.com/docs/git-pull">pull</a><a class="headerlink" href="#pull" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git pull [--rebase] [--ff] [--no-ff] [&lt;remote name&gt;] [&lt;branch name&gt;]</code></p>
</blockquote>
<p><code>pull</code> is a synonym for <a href="#fetch"><code>fetch</code></a> followed immediately by a <a href="#merge"><code>merge</code></a>. It's a nice shortcut for that.</p>
<p>Use the <code>--rebase</code> option when you haven't made any changes and want to get the latest changes that others have pushed to the remote.</p>
<p>To sync with remote and overwrite local changes:</p>
<blockquote>
<p><code>git fetch origin &amp;&amp; git reset --hard origin/master &amp;&amp; git clean -f -d</code></p>
</blockquote>
<h2 id="push"><a href="https://git-scm.com/docs/git-push">push</a><a class="headerlink" href="#push" title="Permanent link">&para;</a></h2>
<blockquote>
<p><code>git push [-u] [&lt;remote name&gt;] [&lt;branch name&gt;]</code></p>
</blockquote>
<p>The <code>-u</code> option will set the following branch and remote to the default upstream. This means you can simply do <code>git pull</code> without the <a href="../glossary/#refspec">refspec</a>.</p></div>
</div>
</div>
<footer class="col-md-12">
<hr>
<p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
</footer>
<script>
var base_url = "..",
shortcuts = {"help": 191, "next": 78, "previous": 80, "search": 83};
</script>
<script src="../js/base.js" defer></script>
<script src="../search/main.js" defer></script>
<div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="searchModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="searchModalLabel">Search</h4>
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
</div>
<div class="modal-body">
<p>
From here you can search these documents. Enter
your search terms below.
</p>
<form>
<div class="form-group">
<input type="search" class="form-control" placeholder="Search..." id="mkdocs-search-query" title="Type search term here">
</div>
</form>
<div id="mkdocs-search-results"></div>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="keyboardModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="keyboardModalLabel">Keyboard Shortcuts</h4>
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
</div>
<div class="modal-body">
<table class="table">
<thead>
<tr>
<th style="width: 20%;">Keys</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td class="help shortcut"><kbd>?</kbd></td>
<td>Open this help</td>
</tr>
<tr>
<td class="next shortcut"><kbd>n</kbd></td>
<td>Next page</td>
</tr>
<tr>
<td class="prev shortcut"><kbd>p</kbd></td>
<td>Previous page</td>
</tr>
<tr>
<td class="search shortcut"><kbd>s</kbd></td>
<td>Search</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
</body>
</html>