I've got a git alias that looks like this:
[alias]
unpushed = log origin..HEAD --pretty=format:'%h %an %s'
Which works great for showing "unpushed" changes when I'm on master. But, this alias doesn't really work right when I'm on a branch.
What would the correct command be to show unpushed changes whether or not I'm on a branch?
We can view the unpushed git commits using the git command. It will display all the commits that are made locally but not pushed to the remote git repository.
On GitHub.com, you can access your project history by selecting the commit button from the code tab on your project. Locally, you can use git log . The git log command enables you to display a list of all of the commits on your current branch. By default, the git log command presents a lot of information all at once.
If you just want to see the outgoing commits for the current branch, you can use the following:
git config alias.unpushed "log @{u}.. --pretty=format:'%h %an %s'"
This causes git log
to show all commits reachable from HEAD
excluding those reachable from the upstream branch. The @{u}..
argument is equivalent to @{u}..HEAD
, and @{u}
is shorthand for the current branch's upstream commit (e.g., origin/foo
if the checked out branch is foo
).
If you want to see all unpushed commits from all branches, do this:
git config alias.unpushed "log --all --not --remotes --tags --pretty=format:'%h %an %s'"
The above causes git log
to walk all references, but stop at (exclude) remote references (e.g., origin/master
) and tags. Git doesn't distinguish between local and remote tags, so the above assumes that all tags are remote (this isn't always true, so you may want to leave out the --tags
argument sometimes).
I personally use the following aliases to show unpushed commits:
# unpushed: graph of everything excluding pushed/tag commits
# with boundary commits (see below for 'git g' alias)
git config alias.unpushed '!git g --not --remotes --tags'
# go: _G_raph of _O_utgoing commits with boundary commits
# (see below for 'git gb' alias)
git config alias.go '!git gb @{u}..'
# g: _G_raph of everything with boundary commits
git config alias.g '!git gb --all'
# gb: _G_raph of current _B_ranch (or arguments) with boundary commits
git config alias.gb '!git gbnb --boundary'
# gbnb: _G_raph of current _B_ranch (or arguments) with _N_o _B_oundary commits
git config alias.gbnb 'log --graph --date-order --pretty=tformat:"%C(yellow)%h%Creset %C(magenta)%aE %ai%Creset %C(green bold)%d%Creset%n %s"'
For simple repositories I use the git g
alias as my primary method of exploring the commits. For complex repositories (dozens of branches), I generally use git gb
to show specific branches or ranges of commits. When I want to see how git push
will change the remote reference (my push.default
is set to upstream
), I use git go
. When I want to see if there is anything anywhere in my local repository I haven't pushed (e.g., to see if I'll lose work if I delete the clone), I use git unpushed
.
I use git-wtf for this and other questions.
This will do it:
git config alias.unpushed "log $(git rev-parse --symbolic-full-name @{u})..HEAD --pretty=format:'%h %an %s'"
It compares the full name of the upstream tracking branch (e.g. refs/remotes/origin/master
) with HEAD. The full name is a valid refspec for your average git operation.
If you use fetch
instead of pull
, and want commits in either branch but not both, use ...
syntax instead of ..
syntax in the command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With