`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:

  • how to identify branches that need to get merged in git
  • Why am I unable to create/checkout this branch?
  • How do you handle the tension between refactoring and the need for merging?
  • Git: branch has lost its history?
  • How to create a new empty branch for a new project
  • git checkout new remote branch when cloning with depth 1 option
  • 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?

  • git2p4 submit fails because of illegal characters in filenames
  • Git - How to see if a branch has EVER been merged into another branch?
  • How do I go back to an earlier commit of a file using git?
  • Is it possible to hook a git commit to the save on Textmate?
  • What is the difference between 'origin' and 'remote' in git commands?
  • Heroku not updating css for rails app
  • 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.