Besides the "basic" commands of Git, everyone has their own little Git tricks they use. I wanted to quickly write a list of my own which I tend to ...
For further actions, you may consider blocking this person and/or reporting abuse
Assuming that you have set your
git-branch
alias already, you could simplify this as:alias git-up="git push -u origin \$(git-branch)"
EDIT: escaped $ to prevent running the alias on definition, thanks @darlanalves
Git assumes the current branch implicitely, so the following also works:
Even better is to use a git alias for this:
so you can use it like
$ git up
.If you think you need to explicitly specify the branch anyway you can define an alias like this:
Nice! Thank you for the tips!
The
git up
alias is perfect.Not specifying the branch still gives me a
Fatal: The current branch has no upstream
. Any ideas why?Did you specify the
-u
flag?Yep, I copied and pasted this exactly
git config --global alias.up 'push -u origin'
yeah, looks like I have the same issue!
Ok, after some digging I got an alias to work. I have no idea why it works when nothing else I tried would but here it is anyway. I think it is best to paste the following into your .git-config file directly:
or if you have git 2.22
I figured this out mostly by messing around with this Stack Overflow answer: stackoverflow.com/a/30529511/9770212
the problem is that the
git-branch
got evaluated when I started my terminal. This is inzsh
. :/I'd immediately get "not a git repository" message.
maybe a function would work?
Oh, you're right... an alias would be immediately evaluated.
Using a function is the way to go.
it kind of tripped me up! I was expecting it to evaluate when invoked, just like a function.
Yeah, it may not be intuitive at first... but is really useful to be able to rely on both behaviors (evaluate on definition or when run).
If you wanted to use other aliases within an alias you have to start playing around with
eval
(which I don't really recommend). Like:alias testingEval="eval 'git-branch'"
But things get messy pretty quickly... I pretty much use alias for super simple things or functions when I need something a bit longer that could use some extra variables, or to use parameters, or runtime expansion.
btw, I use zsh too :) ... I use prezto and got some configs online if you want to take a look
github.com/ivanalejandro0/prezto/b...
I get why it works like that and I'm ok with it :) It really just surprised me but then again, I don't do a lot of bash scripting.
The key to it is just escape the
$
char in an alias.alias git-up="git push -u origin \$(git-branch)"
Oh, cool. I didn't know about that, thanks.
I've edited my comment escaping the $.
I like this kind of articles, we can get some ideas.
From your examples, I had the feeling that you're not sure how to include arguments or bash commands into aliases. You might be interested in one my articles:
How do you prepare your commits?
Sandor Dargo ・ Jul 3 ・ 5 min read
I do know. And the comments taught me a good deal but I don't like adding bash commands to git aliases since I use git on non Linux machines
Sorry then. How come you use
sed
andxargs
in your aliases? Or you want to maintain only one.gitconfig
and push all the rest to OS-specific setenvs?That's it!
I write completely different aliases for Powershell but I get to still use identical git aliases
Have you considered using Git BASH on windows?
yep, quickly dismissed it. It's not worth it plus Powershell is just so damned good!
Nice post!
For the "get the name of the current branch" there's a git plumbing command that can help you get the current branch name reliably without the text manipulation:
I tend to keep this one as part of my git aliases:
Here's a quick write-up if you're interested!
My approach to get the current branch is this:
alias gcb='git symbolic-ref --short HEAD'
(gcb to remind me of Git Current Branch)
I like it more because it uses only git and I don't depend on piping through another app.
I am afraid to do
git cherry-pick
without the-n
parameter because cherry-picking automatically commits after picking the changes. Most of the time I want to review the changes and commit them manually. So I do:ooh, i wasn't aware of the dry run. I tend to not care because I can always do an interactive rebase and delete those commits.
My approach for this is
git commit --amend --no-edit
I like that better. 👍
On the current branch alias, don't forget
git branch --show-current
with Git 2.22: stackoverflow.com/a/55088865/6309.I used it in stackoverflow.com/a/56713414/6309
also if you've got git > 2 this works
git status --porcelain=v2 --untracked-file=no --branch \
| grep branch.head \
| cut -d ' ' -f 3
I HAD NO IDEA. This is awesome! Thank you! :)
looks like I'm still a little behind on versions. I'm on v2.17.x
Neat tip though, I'll have to remember to come back and change my rc files to use this :)
Why use shell aliases for something that's
git
specific? You can just usegit
aliases.Instead of:
Do:
Now, from within any repo, you can run
git this-branch
, and you didn't have to add anything to your shell, and if you switch shells, it will continue to work.That's pretty cool! I basically just learned about that from people on this post
Git aliases
System aliases
Thank you for the nice writeup Antonin! I'll definitively take over a few :)
Might not be totally related, but in order to not be able to commit things that are not to be committed, we're using (husky)[github.com/typicode/husky] and a pretty strict linting :)
We actually use husky too!!
great! also take a look to this: ohshitgit.com/
Great tips, Antonin!
I've been using similar tricks in my daily flow... And they definitely help productivity!
I've added them to a repo so I can share between my machines and with other devs :)
github.com/darlanalves/faster-git
You can simply do
git push -u origin HEAD
and not have to mess around with evaluating the branch name.Your trick git reflog saved my today working. Thanks :)
That's awesome! I'm happy to hear that!
Thanks for sharing!! I have a question about
git log -1
. Why don't you usegit show
instead? Does it have any benefits?I just do this to get the current branch:
git branch | grep '*'
Great thanks!
I had no idea about cherry-picking a list of commits! This is a game-changer!
You should also have *.log in your .gitignore
And *.sw[a-p] if you have vim friends.