Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Git Essentials

You're reading from  Git Essentials

Product type Book
Published in Apr 2015
Publisher
ISBN-13 9781785287909
Pages 168 pages
Edition 1st Edition
Languages
Author (1):
Ferdinando Santacroce Ferdinando Santacroce
Profile icon Ferdinando Santacroce

Chapter 4. Git Fundamentals – Niche Concepts, Configurations, and Commands

This chapter is a collection of short but useful tricks to make our Git experience more comfortable. In the first three chapters, we learnt all the concepts we need to take the first steps into versioning systems using the Git tool; now it's time to go a little bit more in depth to discover some other powerful weapons in the Git arsenal and see how to use them (without shooting yourself in your foot, preferably).

Dissecting the Git configuration


In the first part of this chapter, we will learn how to enhance our Git configuration to better fit our needs and speed up the daily work; now it's time to become familiar with the configuration internals.

Configuration architecture

The configuration options are stored in plain text files. The git config command is just a convenient tool to edit these files without the hassle of remembering where they are stored and opening them in a text editor.

Configuration levels

In Git we have three configuration levels which are:

  • System

  • User

  • Repository

There are different configuration files for every different configuration level.

You can basically set every parameter at every level according to your needs. If you set the same parameters at different levels, the lowest-level parameter hides the top level parameters; so, for example, if you set user.name at global level, it will hide the one eventually set up at system level; if you set it at repository level, it will hide the...

Git aliases


In Chapter 2, Git Fundamentals – Working Locally we already mentioned Git aliases and their purpose; in this paragraph, I will suggest only a few more to help you make things easier.

Shortcuts to common commands

One thing you can find useful is to shorten common commands like git checkout and so on; therefore, these are some useful aliases:

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

Another common practice is to shorten a command by adding one or more options that you use all the time; for example set a git cm <commit message> command shortcut to alias git commit –m <commit message>:

$ git config --global alias.cm "commit -m"

Creating commands

Another common way to customize the Git experience is to create commands you think should exist, as we did in Chapter 2, Git Fundamentals – Working Locally with the git tree command.

git unstage

The classic example is the...

Git references


We said that a Git repository can be imagined as an acyclic graph, where every node, the commit, has a parent and a unique SHA-1 identifier. But during the previous chapters, we even used some references such as the HEAD, branches, tags, and so on.

Git manages these references as files in the .git/refs repository folder:

If you open one of those files, you will find it inside the SHA-1 of the commit they are tied to. As you can see, there are subfolders for tags and branches (called heads).

Symbolic references

The HEAD file instead is located in the .git folder, as shown in the following screenshot:

HEAD is a symbolic reference; symbolic references are references that point to other references, using the ref: <reference> syntax. In this case, the HEAD is currently pointing to the master branch; if you check out another branch, you will see the file's content change, as shown in the following screenshot:

Ancestry references

In Git you often need to reference the past (for example...

World-wide techniques


In this section, you will raise your skills by learning some techniques that will come in handy in different situations.

Changing the last commit message

This trick is for people who don't double-check what they're writing. If you pressed the Enter key too early, there's a way to modify the last commit message, using the git commit command with the --amend option:

$ git commit --amend -m "New commit message"

Please note that with the --amend option, you are actually redoing the commit, which will have a new hash; if you already pushed the previous commit, changing the last commit is not recommended; rather, it is deplorable and you will get in trouble.

Tracing changes in a file

Working on source code in a team, it is not uncommon to need to look at the last modifications made to a particular file to better understand how it evolved over time. To achieve this result, we can use the git blame <filename> command.

Let's try this inside the Spoon-Knife repository to see...

Tricks


In this section, I would suggest just a bunch of tips and tricks that I found useful in the past.

Bare repositories

Bare repositories are repositories that do not contain working copy files but contain only the .git folder. A bare repository is essentially for sharing; if you use Git in a centralized way, pushing and pulling to a common remote (a local server, a GitHub repository, or so on), you will agree that the remote has no interest in checking out files you work on; the scope of that remote is only to be a central point of contact for the team, so having working copy files in it is a waste of space, and no one will edit them directly on the remote.

If you want to set up a bare repository, you have to use only the --bare option:

$ git init --bare NewRepository.git

As you may have noticed, I called it NewRepository.git, using a .git extension; this is not mandatory but is a common way to identify bare repositories. If you pay attention, you will note that even in GitHub every repository...

Summary


In this chapter, you enhanced your knowledge about Git and its wide set of commands. You finally understood how configuration levels work and how to set your preferences using Git, by adding useful command aliases to the shell. Then we looked at how Git deals with references, providing a way to refer to a previous commit using its degree of relationship.

Furthermore, you added some key techniques to your skill set, as it is important to learn something you will use as soon as you start to use Git extensively. You also learned some simple tricks to help you use Git more efficiently.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Git Essentials
Published in: Apr 2015 Publisher: ISBN-13: 9781785287909
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}