This is post is not yet another one about Git aliases. Some like them, some don't. I use only a few. For the commands, I use the most and the ones I wouldn't forget anyway. In other words, I'm lazy to write status
and commit
all the time. These aliases are not worth a post. On the other hand, I modify my prompt in the terminal to give me a lot of information about the current state of the repository I'm at and I also use auto-completion for git commands.
When I'm not in a git repository, my prompt is not altered. This is the default state of my prompt:
Let's create a new git repository by issuing the git init
command right here on spot and see what's going to happen. As you can see on the below image we are on the master branch, plus we see a hashtag. That hashtag shows that we are still in the init stage, the repository initialization has not finished yet.
Let's create a new file (i.e. somefile.txt) and commit it. Now our repository is in a clean state. We will see only the name of our current branch.
So if I'm in a git repository and it has a clean state, I'll simply see the name of the current branch:
Now, I'm going to modify somefile.txt and let's see how the prompt changes. A star appears after the branch name. That *
means that I have an unstaged tracked file.
Let's add another file (newfile.txt) and stage it immediately. Our prompt changed again, we have both a *
and +
. As you might have guessed that +
is to show that we have a staged file.
Now let's stage somefile.txt as well, so we have only staged files. As we expect, the *
disappears we are left only with the +
.
In order to finish our cycle and to have a clean repo again, we have to commit. Our working area is clean again. There are no unstaged or staged files.
Key Takeaways
To wrap it up, my prompt will always show the checked out branch name or commit hash. Besides it will display a *
if we have any unstaged change on a tracked file and +
if we have any staged files.
It also gives you some extra information during rebases and when you resolve merge conflicts.
This can save you issuing a lot a of git branch
or git rev-parse --abbrev-ref HEAD
to see in which branch you are and in addition you won't need some of the git status
commands as well as you'll know just by looking at your prompt if you are in a clean state or if you still have to stage some files while preparing your next commit.
Hot to get it
I don't want to take the credit for this. The credit goes to Carolyn and Sarah who have this super course about Git on Udacity. We started to use git in my company a couple years ago and after some time I realized that if I use only the push/pull/commit/diff/status quintuple I will keep having troubles and I'll like neither git, neither my work.
I enrolled to this course and understood better many concepts. They also provided a nice style-guide and these scripts. I'll be always grateful to them.
Linux and Mac users will need to save this file auto-completion and this file for checking the state of your repo, and this helps you to set up your prompt using the previous two files.
If you are on Windows or if you want more details, check out the course, it's for free!
This article has been originally published on my blog.
Top comments (6)
Is there a reason behind not using zsh + oh-my-zsh and the git plugin?
I can think of reasons for not using zsh :), but yes, there are several pre-packaged versions of this prompt floating around. I use powerline-shell for instance. This post is a good intro, though.
Another notable pre-packaged versions of this prompt is
pshazz
for PowerShell on Windows :DOf course, there are! In one of my environments, I cannot use zsh. But I'm sure there are great tools out there! Would you like to introduce oh-my-zsh a bit more in detail?
I second the oh-my-zsh + power line suggestion. You can even get it working in Windows, if so inclined.
dwijaybane.wordpress.com/2017/12/0...
Babun is an excellent cygwin setup that comes with all of the above.
This is my prompt:
thepracticaldev.s3.amazonaws.com/i...
I'm using a modified version of the agnoster theme.
It updates in real-time too. :)