her.esy.fun/src/Scratch/en/blog/06_How_I_use_git/index.html

214 lines
14 KiB
HTML
Raw Normal View History

2021-04-18 10:23:24 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>YBlog - Git for self</title>
<meta name="keywords" content="git, svn, workflow" />
<link rel="shortcut icon" type="image/x-icon" href="../../../../Scratch/img/favicon.ico" />
2021-05-25 20:25:47 +00:00
<link rel="stylesheet" type="text/css" href="/css/y.css" />
<link rel="stylesheet" type="text/css" href="/css/legacy.css" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml" />
2021-04-18 10:23:24 +00:00
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="apple-touch-icon" href="../../../../Scratch/img/about/FlatAvatar@2x.png" />
<!--[if lt IE 9]>
<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
<![endif]-->
<!-- IndieAuth -->
<link href="https://twitter.com/yogsototh" rel="me">
<link href="https://github.com/yogsototh" rel="me">
<link href="mailto:yann.esposito@gmail.com" rel="me">
<link rel="pgpkey" href="../../../../pubkey.txt">
</head>
<body lang="en" class="article">
<div id="content">
<div id="header">
<div id="choix">
<span id="choixlang">
<a href="../../../../Scratch/fr/blog/06_How_I_use_git/">French</a>
</span>
<span class="tomenu"><a href="#navigation">↓ Menu ↓</a></span>
<span class="flush"></span>
</div>
</div>
<div id="titre">
<h1>Git for self</h1>
</div>
<div class="flush"></div>
<div id="afterheader" class="article">
<div class="corps">
<div>
<img src="../../../../Scratch/img/blog/06_How_I_use_git/central_architecture.png" alt="central architecture" />
</div>
<p>I use <a href="http://www.git-scm.org/">Git</a> to manage my personnal projects. I have a centralized repository which all my computer should synchronize with. Unfortunately I didnt find clearly what I needed on the official Git documentation.</p>
<p>In two words, if you want to use an SVN workflow with Git (and all its advantages) here is how to proceed.</p>
<hr />
<h2 id="initialisation">Initialisation</h2>
<p>Suppose Ive got a directory on my local computer containing a project I want to manage via Git. Here what to do:</p>
<div>
<div class="sourceCode" id="cb1"><pre class="sourceCode zsh"><code class="sourceCode zsh"><a class="sourceLine" id="cb1-1" title="1"><span class="kw">cd</span> to/project/directory/</a>
<a class="sourceLine" id="cb1-2" title="2">git init</a>
<a class="sourceLine" id="cb1-3" title="3">git add</a>
<a class="sourceLine" id="cb1-4" title="4">git commit</a></code></pre></div>
</div>
<p>Now all files in the <code>to/project/directory/</code> are versionned. If you want not to follow some just edit the file <code>.gitignore</code></p>
<p>for example mine is:</p>
<div>
<div class="sourceCode" id="cb2"><pre class="sourceCode zsh"><code class="sourceCode zsh"><a class="sourceLine" id="cb2-1" title="1">*.swp</a>
<a class="sourceLine" id="cb2-2" title="2">.DS_Store</a>
<a class="sourceLine" id="cb2-3" title="3">ikog.py.bak</a>
<a class="sourceLine" id="cb2-4" title="4">output/Scratch/assets</a>
<a class="sourceLine" id="cb2-5" title="5">output/Scratch/en</a>
<a class="sourceLine" id="cb2-6" title="6">output/Scratch/fr</a>
<a class="sourceLine" id="cb2-7" title="7">output/Scratch/multi</a></code></pre></div>
</div>
<p>Next, you want to put your project on a directory accessible from the web:</p>
<div>
<div class="sourceCode" id="cb3"><pre class="sourceCode zsh"><code class="sourceCode zsh"><a class="sourceLine" id="cb3-1" title="1">git <span class="kw">clone</span> --bare <span class="kw">.</span> /path/to/repository</a></code></pre></div>
</div>
<p>Now on any computer you can do:</p>
<div>
<div class="sourceCode" id="cb4"><pre class="sourceCode zsh"><code class="sourceCode zsh"><a class="sourceLine" id="cb4-1" title="1">git <span class="kw">clone</span> protocol://path/to/repository local_directory</a></code></pre></div>
</div>
<p>and <code>local_directory</code> will contain an up-to-date project.</p>
<div class="encadre">
<p><em> You should make this operation also on the computer used to create the repository. Just to verify all will be okay.</p>
<p></em></p>
</div>
<hr />
<h2 id="the-workflow">The workflow</h2>
<p>To resume you now have one repository on the Internet, and one or many computer associated with it. Now, what you want is to synchronize everything.</p>
<p>Before begining your work, the first thing to do is to get all modification from the Internet to your local host:</p>
<div>
<div class="sourceCode" id="cb5"><pre class="sourceCode zsh"><code class="sourceCode zsh"><a class="sourceLine" id="cb5-1" title="1">git pull</a></code></pre></div>
</div>
<p>After that you can do (many times):</p>
<div>
<div class="sourceCode" id="cb6"><pre class="sourceCode zsh"><code class="sourceCode zsh"><a class="sourceLine" id="cb6-1" title="1">hack, hack, hack...</a>
<a class="sourceLine" id="cb6-2" title="2">git add some files</a>
<a class="sourceLine" id="cb6-3" title="3">git commit</a></code></pre></div>
</div>
<p>When you want your local modification to be on the Internet just do a simple:</p>
<div>
<div class="sourceCode" id="cb7"><pre class="sourceCode zsh"><code class="sourceCode zsh"><a class="sourceLine" id="cb7-1" title="1">git push</a></code></pre></div>
</div>
<p>All should be ok.</p>
<p>If you have some trouble with the <code>push</code> and <code>pull</code> verify your <code>.git/config</code> file ; it should contain the following lines:</p>
<div>
<div class="sourceCode" id="cb8"><pre class="sourceCode zsh"><code class="sourceCode zsh"><a class="sourceLine" id="cb8-1" title="1">...</a>
<a class="sourceLine" id="cb8-2" title="2">[remote <span class="st">&quot;origin&quot;</span>]</a>
<a class="sourceLine" id="cb8-3" title="3"> url = protocol://url/of/the/repository</a>
<a class="sourceLine" id="cb8-4" title="4"> fetch = +refs/heads/*:refs/remotes/origin/*</a>
<a class="sourceLine" id="cb8-5" title="5">[branch <span class="st">&quot;master&quot;</span>]</a>
<a class="sourceLine" id="cb8-6" title="6"> remote = origin</a>
<a class="sourceLine" id="cb8-7" title="7"> merge = refs/heads/master</a>
<a class="sourceLine" id="cb8-8" title="8">...</a></code></pre></div>
</div>
<h2 id="branches-synchronisation">Branches Synchronisation</h2>
<p>Well, now, all seems ok, but you have to worry about two little things. Git is all about decentralisation and branches. It is very easy to manage one branch, or many branches on the same host. But synchronize branches on many hosts is not a natural operation.</p>
<p>This is why I created two simple scripts to automate this. One for creating a branch locally and remotely. And one to get remotely created branched on your local host.</p>
<p>Then when you want to create a new branch (locally and remotely) ; you simply have to do a:</p>
<div>
<code class="zsh">git-create-new-branch branch_name</code>
</div>
<p>and when you are on another computer and want to get locally all the remote branches you execute:</p>
<div>
<code class="zsh">git-get-remote-branches</code>
</div>
<p>Here are the code of theese two scripts:</p>
<div>
<div class="sourceCode" id="cb9"><pre class="sourceCode zsh"><code class="sourceCode zsh"><a class="sourceLine" id="cb9-1" title="1"><span class="co">#!/usr/bin/env zsh</span></a>
<a class="sourceLine" id="cb9-2" title="2"></a>
<a class="sourceLine" id="cb9-3" title="3"><span class="kw">if</span> <span class="kw">((</span><span class="ot">$#</span>&lt;1<span class="kw">))</span>; <span class="kw">then</span></a>
<a class="sourceLine" id="cb9-4" title="4"> <span class="kw">print</span> -- <span class="st">&quot;usage: </span><span class="ot">$0</span><span class="st">:t branch_name&quot;</span> <span class="kw">&gt;&amp;2</span></a>
<a class="sourceLine" id="cb9-5" title="5"> <span class="kw">exit</span> 1</a>
<a class="sourceLine" id="cb9-6" title="6"><span class="kw">fi</span></a>
<a class="sourceLine" id="cb9-7" title="7"></a>
<a class="sourceLine" id="cb9-8" title="8"><span class="ot">branch=$1</span></a>
<a class="sourceLine" id="cb9-9" title="9">git br <span class="ot">${branch}</span></a>
<a class="sourceLine" id="cb9-10" title="10">git <span class="kw">co</span> <span class="ot">${branch}</span></a>
<a class="sourceLine" id="cb9-11" title="11">git config branch.<span class="ot">${branch}</span>.remote origin</a>
<a class="sourceLine" id="cb9-12" title="12">git config branch.<span class="ot">${branch}</span>.merge refs/heads/<span class="ot">${branch}</span></a></code></pre></div>
</div>
<div>
<div class="sourceCode" id="cb10"><pre class="sourceCode zsh"><code class="sourceCode zsh"><a class="sourceLine" id="cb10-1" title="1"><span class="co">#!/usr/bin/env zsh</span></a>
<a class="sourceLine" id="cb10-2" title="2"></a>
<a class="sourceLine" id="cb10-3" title="3"><span class="co"># recup branches not on local</span></a>
<a class="sourceLine" id="cb10-4" title="4"><span class="ot">localbranches=(</span> <span class="ot">$(</span>git br <span class="kw">|</span> <span class="kw">sed</span> <span class="st">'s/\*/ /'</span><span class="ot">)</span> <span class="ot">)</span></a>
<a class="sourceLine" id="cb10-5" title="5"><span class="ot">remoteMissingBranches=(</span> <span class="ot">$(</span>git br -r <span class="kw">|</span> <span class="kw">\</span></a>
<a class="sourceLine" id="cb10-6" title="6"> <span class="kw">egrep</span> -v <span class="st">&quot;origin/HEAD|(${(j:|:)localbranches})&quot;</span> <span class="ot">)</span> <span class="ot">)</span></a>
<a class="sourceLine" id="cb10-7" title="7"><span class="kw">for</span> br <span class="kw">in</span> <span class="ot">$remoteMissingBranches</span>; <span class="kw">do</span></a>
<a class="sourceLine" id="cb10-8" title="8"> <span class="ot">branch=${br#</span>origin/<span class="ot">}</span></a>
<a class="sourceLine" id="cb10-9" title="9"> <span class="kw">print</span> <span class="st">&quot;get remote branch </span><span class="ot">$branch</span><span class="st">&quot;</span></a>
<a class="sourceLine" id="cb10-10" title="10"> git br <span class="ot">${branch}</span></a>
<a class="sourceLine" id="cb10-11" title="11"> git config branch.<span class="ot">${branch}</span>.remote origin</a>
<a class="sourceLine" id="cb10-12" title="12"> git config branch.<span class="ot">${branch}</span>.merge refs/heads/<span class="ot">${branch}</span></a>
<a class="sourceLine" id="cb10-13" title="13"><span class="kw">done</span></a></code></pre></div>
</div>
</div>
<div id="afterarticle">
<div id="social">
2021-05-25 20:25:47 +00:00
<a href="/rss.xml" target="_blank" rel="noopener noreferrer nofollow" class="social">RSS</a>
2021-04-18 10:23:24 +00:00
·
<a href="https://twitter.com/home?status=http%3A%2F%2Fyannesposito.com/Scratch/en/blog/06_How_I_use_git/%20via%20@yogsototh" target="_blank" rel="noopener noreferrer nofollow" class="social">Tweet</a>
·
<a href="http://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fyannesposito.com/Scratch/en/blog/06_How_I_use_git/" target="_blank" rel="noopener noreferrer nofollow" class="social">FB</a>
<br />
<a class="message" href="../../../../Scratch/en/blog/Social-link-the-right-way/">These social sharing links preserve your privacy</a>
</div>
<div id="navigation">
<a href="../../../../">Home</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/blog">Blog</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/softwares">Softwares</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/about">About</a>
</div>
<div id="totop"><a href="#header">↑ Top ↑</a></div>
<div id="bottom">
<div>
Published on 2009-08-18
</div>
<div>
<a href="https://twitter.com/yogsototh">Follow @yogsototh</a>
</div>
<div>
<a rel="license" href="http://creativecommons.org/licenses/by/3.0/deed.en_US">Yann Esposito©</a>
</div>
<div>
Done with
<a href="http://www.vim.org" target="_blank" rel="noopener noreferrer nofollow"><strike>Vim</strike></a>
<a href="http://spacemacs.org" target="_blank" rel="noopener noreferrer nofollow">spacemacs</a>
<span class="pala">&amp;</span>
<a href="http://nanoc.ws" target="_blank" rel="noopener noreferrer nofollow"><strike>nanoc</strike></a>
<a href="http://jaspervdj.be/hakyll" target="_blank" rel="noopener noreferrer nofollow">Hakyll</a>
</div>
<hr />
<div style="max-width: 100%">
<a href="https://cardanohub.org">
<img src="../../../../Scratch/img/ada-logo.png" class="simple" style="height: 16px;
border-radius: 50%;
vertical-align:middle;
display:inline-block;" />
ADA:
</a>
<code style="display:inline-block;
word-wrap:break-word;
text-align: left;
vertical-align: top;
max-width: 85%;">
DdzFFzCqrhtAvdkmATx5Fm8NPJViDy85ZBw13p4XcNzVzvQg8e3vWLXq23JQWFxPEXK6Kvhaxxe7oJt4VMYHxpA2vtCFiP8fziohN6Yp
</code>
</div>
</div>
</div>
</div>
</div>
</body>
</html>