Almost all work now revolves around Git. Whether you are an open-source project, company GitHub, or GitLab, you are dominated by Git commands every day.

As a newbie, you are afraid of making mistakes, and you start to learn various commands, such as git push, git pull, etc.

When you are still struggling with how to work on different branches, how to commit, how you can execute the rollback command. Read this article may give you some fresh ideas.

Understand Git operation by understanding .git, viewing .git is like we query the log, but you need to know where to look, what to look up.

What is in the .git directory?

There are usually two ways to create a Git project

  1. On the Github interface, create a new repo, git clone to the local
  2. Create a new directory locally and execute git init

The following is the project I created in a second way locally, view all the files in the .git directory

  • HEAD, is the currently active branch
# currently I'm at master branch
cat .git/HEAD
ref: refs/heads/test1
# switch branch
git checkout -b test
cat .git/HEAD
ref: refs/heads/test
  • config file, current repo configurations,such as remote URL, user, and email, etc. Every time you run git config, it will change this file. After I add user.name:

  • description, description of the current repo on Github page.
  • hooks, a lot of files, seems messy. But this is an exciting feature.

Git provides a set of scripts that can be run automatically at every meaningful Git stage. These scripts called hooks can run before and after commit, rebase, pull operations.

The name of the scripts represents the timing of execution. If you write pre-commit as a hook, check before submitting the code.

For example, I change the commit-msg script following this.

My new .git/hooks/commit-msg looks like this:

# set this to your active development branch
develop_branch="develop"
current_branch="$(git rev-parse --abbrev-ref HEAD)"
# only check commit messages on main development branch
[ "$current_branch" != "$develop_branch" ] && exit 0
# regex to validate in commit msg
commit_regex='(wap-[0-9]+|merge)'
error_msg="Aborting commit. Your commit message is missing either a JIRA Issue ('WAP-1111') or 'Merge'"
if ! grep -iqE "$commit_regex" "$1"; then
    echo "$error_msg" >&2
    exit 1
fi

And if I try to commit without a message, an error will occur:

git commit
Aborting commit due to empty commit message.
  • .git/info/exclude

Usually, we put the Git repository that needs to be submitted but ignores the public files, such as .idea/* and so on into the .gitignore file.

In exclude, it is suitable for placing no need to submit Git repository, user’s local configuration file, or some test classes.

  • .git/index

The index contains a list that is sorted according to the file name, file mode, and file metadata to quickly detect file changes. The index also contains SHA-1 identifiers for all blob types.

The git checkout-index the command allows you to view the index file.

  • .git/refs are used to help record history, the local git log is related to it
  • .git/objects, every time we make a commit, a local snapshot is generated and then stored in the objects directory in a certain way

Let us understand the last refs, objects, index above by introducing the process of one commit in detail.

#git #github #programming

Reveal the Cover of Git
1.40 GEEK