Recently we published a couple of tutorials to get you familiar with Git basics and using Git in a team environment. The commands that we discussed were about enough to help a developer survive in the Git world. In this post, we will try to explore how to manage your time effectively and make full use of the features that Git provides. Note: Some commands in this article include part of the command in square brackets (e.g. 1. Git Auto CompletionIf you run Git commands through the command line, it’s a tiresome task to type in the commands manually every single time. To help with this, you can enable auto completion of Git commands within a few minutes. To get the script, run the following in a Unix system:
Next, add the following lines to your
Although I have mentioned this earlier, I can not stress it enough: If you want to use the features of Git fully, you should definitely shift to the command line interface! 2. Ignoring Files in GitAre you tired of compiled files (like
3. Who Messed With My Code?It’s the natural instinct of human beings to blame others when
something goes wrong. If your production server is broke, it’s very easy
to find out the culprit — just do a
And in the screenshot below, you can see how this command would look on a bigger repository: 4. Review History of the RepositoryWe had a look at the use of
Here’s what a combination of the options looks like: 5. Never Lose Track of a CommitLet’s say you committed something you didn’t want to and ended up
doing a hard reset to come back to your previous state. Later, you
realize you lost some other information in the process and want to get
it back, or at least view it. This is where A simple If I run However, a 6. Staging Parts of a Changed File for a CommitIt is generally a good practice to make feature-based commits, that is, each commit must represent a feature or a bug fix. Consider what would happen if you fixed two bugs, or added multiple features without committing the changes. In such a situation situation, you could put the changes in a single commit. But there is a better way: Stage the files individually and commit them separately. Let’s say you’ve made multiple changes to a single file and want them
to appear in separate commits. In that case, we add files by prefixing
Let’s try to demonstrate the same. I have added three new lines to And let’s see what happes when we prefix a It seems that Git assumed that all the changes were a part of the same idea, thereby grouping it into a single hunk. You have the following options:
In our case, we definitely want to split it into smaller parts to selectively add some and ignore the rest. As you can see, we have added the first and third lines and ignored the second. You can then view the status of the repository and make a commit. 7. Squash Multiple CommitsWhen you submit your code for review and create a pull request (which
happens often in open source projects), you might be asked to make a
change to your code before it’s accepted. You make the change, only to
be asked to change it yet again in the next review. Before you know it,
you have a few extra commits. Ideally, you could squash them into one
using the
If you want to squash the last two commits, the command that you run is the following.
On running this command, you are taken to an interactive interface
listing the commits and asking you which ones to squash. Ideally, you You are then asked to provide a commit message to the new commit. This process essentially re-writes your commit history. 8. Stash Uncommitted ChangesLet’s say you are working on a certain bug or a feature, and you are
suddenly asked to demonstrate your work. Your current work is not
complete enough to be committed, and you can’t give a demonstration at
this stage (without reverting the changes). In such a situation,
To check the list of stashes, you can run the following:
If you want to un-stash and recover the uncommitted changes, you apply the stash:
In the last screenshot, you can see that each stash has an
indentifier, a unique number (although we have only one stash in this
case). In case you want to apply only selective stashes, you add the
specific identifier to the
9. Check for Lost CommitsAlthough
Here you can see a lost commit. You can check the changes in the commit by running
10. Cherry PickI have saved the most elegant Git command for the last. The In the simplest of terms, Let’s consider a scenario where we can apply this. I have two branches and I want to I switch to the branch into which I want to
Although we had a clean ConclusionWith this, we come to the end of our list of tips that I think can help you take your Git skills to a new level. Git is the best out there and it can accomplish anything you can imagine. Therefore, always try to challenge yourself with Git. Chances are, you will end up learning something new! |
|