A place to cache linked articles (think custom and personal wayback machine)
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.

4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. title: Home .git
  2. url: https://martinovic.blog/post/home_git/
  3. hash_url: 4be675592f4f6dcf636812263e9e1233
  4. <p>Git is an incredibly useful tool for a programmer, you use it to version the work you do and distribute it easily on other computers.
  5. But over the years, I&rsquo;ve found that a lot of my workflow depends on various configurations and helper scripts I have in my path.
  6. So why not version those as well, it does allow you to get started in seconds in a very familiar environment on every new computer or even a server if you spend a lot of your time in SSH sessions.
  7. Bonus points for making it a public repository so others can take a look at your stuff and possibly get inspiration for improvements in their workflow, just make sure you don&rsquo;t commit any private keys.</p>
  8. <h1 id="the-setup">The setup</h1>
  9. <p>Feel free to check out my home repository over <a href="https://github.com/tophatcroat/home">here</a>. As you can see, it contains my <code>.zshrc</code>, <code>.vimrc</code> a <code>bin</code> directory with various scripts, dotfiles and some other stuff I&rsquo;ve curated while doing what I do best, fidgeting around a computer.</p>
  10. <p>You can start your own version by simply initializing a Git repository in your home directory.</p>
  11. <div class="highlight"><pre style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"><span style="color:#111">cd</span> ~ <span style="color:#f92672">&amp;&amp;</span> git init</code></pre></div>
  12. <p>Now your directory will be full of stuff you probably don&rsquo;t want to commit, like your downloads, pictures, private keys, shell history or what have you.
  13. So it&rsquo;s important to set up the <code>~/.gitignore</code> file immediately to stop you from accidentally committing stuff you didn&rsquo;t want to.</p>
  14. <p>Most importantly, you should ignore everything by default in your <code>~/.gitignore</code>:</p>
  15. <div class="highlight"><pre style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"><span style="color:#75715e"># Blacklist all in this folder</span>
  16. /*
  17. <span style="color:#75715e"># Except for these</span>
  18. !.gitignore
  19. !.gitconfig
  20. !.notes/
  21. !bin/
  22. !Development/Sh</code></pre></div>
  23. <p>This will instruct Git to keep its hands off all files except for the <code>.gitignore</code>, <code>.gitconfig</code> and the files in the <code>.notes</code>, <code>bin</code> and <code>Development/Sh</code> directories.
  24. So now, when you want to add any other file or directory you will require a force flag like so</p>
  25. <div class="highlight"><pre style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">git add -f path_to_file</code></pre></div>
  26. <p>And that&rsquo;s basically all you need to do to set this up, it&rsquo;s now up to you to add and commit all you want and push it to the Git hosting service of your choice.</p>
  27. <h1 id="the-cloning">The cloning</h1>
  28. <p>So now it&rsquo;s time to clone this into your new home directory, but if you try cloning it using <code>git clone</code> command it will complain that it can&rsquo;t do that in an non empty directory.
  29. To get around this, you need to initialize it first then manually add the remote and finally force a checkout like so:</p>
  30. <div class="highlight"><pre style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"><span style="color:#111">cd</span> ~
  31. <span style="color:#75715e"># Create an empty git repository in your home folder</span>
  32. git init
  33. <span style="color:#75715e"># Add the remote</span>
  34. git remote add origin https://github.com/TopHatCroat/home
  35. <span style="color:#75715e"># Get the stuff from up there</span>
  36. git fetch
  37. <span style="color:#75715e"># Be careful! This will overwrite any local files existing on remote!</span>
  38. git reset --hard origin/master</code></pre></div>
  39. <p>Running the last command will basically checkout all the files from the origin master branch and overwrite any files with the same path, so be careful if you already did some setup on the new machine.</p>
  40. <p>And that&rsquo;s it, you&rsquo;ve saved yourself the bother of copying over the config files.
  41. The only thing that remains is to install the tools that use the configs you&rsquo;ve just brought over.
  42. If you are feeling adventurous you could make a script that will do that for you like I did in <a href="https://github.com/TopHatCroat/home/blob/master/Development/Sh/setup.sh">here</a>, but that is up to you.</p>
  43. <p><em>EDIT:</em> As a response to this blog post others have pointed out <a href="https://www.atlassian.com/git/tutorials/dotfiles">another viable solution</a>, or by using <a href="https://protesilaos.com/codelog/gnu-stow-dotfiles/">GNU stow</a>, or <a href="https://github.com/technicalpickles/homesick">homesick</a> or <a href="https://github.com/thoughtbot/rcm">rcm</a>.
  44. Still, the solution presented here is the simplest to setup and understand and it does not depend on any other tool besides Git.
  45. All in all, there are a bunch of ways to do this and a lot more resources are available <a href="https://dotfiles.github.io/">here</a>.</p>