Getting Around First Steps With Git

So, your pain threshold limits are exhausted by everyday use of your current version control software (SVN, CVS anyone?). You are looking for another solution to version control problems and you start exploring Git. You heard that it is really cool, but now you’re getting hands on to figure out how cool it really is. For start, you should know that Git is different. Your current mindset will drive you into using it the same way you used SVN, but very soon you’ll find out that if you don’t start working on your mindset, using Git can become more a problem than a solution. Fortunately, Pro Git book is available online for free and I highly recommend going through it with understanding. If there is a list of things to understand and clarify at start that I would recommend: I’m new to Git and I had some basic questions to answer for myself, and afterwards I saw others asking again the same things. If I would take a couple of them to answer that would be these.

Git autocompletion

Great tool to explore and learn Git commands during your everyday work. It is easy to set up and invaluable to have it. First download the git-completion.bash file
cd # to go to your home folder
curl \
https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -O
and then add this line to your ~/.bash_profile or however you name your profile file
source ~/git-completion.bash

Checking out remote branch

Someone pushed new branch to remote repository and you are having tough time to start working on it. Use
git fetch
this will fetch all remote branches and put them in local repository
git checkout -b local-branch-name origin/remote-branch-name
will check out branch and set tracking between local and remote branch properly for you.

Pushing and pulling only to and from current branch

After you have finished your work on a branch, you want to push your changes. But git push command by default pushes changes from all branches if no other parameters are given. It can be configured in general to push only current branch with
git config push.default current
Regarding puling is a bit more complicated, since this is the option to be set for each branch. Probably it can be generalized. For example, you can have  a remote branch called funky-branch which is on the origin remote repository and local branch with same name funky-branch. Then we can pull only its changes if we set following config options for this local branch
git config branch.funky-branch.remote origin
git config branch.funky-branch.merge refs/heads/funky-branch
now git pull works without pulling other branches along the way. There are of course numerous more questions to answer regarding Git, but when you touch the ground with your own feet, you’ll get to answer them really quickly since it has a great documentation and an active community. Git is extremely powerful and incredibly fast tool which enables us to make some really complicated development scenarios easy to manage. On the other hand it can often look like a kind of magic since there is a bit more to understand of it comparing to centralized version control solutions. One thing that is obvious after some weeks of everyday usage of Git is that it fits really good to the way my mind works during development. If you still have doubts, give it a try, it might even help :)

Making current git branch visible in your command prompt

Git is known for powerful branching possibilities. Although, it is hard to shuffle between branches and to always know in which you are currently. There is simple solution to this. You can add name of current branch to your command prompt by adding this to your shell profile script.
# Set up git branch information in command prompt
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/'
# Example of use of the function in command prompt $(parse_git_branch)
PS1="\w\$(parse_git_branch) $ "
After resourcing your shell profile you’ll see current checkout branch name in command prompt whenever you enter git workspace folder.

Selecting only part of a change for commit

Usually changes we make are not perfectly clean, or currently changed files contain at least two unrelated changes. Git provides the way to select only a part of all the changes and stage them for commit. It is done by using:
git app -p
Then you would have a change to decide on every group of lines weather to include it or not, you can even split it to parts so you can select more granular way …

Cleaning up repository with garbage collection

Git doesn’t do garbage collection on the repositories by default which has couple of advantages. Commands don’t lose time to prune objects all the time and you have kind of history to refer to locally. But, over time it gets slower, which can be fixed with activating garbage collection.
git gc --prune
// or check `man git-gc` for more information about git garbage collection

Getting up to more speed

Here you can find extensive list of resources for learning Git with selection of top 10 sites that provide the best resources online. After you have finished the basics you can proceed with more advanced topics. This great post from Markus Prinz can help you with optimizing Git for more advanced and more personalized workflows.