Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git alias which works for `main` or `master` or other

Tags:

git

With the variety of Default Branch choices in git, how can I write a script/alias which references the default branch no matter the name?

My specific example is my alias which rebases onto the newest remote master:

### version for `master`
#   rebaseOn = "!f() { BRANCH=$(git symbolic-ref --short HEAD) && git fetch && git pull --rebase; git rebase origin/${1:-master}; }; f"
### version for `main`
    rebaseOn = "!f() { BRANCH=$(git symbolic-ref --short HEAD) && git fetch && git pull --rebase; git rebase origin/${1:-main}; }; f"

usage: git rebaseOn or git rebaseOn newParentBranch (because its defaulting the input ${1:-main})

I would like that command to work for either version there, because various repositories I work with have main or master or default or trunk


So far I've found https://stackoverflow.com/a/50056710/356218: git remote show upstream | grep "HEAD branch" | sed 's/.*: //' but that is quite long for inserting into an alias.

Is there a better option than that?


Best answer so far

Restating the current best answer in my own words:

@matt suggesting of using git branch --set-upstream-to:

  • I suppose you set git branch --set-upstream-to <main/master> when you initially clone a repo, and be very consistent about running that command with every repo you touch
  • So then you can ignore what github or such says is the default master, and know that on this setup of yours, you always use your preference locally
    • As opposed to pair programming or anything where you must fall back to standard behavior
  • Then before rebasing onto default, you must pull your choice of name (script this) so that any new commits are included before the rebase
    • So if you're using a branch to work in parallel with working on something else on default you can't really do that ever with this method, always need a separate branch for commits which aren't ready to push yet
  • Then you can merge into default and push without extra work

Merging works mildly better probably (as is always the case with git)

Note: the real frustration is when a repo has both a master and main branch, and its difficult to remember/know/discover which is the primary via CLI (have to git log and look at dates, remember fairly obscure commands, or check github. All those take more than 2 seconds and a context change, to give an idea of the level of the frustration)

like image 751
Thymine Avatar asked Feb 16 '21 21:02

Thymine


People also ask

Should I use main or master in git?

The master branch is no different than any other branch in a cloned Git repo, except that historically it's been the default name used when the first branch is created. A developer can delete, rename and even re-create the master branch after it's deleted, just like any other Git branch.

What is the use of alias in git?

Git aliases are a powerful workflow tool that create shortcuts to frequently used Git commands. Using Git aliases will make you a faster and more efficient developer. Aliases can be used to wrap a sequence of Git commands into new faux Git command.

Where do I put git alias?

Your git aliases are often stored per your user's configuration at ~/. gitconfig . You can also manually set aliases using, for example, the command git config alias. s 'status -s' .

How do I make git default to master instead of Main?

You need to do this on GitHub, not on your local computer. If you go to the main repo page on GitHub and select the branches dropdown menu, you will see two branches listed and a checkmark next to master . We are going to change it so the default repo and the checkmark are next to the main branch.

What are some useful Git aliases to use?

8 useful Git aliases 1. Git status Git command line users often use the status command to see changed or untracked files. By default, this... 2. Git log --oneline Create an alias to display your commits as single lines for more compact output: $ git config... 3. Git last commit This shows details ...

What is the D alias in git diff?

The git diff command displays differences between files in different commits or between a commit and the working tree. Simplify it with the d alias: The standard git diff command works fine for small changes. But for more complex ones, an external tool such as vimdiff makes it more useful.

How to Alias the remote branch name in Git?

However, if you’re working on the same branch name as the one you wish to push changes to, then we can alias the remote branch name as HEAD . Push your commits from the local git repository to the origin or upstream remotes with a shortcut as simple as git done using this alias:

What does the git config command do?

The git config command is actually a helper utility for writing to the global and local Git config files. Invoking this command will update the underlying global config file just as it had been edited in our previous example. Git aliases are a powerful workflow tool that create shortcuts to frequently used Git commands.


2 Answers

Continuing from great @torek's comment, you can set your aliases like that:

main-branch = !git symbolic-ref refs/remotes/origin/HEAD | cut -d'/' -f4
remotesh = remote set-head origin --auto
com = "!f(){ git checkout $(git main-branch) $@;}; f"
upm = "!f(){ git pull --rebase --autostash origin $(git main-branch) $@;}; f"
rebasem = "!f(){ git rebase -i --autosquash origin/$(git main-branch) --no-verify $@;}; f"

git main-branch will return the name of the current "main" branch (main/master).

In other aliases use $(git main-branch) instead of hardcoded master branch name. Just remember that it needs to be a shell script (which starts with !) so that the $(git main-branch) gets actually interpolated.

If any of the aliases errors with fatal: ref refs/remotes/origin/HEAD is not a symbolic ref, then simply run git remotesh .

like image 130
jtompl Avatar answered Oct 09 '22 09:10

jtompl


You have numerous options here. I would, however, recommend that you use none of the ones I list here, because your branch names are yours. There's no reason you need to use the same names as some other repository. That's what matt suggests in a comment as well.

Still, your options include:

  • Read origin/HEAD in your own repository.

    repo-A$ git symbolic-ref refs/remotes/origin/HEAD
    refs/remotes/origin/master
    
    repo-B$ git symbolic-ref refs/remotes/origin/HEAD
    refs/remotes/origin/main
    

    where repo-A is a clone of an older repository that uses master and repo-B is a clone of a newer repository that uses main, for instance.

    Note that you may occasionally need to run git remote set-head --auto to get your Git to notice that their Git has renamed master to main and hence to update your origin/HEAD to match. Your origin/HEAD is established when you run git clone and not updated after that, unless you use git remote set-head --auto. You could of course put this git remote set-head operation into your alias.

  • Use git remote show, just as you showed. It's in an alias: why do you care how long the alias is?

  • Use the upstream setting (git branch --set-upstream-to) and then just run git rebase, so as to pick the upstream of the current branch. Of course that will mostly be origin/name for branch name; presumably, you're using this alias when you want to update your name before a forced push to update the branch named name over on origin. If so, this option is a bad one.

  • Write a script. Put the script in your $PATH somewhere, naming it git-rebase-on-default-branch for instance. Running git rebase-on-default-branch will run your script. Now that it's not an alias, but rather a script—in whatever language you like—you can put as much code as you like in it, making it as clever and fancy as you want. (This, I see, is also Ôrel's suggestion.)

There are no doubt more, but this should cover at least some of the bases.

like image 43
torek Avatar answered Oct 09 '22 10:10

torek