Now I can use interactive-checkout as a command instead of git checkout $(git for-each-ref refs/heads/ -format='%(refname:short)' | fzf) and I can potentially bind it to an alias as well.ĭon't forget to make the script executable with chmod :-). Git checkout $(git for-each-ref refs/heads/ -format = '%(refname:short)' | fzf) The script interactive-checkout has the following contents. Instead I created a separate script.įor me this script is at ~/Code/bin/interactive-checkout, this works for me because I have the directory ~/Code/bin added to my path. Your branch is up to date with 'origin/master'Ĭool, that works too! So we are finished but it turned into one hell of a command which I don't plan on typing out ever. $ git checkout $(git for-each-ref refs/heads/ -format = '%(refname:short)' | fzf) If I now try to switch to my current branch I get a more appropriate message. I can use that command as a substitute for git branch. $ git for-each-ref refs/heads/ -format = '%(refname:short)' In git we can use a lesser known, for-each-ref command and format the output as follows. So we have to get the branch names in some other way to not have the asterisk showing up. Since the output of fzf contains the asterisk too. If I would select master, my current branch, in fzf it would give me the error shown above. $ git checkout $(git branch | fzf)Įrror: pathspec 'master' did not match any file(s) known to git But just to make it prone to typo's, lets fix it. Eventhough it is unlikely I would want to try to switch my current branch. $ git branchĪs you see the current branch is marked with an asterisk( *). There is a problem with this however and is has to do with the outputs of git branch. As you see above you can switch branches like this. Where the output of $(git branch | fzf) is used as argument to the checkout subcommand. In order to do so you can use the following. Replace the branchnamehere with the actual branch name you want to switch to. Anyways, to switch or change a git branch type the following command and hit enter: git checkout branchnamehere. In order for this to be somewhat useful you need to combine it with a git checkout. In Git’s terminology, switching branches refers to the git checkout command. In this case it filtered the branches that were listed using git branch on the keyword juice. The above example just outputs the branch name you select. You can pipe lines to fzf and it gives you an interactive input prompt you can use to filter. This is where fzf comes in, a fuzzy finder for the command-line. I ain't got time to remember JIRA issue numbers. This makes relying on Tab-completion harder. For this project I work on using Bitbucket we use branch names starting with a JIRA issue number. In large git repo's I find myself sometimes lost in switching branches.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |