This was originally posted here
Being a git lover, I got tired of typing same commands over and over again. This motivated me to build some alias for my git workflow I use every day. Most of the alias was inherited from Nicholas C. Zakas's gist. I took that a step further by creating more alias which I use every time I contribute to open source projects like Eslint.
Commands
Actual code for all the commands is here
Work on an issue
When you plan to work on an issue on an open source project, you will want to create a new branch on your project and then start work on it. For that you have to go through couple of steps. To stream line that process now on top of your project you can just run the command:
ws 34
This command will update the master branch with the updates from the remote and then create a new branch on it with name issue34
.
Remove a branch after work is done
Once you are done with your work on the branch. Then you can simply run the below command to delete the branch from local and remote git.
wd 34
Update your branch (all changes commited) with master
letβs say after working on your branch, you want to update your branch with latest content from the master. This is important in scenarios where you want to rebase your branch with latest content from master
branch.
update
Update your branch (uncommited changes) with master
letβs say after working on your branch, you want to update your branch with latest content from the master. But at this time you have not commited your changes. Using updateD
, it will stash your changes then update your branch from master and then pick the changes from stash and apply it back on the current branch.
updateD
Squash your commits
I always prefer to keep my changes history clean by squashing my commits. Once you are working on a branch and you want to squash your commits, simple run the following command to rebase. You can pass in a parameter of how many commits you want to include in your rebase from the HEAD. By default it will run rebase on last 2 commits.
rebase 4 # This will rebase on last 4 commits, default is 2
Push your changes to remote
After you have finished your work, you want to push your changes to remote. Here mostly you will be doing a regular push
but if you have done some rebases on your branch then your only option is to force push
. We use --force-with-lease
flag for force ppush just to be little more safe.
Regular push:
push # This will push your branch changes but will not push on master
Force push:
fpush # This will push your branch changes but will not push on master
Both commands will not allow you to push it to master because it considered a good practice to always branch off your changes and then merge it to master using a PR. This is completely my view.
Pull changes from origin
Running this command will pull thhe changes from upstream and merge them into thhe current branch. It uses --rebase --prune
flags.
pull
Update fork
Quick way to update your fork from the upstream repo. It will go to the master branch, get thhe upstream master, merge that upstream master into local master and then push thhe master to origin master. Its a complete cycle.
uf
It expects you to setup upstream
before running this command.
Quick commit
Quick way to commit your changes by supplying the commit message. Itt will add the currently changed files using the -a
flag.
c "<message>"
Log commits
Quick and easy way to view your commit log in oneline. Its formatted in a way where you can see the commit message, date and author along with some other info.
log
Git status
Get the current status of the branch.
s
Setup
To use all these command, follow the steps below to setup:
- Go to your user directory, for windows user it will be
C:\Users\<your user>
. - Create a
.bashrc
file, if you do not have that file. - Add/append the content from the below gist to that file.
- Open bash window. If already open then close it and then reopen bash window.
- Run
alias
command and you should see the list of all the alias defined.
Note: Feel free to change the function based on your need.
Gist for all the above commands
I hope this helps people to streamline their git workflows. Please share your suggestions.
Top comments (15)
with ZSH you have already set up alias for Git, and I installed example wiht mine:
plugins=(git bower osx colored-man-pages colorize brew npm)
here the alias set up
github.com/robbyrussell/oh-my-zsh/...
But I have Zplug to manage more plugin and on is very cool here:
zplug "plugins/git", from:oh-my-zsh
If you typing the same cmd, with zsh I just use the autocompletion?
I guess it make sense to use ZSH but I am a windows user and I don't want to install more stuff. That's why this works for me.
Again, important part is the functions and how you call them (using bash, zsh, etc) is very personal to the user using it. They can be used anywhere. Even the alias names can be changed based on the user preference.
the important it's the consistency and stays DRY (don't repeat your self).
And the productivity is important too.
I can start to work slowly because Windows or other software, but my boss doesn't go to understand the purpose?
Maybe you would be even faster on windows who knows :)
I tried and I'm more productive on Mac osx, Windows with virus and co, I didn't go anywhere then Linux can be a good fit for me too, I'm working on Debian and Ubuntu sometimes.
It's really similar to mac osx.
Yeah by my opinion mac is more user-friendly for developers, but what I noticed for myself is that I really enjoy more working on it, and my mood can increase productivity not tools, you can find similar tools for windows, and something can be even better, I have friends who really don't like mac os, I'm sure that theirs productivity is much better on windows
I'm sure you're friends are very marginal.
I think someone has to say it to you, there are people with different opinions even with different tastes. You should know that it isn't called marginality
Hi,
Did you manage to add the "git helpers"?
I'm also using oh-my-zsh on my mac with git plugin enabled.
to see the alias associate to git type:
alias | grep git
but again I'm using a plugin to help me to use the alias and force to remember it.
It's very helpfull when you have like me 390 alias:
alias | wc
390 1346 12969
git helpers is not a command in Git?
I'm not using zplug, however i do use alias | grep to find the alias I'm looking for.
I have managed to create a plugin for zsh that is based on git helpers. I'm not sure how to share this plugin
nice, I can suggest only one of my favorite things to use
git merge-base master HEAD
as a default commit for rebasing, it's nearest common ancestors between current branch and master
I have never heard of that... an you explain that a little bit as to when do you use and how it works...
yeah I found it somewhere as it usual happens, I use it for 2 aliases
rebase-branch => !git rebase -i `git merge-base master HEAD`
reset-branch => !git reset `git merge-base master HEAD`
first do interactive rebase of feature branch from first commit after master, second just reset all commits on feature branch leaves you with unstaged changes of all commits (you're loosing all commit messages of cause), so I thought that in your case maybe it would be good instead of default 2 commits back interactive rebase do rebase from the beginning of feature branch
Nice, I recently came up with this tool, which simplifies opening repo in browser from terminal.
github.com/plutov/o