Reginald Lynch

What is Git Object

Introduction In this post, we will take a look at Git's data model The data model of Git is different from other common version control systems (VCSs) in the way Git handles its data. Traditionally, a VCS will store its data as an initial file, followed by a list of patches for each new version of the file: Git is different: Instead of the regular file and patches list, Git records a snapshot of all the files tracked by Git and their paths relative to the repository root—that is, the file

The Three Stages Of Git

In this post, we'll see how to create a blob, tree, and commit object in the repository. We'll also learn about the three stages of creating a commit. Step 1 : First, we'll make a small change to the file and check git status: echo "Another line" >> text-file.txt git status Step 2 : Let's add the text-file.txt file and run git status again: git add text-file.txt git status Step 3 : We can edit the file again and run git status: echo "Another line 2" >> text-file.txt git status

Git is a directed acyclic graph (DAG).

The history in Git is formed from the commit objects; as development advances, branches are created and merged, and the history will create a directed acyclic graph, the DAG, because of the way that Git ties a commit to its parent commit. The DAG makes it easy to see the development of a project based on the commits. A graph of the example repository with abbreviated commit IDs You can view the history (the DAG) in Git by using its git log command. There are also a number of visual Git tools th

How to extract fixed issues in Git

A common use case of creating a release is to create a release note, containing, among other things, the bugs fixed in the release. A good practice is to write in the commit message whether a bug is fixed by the commit. A better practice is to have a standard way of doing this—for example, a line with the string "Fixes-bug: ", followed by the bug identifier in the last part of the commit message. This makes it easy to compile a list of bugs fixed for a release note. The JGit project is a good

How to Get a list of the changed files in Git

The same repository and HEAD position (HEAD pointing to 93da791) that we saw in the previous post will be used. The release is also the same, which is v5.8.1.202007141445-r. Step 1 : The following command lists all the files that have changed since the last release (v5.8.1.202007141445-r) git diff --name-only v5.8.1.202007141445-r..HEAD By specifying --name-only, Git will only give the paths of the files that were changed by the commits in the range specified as output. Step 2 : The output of t

How to View the history with gitk (Git)

We saw earlier how we can view the history (the DAG) and visualize it by using git log. However, as the history grows, the terminal representation of the history can be a bit cumbersome to navigate. Fortunately, there are a lot of graphical tools in Git, one of them being gitk, which works on multiple platforms (Linux, Mac, and Windows). Step 1 : Make sure you have gitk installed: which gitk If nothing shows up, then gitk is not installed on your system, or at least is not available on your $PA

How to Find commits in the history (Git)

we will use the JGit repository, trying to find commits related to the "Performance" keyword. In this recipe, we will look through the entire history, so we don't need the master branch to point to a specific commit. Step 1 : we can use the --grep option to find specific strings in commit messages. In this recipe, we look at the entire history and search every commit that has "Performance" in its commit message: git log --grep "Performance" --oneline --all Step 2 : Note that the search is case

How to search through the history code (Git)

Sometimes, You may want to know which commits touched a specific method or variable. This is also possible using git log. You can perform a search for a string, for example, or a variable or method, and git log will give you the commits, adding or deleting the string from the history. In this way, you can easily get the full commit context for the piece of code. Again, we will use the JGit repository with the master branch pointing to d35f0ff: git checkout master && git reset --hard d35f

How to Configure targets (Git)

In this post, we will look at the different layers that can be configured. The layers are as follows: SYSTEM: This layer is system-wide and can be found in /etc/gitconfig GLOBAL: This layer is global for the user and can be found in ~/.gitconfig LOCAL: This layer is local to the current repository and can be found in .git/config Step 1 : We will use the jgit repository for this example; git clone cd jgit Step 2 : we can use the command git config --l

How to Query the existing configuration (Git)

we will look at how we can query the existing configuration and set the configuration values. Step 1 : We'll use jgit again by using the following command: git clone cd jgit Step 2 : To view all the effective configurations for the current Git repository, run the following command: git config --list Step 3 : If we are just interested in a single configuration item, we can just query it by its section.key or section.subsection.key: git config git c

How to Create a template commit message (Git)

we will see how to create a template commit message that will be displayed in the editor when creating a commit. The template is only for the local user and not distributed with the repository in general. Step 1 : we will use the example repository from this post git clone cd Git-s-objects Step 2 : We'll use the following command as a commit message template for commit messages: Short description of commit Longer explanation of the motivatio

How to create a git directory template

Sometimes, having a global configuration isn't enough. You will also need to trigger the execution of scripts (also known as Git hooks), exclude files, and so on. It is possible to achieve this with the template option set to git init. It can be given as a command-line option to git clone and git init, or as the $GIT_TEMPLATE_DIR environment variable, or as the configuration option init.templatedir. It defaults to /usr/share/git-core/templates. The template option works by copying files in the t

How to Configure Rebase and merge (Git)

By default, when performing git pull, a merge commit will be created if the history of the local branch has diverged from the remote one. However, to avoid all these merge commits, a repository can be configured so that it will default to rebase instead of merging when doing git pull. Several configuration targets related to the option are available as follows: Step 1 : pull.rebase: This configuration, when set to true, will pull to rebase the current branch on top of the fetched one when perfor

How to Configure Expiry Objects (Git)

By default, Git will perform garbage collection on unreferenced objects and clean reflog for entries that are more than 90 days old. For an object to be referenced, something must point to it; a tree, a commit, a tag, a branch, or some of the internal Git bookkeeping, such as stash or reflog. There are three settings that can be used to change this time as follows: Step 1 : gc.reflogexpire: This is the general setting to know for how long a branch's history is kept in reflog. The default time is

How to Configure Autocorrect (Git)

This configuration is useful when you get tired of messages such as the following one just because you made a typo on the keyboard: git statis By setting the configuration to help.autocorrect, you can control how Git will behave when you accidentally send a typo to it. By default, the value is 0 and it means to list the possible options similar to the input (if statisis given, status will be shown). A negative value means to immediately execute the corresponding command. A positive value means