You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
14 KiB

<!DOCTYPE html>
<html lang="en">
<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" />
<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" />
<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=""></script>
<!-- IndieAuth -->
<link href="" rel="me">
<link href="" rel="me">
<link href="" rel="me">
<link rel="pgpkey" href="../../../../pubkey.txt">
<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 class="tomenu"><a href="#navigation">↓ Menu ↓</a></span>
<span class="flush"></span>
<div id="titre">
<h1>Git for self</h1>
<div class="flush"></div>
<div id="afterheader" class="article">
<div class="corps">
<img src="../../../../Scratch/img/blog/06_How_I_use_git/central_architecture.png" alt="central architecture" />
<p>I use <a href="">Git</a> to manage my personnal projects. I have a centralized repository which all my computer should synchronize with. Unfortunately I didn’t 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 I’ve got a directory on my local computer containing a project I want to manage via Git. Here what to do:</p>
<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>
<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 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"></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>
<p>Next, you want to put your project on a directory accessible from the web:</p>
<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>
<p>Now on any computer you can do:</p>
<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>
<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>
<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 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>
<p>After that you can do (many times):</p>
<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>
<p>When you want your local modification to be on the Internet just do a simple:</p>
<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>
<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 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>
<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>
<code class="zsh">git-create-new-branch branch_name</code>
<p>and when you are on another computer and want to get locally all the remote branches you execute:</p>
<code class="zsh">git-get-remote-branches</code>
<p>Here are the code of theese two scripts:</p>
<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 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 id="afterarticle">
<div id="social">
<a href="/rss.xml" target="_blank" rel="noopener noreferrer nofollow" class="social">RSS</a>
<a href="" target="_blank" rel="noopener noreferrer nofollow" class="social">Tweet</a>
<a href="" 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 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 id="totop"><a href="#header">↑ Top ↑</a></div>
<div id="bottom">
Published on 2009-08-18
<a href="">Follow @yogsototh</a>
<a rel="license" href="">Yann Esposito©</a>
Done with
<a href="" target="_blank" rel="noopener noreferrer nofollow"><strike>Vim</strike></a>
<a href="" target="_blank" rel="noopener noreferrer nofollow">spacemacs</a>
<span class="pala">&amp;</span>
<a href="" target="_blank" rel="noopener noreferrer nofollow"><strike>nanoc</strike></a>
<a href="" target="_blank" rel="noopener noreferrer nofollow">Hakyll</a>
<hr />
<div style="max-width: 100%">
<a href="">
<img src="../../../../Scratch/img/ada-logo.png" class="simple" style="height: 16px;
border-radius: 50%;
display:inline-block;" />
<code style="display:inline-block;
text-align: left;
vertical-align: top;
max-width: 85%;">