15

Dealing with changes to git default branches | rnelson0

 4 years ago
source link: https://rnelson0.com/2020/11/24/dealing-with-changes-to-git-default-branches/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Dealing with changes to git default branches

As you may have heard by now, the default git branch name is changing from master to main in most git software (GitHub, BitBucket, GitLab, etc.) to drive out some divisive terminology. A lot of tutorials I see include scripts and aliases that depend on the default branch name being master, and those tools may be broken now, leaving you and your users with some issues after the migration is completed server-side.

The good news is that the default branch isn’t just a “thing you know”, it can be extracted dynamically from a git repo. Doing so is a very simple piece of code:

git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'

You can integrate this into any other aliases or scripts you have. For example, in my .bashrc, I’ve replaced these branch-specific aliases:

alias updatemaster='git checkout master && git fetch origin --prune && git fetch origin --tags && git pull origin master'
alias updateprod='git checkout production && git fetch origin --prune && git fetch origin --tags && git pull origin production'

with this single non-specific alias:

alias updateorigin='git checkout $(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@') && git fetch origin --prune && git fetch origin --tags && git pull origin $(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')'

And for my VS Code setup on Windows, where I use a PowerShell terminal instead of WSL/bash, my .gitconfig contains an alias for git up:

[alias]
up = !"git checkout $(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'); git fetch --prune; git pull"

Regardless of whatever system you use, now is a good time to start replacing any hardcoded instances of master and replace it with a more dynamic reference so that changes to the default branch are transparent to you. Share this information with your coworkers and update your training and new hire documents and your whole company can make the transition smooth, too.


Recommend

  • 75
    • geeks.uniplaces.com 6 years ago
    • Cache

    Mastering git branches – uniplacesgeeks

    Branches are one of the most misunderstood concepts in git and yet they are so simple to understand. How many uncomfortable situations have you been when dealing with branches? All the rebases and…

  • 24

    Over the last few weeks there has been ongoing discussion within the Git project and teams at Bitbucket, Gitlab, GitHub and other industry providers about Git's use of 'master' as the default name for the first branch that...

  • 9
    • shannoncrabill.com 4 years ago
    • Cache

    Orphan Branches in Git

    Shannon Crabill — Front End Software EngineerSkip to content You may run into a situation where a branch of a project doesn’t need to share history wi...

  • 10

    Reworking Git Branches with git filter-branch Posted at Mar 16, 2020 Within Debezium, the project I’m working on at Red Hat, we recently encoun...

  • 7
    • www.otsukare.info 4 years ago
    • Cache

    Git Workflow with branches

    Git Workflow with branchesotsukare Thoughts after a day of workGit Workflow with branches Mer 0...

  • 12
    • pawelgrzybek.com 4 years ago
    • Cache

    Remove all git branches except master

    Remove all git branches except masterPublished: 2020.12.20 | 1 minutes readAfter working on a project for a while, it’s easy to end up with a lot of legacy branches that are not needed anymore. Removing them one by one would b...

  • 10

    We have decided to unify the naming of our default branches to main. All new repos will already use this naming. Over the next couple of months, we're going to rename the branches in our existing repos as well. We're u...

  • 11

    The Mike Taylor method™ of naming git branches 22 Jan 2021 I thought I would document how I do branching in git because it’s clearly the best a perfectly acceptable way to do...

  • 7
    • elijahmanor.com 3 years ago
    • Cache

    List Recent Git Branches

    This post is part of my Byte Series, where I document tips, tricks, and tools that I've found useful.If you are anything like me, you have a lot of branches in your local projec...

  • 5

    Dealing with diverged git branches Hello! One of the most common problems I see folks struggling with in Git is when a local branch (like main) and a remote branch (maybe also called...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK