zigford.org/git---working-with-branches.html
2020-07-21 06:49:32 +10:00

99 lines
4.4 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="main.css" type="text/css" />
<link rel="stylesheet" href="blog.css" type="text/css" />
<link rel="alternate" type="application/rss+xml" title="Subscribe to this page..." href="feed.rss" />
<title>Git - Working with branches</title>
</head><body>
<div id="divbodyholder">
<div class="headerholder"><div class="header">
<div id="title">
<h1 class="nomargin"><a class="ablack" href="http://zigford.org/index.html">zigford.org</a></h1>
<div id="description"><a href="about.html">About</a><a href="links.html"> | Links</a><a href="scripts.html"> | Scripts</a><br>Sharing linux/windows scripts and tips</br></div>
</div></div></div>
<div id="divbody"><div class="content">
<!-- entry begin -->
<h3><a class="ablack" href="git---working-with-branches.html">
Git - Working with branches
</a></h3>
<!-- bashblog_timestamp: #201908061724.51# -->
<div class="subtitle">August 06, 2019 &mdash;
Jesse Harris
</div>
<!-- text begin -->
<p>In this quick article, I will:</p>
<ul>
<li>quickly create a branch on a git repository.</li>
<li>make some commits</li>
<li>push them to a remote branch</li>
<li>clone to a new location (to simulate working on another machine)</li>
<li>merge the new branch to master</li>
<li>delete the local and remote branches</li>
</ul>
<hr />
<p>Reminder - This site is mainly for my memory and reference. You can find this
information anywhere out on the web, but I find the best way to remember
something is to write it down yourself.</p>
<p>Firstly, clone a repo and cd into it.</p>
<p><strong>make a new local branch</strong></p>
<pre><code> git checkout -b updates/onedrive
</code></pre>
<p>Now, make some commits and we will push this to a new remote branch like this:</p>
<p><strong>push to remote branch</strong></p>
<pre><code> git push -u origin updates/onedrive
</code></pre>
<p><em>note, the remote and local branch names need not match</em><br />
<em>second note, <code>-u</code> stands for --set-upstream-to</em></p>
<p>Next, go to another computer where you will resume work. (or for the sake of
practice, just clone again to another directory)
On the new clone, we need to fetch all other branches</p>
<p><strong>download all branches</strong></p>
<pre><code> git fetch origin
</code></pre>
<p><strong>create a local branch, pull the remote branch to it</strong></p>
<pre><code> git checkout -b updates/onedrive
git pull origin updates/onedrive
</code></pre>
<p>Here we can examine the branch, continue to make changes and commits. If we want
to push back to the remote branch, we need to set the upstream:</p>
<pre><code> git push -u origin updates/onedrive
</code></pre>
<p>When we are done, perhaps we want to merge the changes back to master. In that case:</p>
<p><strong>merge changes to master</strong></p>
<pre><code> git checkout master
git merge updates/onedrive
</code></pre>
<p>Now would be a good time to push changes. Then you can delete the local and
remote branches.</p>
<p><strong>delete local branch</strong></p>
<pre><code> git branch -d updates/onedrive
</code></pre>
<p><strong>delete remote branch</strong></p>
<pre><code> git push --delete origin updates/onedrive
</code></pre>
<p>I hope this information helps me, let alone you!</p>
<p>Helpful links</p>
<p><a href="https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely">stackoverflow</a><br />
<a href="https://stackify.com/git-checkout-remote-branch/">stackify</a><br />
<a href="https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging">git-scm.com</a><br />
<a href="https://www.freecodecamp.org/forum/t/push-a-new-local-branch-to-a-remote-git-repository-and-track-it-too/13222">freecodecamp.org</a></p>
<p>Tags: <a href='tag_git.html'>git</a></p>
<!-- text end -->
<!-- entry end -->
</div>
<div id="footer">&copy <a href="http://twitter.com/zigford_org">Jesse Harris</a> &mdash; <a href="mailto:jesse&#64;zigford&#46;org">jesse&#64;zigford&#46;org</a><br/>
Generated with <a href="https://github.com/cfenollosa/bashblog">bashblog</a>, a single bash script to easily create blogs like this one</div>
</div></div>
</body></html>