Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It can be scary at first but within a few hours of practice you’ll get the basics down. Here are a few tricks and tips that i personally found useful when using Git.
You know typing out git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d to clean up all your branches is fun and all but i’d rather just type something that takes less brain power.
If your using bash you can amend your .bash_profile , if your using zsh you should have a .zshrc file, both of these should be within your home directory ~ .
alias gc='git commit'
alias ga='git add -p'
alias gs='git status'
alias go='git checkout'
The above syntax will work with both bash and zsh, now you can just use gs , ga , gc -m "message" , and push your changes up
If you use the Bash shell, Git comes with a nice auto-completion script you can enable. Download it directly from the Git source code here on Github. Copy this file to your home directory, and add this to your .bash_profile file:
Now you will be able to just tab while typing branch names and it will auto complete the rest of the name, navigating like a bawss.
git clean -df & git checkout -- . This is my second most used Git command! its so cool!
Say you have committed some code that your preparing for a push. You also have some other files that you don’t want anymore, so you want to clear all the files that are not committed for a push.
You can see here i have some files ready for commit, there are also a whole bunch of files that i no longer am interested in. I created a clear alias that runs the above command.
After running clear you can see it deletes any new files, removes any other changes and even removes temporary files like the logs and any extra cruft within my tmp directory.
Go into your local directory and type Git branch and you may just have a ton of branches, i once had 3 pages full of branches! it can get messy, especially working with a large team that also generally bring in a few when you do a git fetch . So doing a spring clean every now and then can be useful.
Git nuke – my alias.
This little concoction will remove all the branches that have been merged into your master branch.
git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d
If you want to delete a branch at a time, here is how you do it locally.
git branch -D branch_name
Delete branch on Github
If you would like to delete a branch that is on Github, all you have to do is this!
git push origin :branch_name
Git stash and pop
Git stash and pop have saved my life so many times. Have you ever started big hairy story and then after about 3 hours of hacking you find out hang on i’m on the master branch!
Will then put all your changes into an un-stageable commit. if you where to run git status you will see that all the changes are gone. you can then create a new branch and while on the new branch call:
git stash pop
Which opens up the stash and all your previously made changes will now be available within the new branch you made for them. No one is the wiser.
Git show me all the fights!
Ever had a conflict? ever had many conflicts amongst a ton of other code
git diff --name-only --diff-filter=U
Will show you all the conflicted files! simple.
Ever stage code for commit and then commit that code with a messaged and then when attempting to push.. er.. ma.. gash.. I’m on master!
But its committed!? now what?
git reset --soft HEAD^ # use --soft if you want to keep your changes
git reset --hard HEAD^ # use --hard if you don't care about keeping the changes you made
–soft will undo all the commits, and have the files ready for a git stash
–hard will nuke it all and preserve the master as it was before you made any changes what so ever. You where never even here.
Git rollback pushed commit
Ever make a push you regret.. that you just look at and realise i have made a huge mistake.. its not too late.. git has a one way time machine.. one way because once you go to the past you are not going to be coming back.
git reset --hard <old-commit-id>
git push -f origin branch
Here you can just reference the commit id and you can remove all commits after that referenced commit. If i was to git reset --hard ffc0aa3 and then git push -f origin branch commit 2a9aod9 would cease to exist.
Well thats it for now, Hope that these will help you navigate some of the best software in the world! IMHO.