Git branch command behaves like 'less'

北城余情 提交于 2019-11-26 21:17:58
Zach Schneider

As mentioned above, this was a default behavior change introduced in Git 2.16.

You can turn paged output for git branch back off by default with the pager.branch config setting:

git config --global pager.branch false

As other answers pointed out, git defaults to piping itself into a pager (less by default) for most commands.

An important point, though, is that When the LESS environment variable is unset, Git sets it to FRX, and the consequence is that the user-visible behavior is the same as if the pager was not used when the command's output is short (i.e. if you have only few branches). See man less:

-F or --quit-if-one-screen
Causes less to automatically exit if the entire file can be displayed on the first screen.

-R or --RAW-CONTROL-CHARS
[...]ANSI "color" escape sequences are output in "raw" form.

-X or --no-init
Disables sending the termcap initialization and deinitialization strings to the terminal. This is sometimes desirable if the deinitialization string does something unnecessary, like clearing the screen.

If you get the behavior you describe, you most likely have $LESS set to something else, and unsetting it would get rid of the issue while keeping the "pager" behavior for long output. Alternatively, you can activate the behavior for while keeping $LESS as-is by adding this to your .gitconfig file:

[core]
    pager = less -FRX

If you really dislike the pager thing, you can deactivate it globally or on a per-command basis (see other answers).

Not to argue semantics, but the behavior you're getting is the default. That's why you get it when you don't ask for something different. By default, branch (and numerous other git commands) use a pager when sending output to the terminal.

You can override this default by using the --no-pager option

git --no-pager branch

Or if you redirect the output to a file, git should detect that it isn't writing to a terminal and so should not use a pager anyway. (On the other hand, that suggests a scripting use case, in which case you should consider using a plumbing command like git for-each-ref in preference to git branch.)

this git behaviour was more and more annoying for me, too. I got my tag list in less when just wanting to list tags for example.

One can control this behaviour also by changing the default Git PAGER to cat instead of less. I'd rather scroll in iTerm than in editor. I like to use the editor when I want.

So:

git config --global core.pager cat

I hope it helps.

For those that want to update their ~/.gitconfig to fix this, it would look like this:

[pager]
   branch = false

Git branch command behaves like 'less'

Because git by default opens the output in pager(at least in ubuntu) The accepted answer will completely replace pager, which you may not like if your output is very long.

I would recommend replacing the pager with less so it doesn't "scroll" outputs less than the height of the terminal.

git config --global --replace-all core.pager "less -F -X"

https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables

GIT_PAGER controls the program used to display multi-page output on the command line. If this is unset, PAGER will be used as a fallback.

To solve your issue, you could unset PAGER and GIT_PAGER in your shell.

João Tiago

Do the following:

[alias]
  br = !git --no-pager branch
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!