oliverdavies.uk/content/node.042e4d0c-f835-47dd-8075-745df0a7f8f2.yml

132 lines
9.1 KiB
YAML

uuid:
- value: 042e4d0c-f835-47dd-8075-745df0a7f8f2
langcode:
- value: en
type:
- target_id: daily_email
target_type: node_type
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
revision_timestamp:
- value: '2025-05-11T09:00:57+00:00'
revision_uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
revision_log: { }
status:
- value: true
uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
title:
- value: "How I've configured Git"
created:
- value: '2022-08-24T00:00:00+00:00'
changed:
- value: '2025-05-11T09:00:57+00:00'
promote:
- value: false
sticky:
- value: false
default_langcode:
- value: true
revision_translation_affected:
- value: true
path:
- alias: /daily/2022/08/24/2022-08-24
langcode: en
body:
- value: |
<p>After yesterday's post on why I prefer using Git on the command line rather than using a GUI tool, today I thought that I'd post about how I've configured Git.</p>
<p>First, I rarely ever run the <code>git</code> command - I usually run a <code>g</code> function that I've created within my zsh configuration.</p>
<p>Rather than being an simple alias, it's a shell function that will run <code>git status -sb</code> to show the current status of the repository if there are no additional arguments. If there are, such as when running <code>g add</code>, then this is executed as a normal Git command. (This is something that I first saw from Thoughtbot, if I remember correctly).</p>
<h2 id="using-.gitconfig">Using .gitconfig</h2>
<p>The main part of my configuration is within Git's <code>~/.gitconfig</code> file, where I can configure Git to work how I want.</p>
<p>For example, I like to avoid merge conflicts, so I always want to use fast-forward merges whilst pulling and also to rebase by default. I can do this by adding <code>ff = only</code> and <code>rebase = true</code> to the <code>[pull]</code> section of my <code>~/.gitconfig</code> file.</p>
<p>I can do this manually, or running <code>git config --global pull.rebase true</code> will set the option but also update the file automatically.</p>
<p>Some of the tweaks that I've made are to only allow fast-forward merges by adding <code>merge.ff = only</code>, automatically squash commits when rebasing by setting <code>rebase.autosquash = true</code>, and automatically pruning branches by adding <code>fetch.prune = true</code>.</p>
<h3 id="simple-aliases">Simple aliases</h3>
<p>Another way that I configure Git is using aliases, which are also within the <code>~/.gitconfig</code> file.</p>
<p>For example, if I ran <code>git config --global alias.b "branch"</code>, then running <code>git b</code> would just run <code>git branch</code> which shortens the command and saves some time and keystrokes.</p>
<p>I have similar one- or two letter "short" aliases for pushing and pulling code, and some that also set some additional arguments such as <code>aa</code> for <code>add --all</code> and <code>worktrees</code> for <code>worktree list</code>.</p>
<h3 id="more-complicated-aliases">More complicated aliases</h3>
<p>Aliases can be more complex if needed by prefixing it with a <code>!</code>, meaning that it executes it as a shell command.</p>
<p>This means that I can have <code>repush = !git pull --rebase &amp;&amp; git push</code> to chain two separate Git commands and combine them into one, and <code>ureset = !git reset --hard $(git upstream)</code> which executes the full command, including another alias as part of it.</p>
<p>I also have <code>issues = !gh issue list --web</code> and <code>pulls = !gh pr list --web</code> to open the current repository's GitHub issues or pull requests respectively, which can be done as it's not limited to just running <code>git</code> commands.</p>
<h3 id="custom-functions">Custom functions</h3>
<p>Finally, if an alias is getting too long or complex, then it can extracted to it's own file.</p>
<p>Any executable file within your <code>$PATH</code> that starts with <code>git-</code> will automatically become a Git command.</p>
<p>One example that I have is <a href="https://github.com/opdavies/dotfiles/blob/2b20cd1e59ae3b1fa81074077e855cbdfa02f146/bin/bin/git-cm">git-cm</a> which, similar to the <code>g</code> function`, is a bash script that checks for any arguments passed to it and runs a slightly different command. It achieves the same thing as if it were an alias, but it does make it easier to write and maintain as it's in a separate file.</p>
<p>These are just some examples. If you want to see my entire configuration, then check out <a href="https://github.com/opdavies/dotfiles/tree/2b20cd1e59ae3b1fa81074077e855cbdfa02f146/roles/git/files">my dotfiles repository on GitHub</a>.</p>
<p>How have you configured Git for your workflow? Reply to this email and let me know.</p>
format: full_html
processed: |
<p>After yesterday's post on why I prefer using Git on the command line rather than using a GUI tool, today I thought that I'd post about how I've configured Git.</p>
<p>First, I rarely ever run the <code>git</code> command - I usually run a <code>g</code> function that I've created within my zsh configuration.</p>
<p>Rather than being an simple alias, it's a shell function that will run <code>git status -sb</code> to show the current status of the repository if there are no additional arguments. If there are, such as when running <code>g add</code>, then this is executed as a normal Git command. (This is something that I first saw from Thoughtbot, if I remember correctly).</p>
<h2 id="using-.gitconfig">Using .gitconfig</h2>
<p>The main part of my configuration is within Git's <code>~/.gitconfig</code> file, where I can configure Git to work how I want.</p>
<p>For example, I like to avoid merge conflicts, so I always want to use fast-forward merges whilst pulling and also to rebase by default. I can do this by adding <code>ff = only</code> and <code>rebase = true</code> to the <code>[pull]</code> section of my <code>~/.gitconfig</code> file.</p>
<p>I can do this manually, or running <code>git config --global pull.rebase true</code> will set the option but also update the file automatically.</p>
<p>Some of the tweaks that I've made are to only allow fast-forward merges by adding <code>merge.ff = only</code>, automatically squash commits when rebasing by setting <code>rebase.autosquash = true</code>, and automatically pruning branches by adding <code>fetch.prune = true</code>.</p>
<h3 id="simple-aliases">Simple aliases</h3>
<p>Another way that I configure Git is using aliases, which are also within the <code>~/.gitconfig</code> file.</p>
<p>For example, if I ran <code>git config --global alias.b "branch"</code>, then running <code>git b</code> would just run <code>git branch</code> which shortens the command and saves some time and keystrokes.</p>
<p>I have similar one- or two letter "short" aliases for pushing and pulling code, and some that also set some additional arguments such as <code>aa</code> for <code>add --all</code> and <code>worktrees</code> for <code>worktree list</code>.</p>
<h3 id="more-complicated-aliases">More complicated aliases</h3>
<p>Aliases can be more complex if needed by prefixing it with a <code>!</code>, meaning that it executes it as a shell command.</p>
<p>This means that I can have <code>repush = !git pull --rebase &amp;&amp; git push</code> to chain two separate Git commands and combine them into one, and <code>ureset = !git reset --hard $(git upstream)</code> which executes the full command, including another alias as part of it.</p>
<p>I also have <code>issues = !gh issue list --web</code> and <code>pulls = !gh pr list --web</code> to open the current repository's GitHub issues or pull requests respectively, which can be done as it's not limited to just running <code>git</code> commands.</p>
<h3 id="custom-functions">Custom functions</h3>
<p>Finally, if an alias is getting too long or complex, then it can extracted to it's own file.</p>
<p>Any executable file within your <code>$PATH</code> that starts with <code>git-</code> will automatically become a Git command.</p>
<p>One example that I have is <a href="https://github.com/opdavies/dotfiles/blob/2b20cd1e59ae3b1fa81074077e855cbdfa02f146/bin/bin/git-cm">git-cm</a> which, similar to the <code>g</code> function`, is a bash script that checks for any arguments passed to it and runs a slightly different command. It achieves the same thing as if it were an alias, but it does make it easier to write and maintain as it's in a separate file.</p>
<p>These are just some examples. If you want to see my entire configuration, then check out <a href="https://github.com/opdavies/dotfiles/tree/2b20cd1e59ae3b1fa81074077e855cbdfa02f146/roles/git/files">my dotfiles repository on GitHub</a>.</p>
<p>How have you configured Git for your workflow? Reply to this email and let me know.</p>
summary: null
field_daily_email_cta: { }