`git branch –list` returns files?

This is strange. I’m trying to make a script that will checkout every local branch and rebase origin/master onto it.

So this is my script:

for br in `git branch -l`; do
    git checkout $br
    git rebase origin/master

Simple. But before I made the script, I wanted to make sure that `git branch -l` returns what I think it is returning…..it isn’t.

git branch -l returns the correct result. But `git branch -l` is actually returning all local branch PLUS the files in the current dir!

It returns it like this:

[list of local branches minus master] [list of files in the current dir] master

`git branch –list` behaves the same way.

Why is this happening?

And is there a better way to rebase origin/master to all local branches?

  • Bitbucket Webhooks
  • update Jenkins credentials by script
  • How To Deploy Your PHP Applications Correctly?
  • bash function ignoring dot for git clone command
  • Is it possible to create branch that is tracking non-existing remote
  • Scripting git commands in Python without external library
  • Accessing SSH key from bash script running via a cron job
  • git post-commit hook - script on committed files
  • 2 Solutions collect form web for “`git branch –list` returns files?”

    This is kind of a silly thing.

    git branch –list is putting an asterisk in front of the currently selected branch; your shell script is interpreting that as the wildcard and adding the files in the current directory to the list of items to traverse with your for loop.

    You can try

    for br in `git branch -l | tr '*' ' '`; do
        git checkout $br
        git rebase origin/master

    The other thing you can do, not really needed here but useful for more robust scripts as it were, is to use git for-each-ref to extract the refnames you want. For instance, the equivalent of git branch --list minus the prefix white-space-or-asterisk is:

    $ git for-each-ref refs/heads/ --format='%(refname:short)'

    as in this example:

    $ git branch -a
    * master
      remotes/origin/HEAD -> origin/master
    $ git for-each-ref refs/heads/ --format='%(refname:short)'

    Likewise you can get remotes (git branch -r) by using refs/remotes/ instead of refs/heads/, and you can find tags using refs/tags/. There are even extra options with for-each-ref designed to protect strings from various program actions (not needed for this particular case, see the documentation for git check-ref-format and its restrictions on branch names), but which make it possible to use eval on the output of git for-each-ref.

    Git Baby is a git and github fan, let's start git clone.