|
| 1 | +--- |
| 2 | +title: "Modern Git for Modern Times" |
| 3 | +description: "Modern problems require modern solutions" |
| 4 | +date: "2025-10-17" |
| 5 | +slug: "modern-git" |
| 6 | +--- |
| 7 | + |
| 8 | +Git has been around for a long time — since April 2005. That’s 20 years! |
| 9 | +Fun fact — [Bazaar](https://en.wikipedia.org/wiki/GNU_Bazaar) and |
| 10 | +[Mercurial](https://en.wikipedia.org/wiki/Mercurial) were released around the same time as Git. |
| 11 | +Not so fun facts — during this time |
| 12 | +[Atlassian removed the Mercurial support from Bitbucket](https://www.atlassian.com/blog/bitbucket/sunsetting-mercurial-support-in-bitbucket) |
| 13 | +and [Canonical retired Bazaar altogether](https://discourse.ubuntu.com/t/phasing-out-bazaar-code-hosting/62189). |
| 14 | +Meanwhile, GitHub and GitLab are alive and well — safe to say, Git is here to stay. |
| 15 | +In fact, we might see [Git 3.0](https://git-scm.com/docs/BreakingChanges#_git_3_0) soon — a new version for the new decade. |
| 16 | + |
| 17 | +But what about new features? Surely there’s something useful for day-to-day workflows, right? |
| 18 | + |
| 19 | +# Switch Branches |
| 20 | + |
| 21 | +`git checkout` does the trick — but it’s much more than a branch switching tool. |
| 22 | +Take a look at [the `man` page](https://git-scm.com/docs/git-checkout): |
| 23 | + |
| 24 | +> Updates files in the working tree to match the version in the index or the specified tree. |
| 25 | +> If no pathspec was given, `git checkout` will also update `HEAD` to set the specified branch as the current branch. |
| 26 | +
|
| 27 | +Kinda a mouthful, right? |
| 28 | +`git switch` is an alternative with a more focused scope. |
| 29 | +Take a look at [the `man` page](https://git-scm.com/docs/git-switch): |
| 30 | + |
| 31 | +> Switch to a specified branch. The working tree and the index are updated to match the branch. |
| 32 | +> All new commits will be added to the tip of this branch. |
| 33 | +
|
| 34 | +Simple and straightforward! To switch a branch (FYI — it auto-tracks remote branches): |
| 35 | + |
| 36 | +```console |
| 37 | +$ git switch BRANCH |
| 38 | +``` |
| 39 | + |
| 40 | +To create and switch a branch in one go: |
| 41 | + |
| 42 | +```console |
| 43 | +$ git switch --create BRANCH |
| 44 | +``` |
| 45 | + |
| 46 | +> :information_source: `git switch` is experimental from [v2.23](https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.23.0.adoc) (August 2019), |
| 47 | +> stable from [v2.51](https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.51.0.adoc) (August 2025). |
| 48 | +
|
| 49 | +# Push Branches |
| 50 | + |
| 51 | +Attempting to push a local branch results in a helpful message: |
| 52 | + |
| 53 | +```console |
| 54 | +$ git push BRANCH |
| 55 | + |
| 56 | +fatal: The current branch BRANCH has no upstream branch. |
| 57 | +To push the current branch and set the remote as upstream, use |
| 58 | + |
| 59 | + git push --set-upstream origin BRANCH |
| 60 | + |
| 61 | +To have this happen automatically for branches without a tracking |
| 62 | +upstream, see 'push.autoSetupRemote' in 'git help config'. |
| 63 | +``` |
| 64 | + |
| 65 | +Git advertising [a new feature](https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushautoSetupRemote)? Amazing! |
| 66 | +Unfortunately, with all the noise these days, it's easy to miss. |
| 67 | +It does work though — changing the config instructs Git to set upstream on its own: |
| 68 | + |
| 69 | +```console |
| 70 | +$ git config --global push.autoSetupRemote true |
| 71 | +``` |
| 72 | + |
| 73 | +> :information_source: `push.autoSetupRemote` is available from [v2.37](https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.37.0.adoc) (June 2022). |
| 74 | +
|
| 75 | +# Compare Changes |
| 76 | + |
| 77 | +`git diff` is good, but it can be better — especilly when it comes to the _move_ kind of changes. |
| 78 | + |
| 79 | +There is [a diff option](https://git-scm.com/docs/git-config#Documentation/git-config.txt-diffcolorMoved) for this, disabled by default: |
| 80 | + |
| 81 | +```console |
| 82 | +$ git config --global diff.colorMoved true |
| 83 | +``` |
| 84 | + |
| 85 | +When configured, the diff output uses different colors to highlight moved content — |
| 86 | +magenta (`tput` color #5) instead of red (#1) and cyan (#6) instead of green (#2). |
| 87 | +Additions and deletions keep their usual red and green colors. |
| 88 | + |
| 89 | +The option accepts [multiple modes](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---color-movedmode). |
| 90 | +`dimmed-zebra` might be a good one — it dims _moves_, which might be not super important most of the time. |
| 91 | + |
| 92 | +> :information_source: `diff.colorMoved` is available from [v2.15](https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.15.0.adoc) (October 2017). |
| 93 | +
|
| 94 | +# Restore Changes |
| 95 | + |
| 96 | +Restoring changes can be done with `git checkout` (as is switching branches and much more). |
| 97 | +Following the same idea behind `git switch`, `git restore` is a simpler alternative. |
| 98 | +Take a look at [the `man` page](https://git-scm.com/docs/git-restore): |
| 99 | + |
| 100 | +> Restore specified paths in the working tree with some contents from a restore source. |
| 101 | +> If a path is tracked but does not exist in the restore source, it will be removed to match the source. |
| 102 | +
|
| 103 | +To restore a file to its committed state: |
| 104 | + |
| 105 | +```console |
| 106 | +$ git restore PATH |
| 107 | +``` |
| 108 | + |
| 109 | +Also take a look at `--staged` and `--worktree` arguments when dealing with more complex scenarios. |
| 110 | + |
| 111 | +> :information_source: `git restore` is experimental from [v2.23](https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.23.0.adoc) (August 2019), |
| 112 | +> stable from [v2.51](https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.51.0.adoc) (August 2025). |
| 113 | +
|
| 114 | +# What’s Next? |
| 115 | + |
| 116 | +[The _How Core Git Developers Configure Git_ article](https://blog.gitbutler.com/how-git-core-devs-configure-git) |
| 117 | +from the GitButler folks is a great read — it explores which options might be worth to have enabled by default. |
| 118 | + |
| 119 | +New commands like `git switch` and `git restore` are easier for Git newbies to understand. |
| 120 | +Not everyone has been using Git for 15+ years and is familiar with all `git checkout` nuances. |
| 121 | + |
| 122 | +All in all, it’s amazing to see Git continue to grow and evolve. Here’s to the next 20 years! |
0 commit comments