Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
285 lines (246 sloc) 15 KB
<!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">
<title>Avoid committing junk</title>
<link href="http://anotherdatum.com/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Another Datum Full Atom Feed" />
<!-- Bootstrap Core CSS -->
<link href="http://anotherdatum.com/theme/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="http://anotherdatum.com/theme/css/clean-blog.min.css" rel="stylesheet">
<!-- Code highlight color scheme -->
<link href="http://anotherdatum.com/theme/css/code_blocks/tomorrow.css" rel="stylesheet">
<!-- CSS specified by the user -->
<link href="http://anotherdatum.com/css/overrides.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<meta name="description" content="Have you ever accidentaly pushed temporary stuff to the remote git server? Then keep reading.">
<meta name="author" content="Yoel Zeldes">
<meta name="tags" content="git">
<meta property="og:locale" content="en">
<meta property="og:site_name" content="Another Datum">
<meta property="og:type" content="article">
<meta property="article:author" content="http://anotherdatum.com/author/yoel-zeldes.html">
<meta property="og:url" content="http://anotherdatum.com/avoid-committing-junk.html">
<meta property="og:title" content="Avoid committing junk">
<meta property="article:published_time" content="2016-11-10 21:00:00+02:00">
<meta property="og:description" content="Have you ever accidentaly pushed temporary stuff to the remote git server? Then keep reading.">
<meta property="og:image" content="http://anotherdatum.com/images/avoid-committing-junk/cover.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@YZeldes">
<meta name="twitter:title" content="Avoid committing junk">
<meta name="twitter:image" content="http://anotherdatum.com/images/avoid-committing-junk/cover.png">
<meta name="twitter:description" content="Have you ever accidentaly pushed temporary stuff to the remote git server? Then keep reading.">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://anotherdatum.com/">Another Datum</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li><a href="http://anotherdatum.com">Posts</a></li>
<li><a href="http://anotherdatum.com/pages/about.html">about me</a></li>
<li><a href="http://anotherdatum.com/pages/resources.html">Resources</a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Page Header -->
<header class="intro-header" style="background-image: url('images/avoid-committing-junk/cover.png')">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="post-heading">
<h1>Avoid committing junk</h1>
<span class="meta">Posted on 10 November 2016</span>
</div>
</div>
</div>
</div>
</header>
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<!-- Post Content -->
<article>
<p>In the development process every developer writes stuff he doesn't intend to commit and push to the remote server,
e.g. debug prints. It happens to all of us every now and then: we forget to remove this temporary stuff before committing...</p>
<p>I solved this somewhat embarrassing situation using a simple approach: to every line I don't want to accidentally commit
I add the magic characters sequence <em>xxx</em>. This sequence can be in any part of the line: inside a comment, as a variable name,
as a function name, you name it. A few usage examples:</p>
<ul>
<li>Debug print: <code>print 'xxx reached this line'</code>.</li>
<li>Variable used for debugging: <code>xxxCounter = 0</code>.</li>
<li>Temporary function: <code>def xxxPrintDebugInfo():</code>.</li>
<li>TODO which must be attended before committing: <code># TODO: don't forget to refactor this function xxx</code>.</li>
</ul>
<p>The way I implemented it is using git hooks, which is git's mechanism to fire off custom scripts when certain important actions occur.
I used the pre-commit hook for validating the commit's content.</p>
<p>Just create a file with the name <code>.git/hooks/pre-commit</code> with the following content:</p>
<div class="highlight"><pre><span></span><span class="ch">#!/bin/sh</span>
<span class="nv">marks</span><span class="o">=</span>xxx,aaa,asd
<span class="nv">marksRegex</span><span class="o">=</span><span class="sb">`</span><span class="nb">echo</span> <span class="s2">&quot;(</span><span class="nv">$marks</span><span class="s2">)&quot;</span> <span class="p">|</span> sed -r <span class="s1">&#39;s/,/|/g&#39;</span><span class="sb">`</span>
<span class="nv">marksMessage</span><span class="o">=</span><span class="sb">`</span><span class="nb">echo</span> <span class="s2">&quot;</span><span class="nv">$marks</span><span class="s2">&quot;</span> <span class="p">|</span> sed -r <span class="s1">&#39;s/,/ or /g&#39;</span><span class="sb">`</span>
<span class="k">if</span> git diff --staged <span class="p">|</span> egrep -q <span class="s2">&quot;^\+.*</span><span class="nv">$marksRegex</span><span class="s2">&quot;</span><span class="p">;</span> <span class="k">then</span>
<span class="nb">echo</span> <span class="s2">&quot;You forgot to remove a line containing </span><span class="nv">$marksMessage</span><span class="s2">. You can forecully commit using \&quot;commit -n\&quot;&quot;</span>
<span class="nb">exit</span> <span class="m">1</span>
<span class="k">fi</span>
</pre></div>
<ol>
<li><code>marks</code> contains the characters sequences which are not allowed to be committed.</li>
<li><code>git diff --staged</code> shows the changes which will be committed. The changes pass through a regular expression
that searches for any forbidden mark (using <code>egrep</code>).</li>
<li>If a forbidden mark is found, the script exits with an error code, causing the commit to fail. </li>
</ol>
<p>If you want to bypass the hook (e.g. you want to commit a binary file such as an image, which may contain a forbidden mark), you can <code>commit -n</code>.</p>
</article>
<div class="tags">
<p>tags: <a href="http://anotherdatum.com/tag/git.html">git</a></p>
</div>
<hr>
<!-- Begin MailChimp Signup Form -->
<link href="//cdn-images.mailchimp.com/embedcode/classic-10_7.css" rel="stylesheet" type="text/css">
<style type="text/css">
#mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; width:300px;}
#mc_embed_signup form{padding: 0;}
/* Add your own MailChimp form style overrides in your site stylesheet or in this style block.
We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */
</style>
<div id="mc_embed_signup">
<form action="https://anotherdatum.us14.list-manage.com/subscribe/post?u=6894d7badcfb253606fa3fb54&amp;id=c6f34ad6b7" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
<div id="mc_embed_signup_scroll">
<h2>Get updated of new posts</h2>
<div class="mc-field-group">
<label for="mce-EMAIL">Email Address </label>
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
</div>
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div> <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
<div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_6894d7badcfb253606fa3fb54_c6f34ad6b7" tabindex="-1" value=""></div>
<div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
</div>
</form>
</div>
<script type='text/javascript' src='//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script><script type='text/javascript'>(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';fnames[2]='LNAME';ftypes[2]='text';}(jQuery));var $mcj = jQuery.noConflict(true);</script>
<!--End mc_embed_signup-->
<hr />
<div class="comments">
<h2>Comments !</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'anotherdatum';
var disqus_identifier = 'avoid-committing-junk.html';
var disqus_url = 'http://anotherdatum.com/avoid-committing-junk.html';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//anotherdatum.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the comments.</noscript>
</div>
</div>
</div>
</div>
<hr>
<!-- Footer -->
<footer>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<ul class="list-inline text-center">
<li>
<a href="https://il.linkedin.com/in/yoelzeldes">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-linkedin fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<li>
<a href="https://github.com/yoel-zeldes">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-github fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<li>
<a href="https://www.facebook.com/yoel.zeldes">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-facebook fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<li>
<a href="https://twitter.com/YZeldes">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-twitter fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
</ul>
<p class="copyright text-muted">
Blog powered by <a href="http://getpelican.com">Pelican</a>,
which takes great advantage of <a href="http://python.org">Python</a>.
<br />
Blog sources can be found <a href="https://github.com/yoel-zeldes/yoel-zeldes.github.io">here</a>.
</p> </div>
</div>
</div>
</footer>
<!-- jQuery -->
<script src="http://anotherdatum.com/theme/js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="http://anotherdatum.com/theme/js/bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="http://anotherdatum.com/theme/js/clean-blog.min.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-83684090-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript">
var disqus_shortname = 'anotherdatum';
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
</body>
</html>