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.
There are usually two ways to create a Git project
git clone
to the localgit init
The following is the project I created in a second way locally, view all the files in the .git
directory
# 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
git config,
it will change this file. After I add user.name
: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.
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.
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 log
is related to itLet us understand the last refs, objects, index
above by introducing the process of one commit in detail.
#git #github #programming