Git Commit: A Masterclass

0
284

[ad_1]

fatmawati achmad zaenuri/Shutterstock.com

The Git commit command stores copies of your working directory changes in your Git repository. But it can also be used to modify existing commits and also to revert commits.

A basic requirement of any version control system is to store different versions of files for you. In Git, the command that does this is commit. Here is everything you need to know.

What is a commit in Git?

Commits are the series of snapshots taken throughout a project’s lifecycle that make up its development history. Commits are what allow us to extract a version of the project as it was at different points in the past. Why is that important?

Version Control Systems (VCS) are most commonly used with software source code and development projects. But they can be used successfully with any collection of text files, such as Markdown files containing book chapters.

You may not want the VCS to handle all the files in your project directories, so you need to be able to designate the files whose version you want to control. This adds them to the version control view of the project. They will be monitored for changes.

Another way to achieve this is to use an ignore list. This tells Git which files, directories, or file types to always ignore.

Over time, as new files are added to the project, some will need to be added to the version control system. In Git, this is handled by the add domain. Actually, the add command does double duty, as we will see.

To keep a history of changes that have been made to the project, you will periodically ask Git to store a snapshot of the project’s state, using the commit domain. This is where the add the command reappears in our workflow. we use the add command to tell Git which one change files that we want to have included in the snapshot. So we use commit to tell Git to create the snapshot.

Configuring the commit command

Information about the commit is stored with it, so it is always possible to know who made the commit, when, and what it contains. Some of this metadata is captured at commit time, such as the commit message.

The metadata related to the identity of the members of the development team can be configured by each user, to avoid repeatedly providing the same information.

To set your name globally for all repositories on your computer, use this command.

git config --global user.name "Dave McKay"

Configuring the global Git username

To verify that your name has been set, use this command.

git config --global user.name

Git global username check

If you need to use a different name in a particular repository, change to the project directory and use the same command without the --global option.

git config user.name "McKay, David"
git config user.name

Set a repository-specific Git username

We now have a different default username for this repository, and our global name is still used for other repositories.

Similarly, we can configure an email address globally or for a single repository by including or omitting the --global option.

git config user.email "[email protected]"
git config --global user.email "[email protected]"
git config user.email
git config --global user.email

Configuring repository-specific and global Git default user email addresses

These settings are saved in configuration files. Git global settings are kept in “~/.gitconfig”, and repository-specific settings are kept in the repository’s “.git/config” file.

he commit references the command and uses these values ​​as it operates.

Using the commit command

The basic use of commit The command is to take the files found in the staging area, known as the index, and store them as a commit in the current branch of the repository.

A basic commitment

We have a project with a modified file. we will use the add Command to prepare the file, then commit it. we are using the -m (confirmation message) so that we can provide a brief description of the purpose of the changes. If we do not use this option, we are prompted for a confirmation message when the confirmation is carried out. It’s more convenient to add one on the command line.

git add jibber.c
git commit -m "Updated help text"

Single file staging and commit

If we use the git log command we can review the details of the commits, in chronological order, with the most recent commit at the top of the list.

git log

Checking the Git repository log

Confirmations are displayed in less.

The last commit at the top of the Git log

The confirmation has been tagged with the name and email address we provided above, and our confirmation message is also logged.

Auto Archive Files

Preparing many files can take a bit of time. A different approach is to use the -A (all) option with add.

This automatically organizes all modified files along with all currently no tracking files Untracked file preparation respects the settings in your “.gitignore” file. Git won’t stage files that you’ve told it you don’t want included. Finally, files in the index that are no longer in the working directory are remote of the index.

Clearly, the -A option can make many things happen at once. he --dry-run The option gives you a preview of the changes without actually making them.

git add -A --dry-run

Using --dry-run to preview files to be staged and not staged

In our example, it will stage two modified existing files and two new files. Let’s go ahead and use the -A option before using commit domain.

git add -A
git commit -m "Enhanced parsing"

Commit modified and newly created files, after using add -A option

We can see that in total four files are modified. Two of them are newly created files, which are listed.

Staging and commitment at the same time

he commit the command has lower case -a (Every options. This performs staging and file commit in one step.

he commit -a stages of options and modified commitments existing files, and delete index files if they have been removed from your working directory. He No automatically organize files without tracking.

As the add command, the commit command has a --dry-run option that allows you to preview your actions before running it.

git commit -a --dry-run

Using the commit -a option to preview changes without committing them

Now we are going to execute the command.

git commit -a --dry-run

Using the commit -a option to stage and commit in one step

The files are organized and committed to us.

Commit to a different branch

If you’ve made some changes to the files in your working directory and realize you didn’t check out the correct branch, you should commit the changes to the correct branch without affecting the current branch.

Git doesn’t have a command to commit to a different branch. But you can rectify this situation with a little Git prowess.

We will use the Git stash command to make a copy of the changes. We’ll then check out the correct branch and apply the changes from the stash. To apply the hidden changes we are using the pop command instead of apply domain. he pop The command applies the changes and also removes them from the stash.

We’ve made some changes to our repository new-parser branch. They should have been made in the classic-parser branch.

git stash
git checkout classic-parser
git stash pop

Save changes, check out a branch, and apply changes from the stash

Now we can make a commitand update this branch.

git commit -a -m "Added pre-parser functions"

Commit the changes that were retrieved from the stash

If we go back to new-parser branch we can see that it is up to date, which means that the changes have been removed from your working directory, and your repository and files are in sync.

git checkout new-parser
git status

Checking the status of a branch to make sure it's up to date

RELATED: How to update and maintain separate Git branches

Make changes to commits

If you need to improve your commit message, perhaps you’ve caught a typo in it, or you forgot to prepare a file that should have been included in the commit, you can use the --amend option to fix things. The caveat is that this should not be used on commits that have been pushed to a remote repository.

In our last commit message, “fraze” should have been “phrase”. if we use git log we can see this.

The Git log with a type highlighted

To correct this, we will use the --amend option like this.

git commit --amend -m "Optimized phrase identification"

Using the commit --amend option to correct a commit message

if we use git log once again, we can see that the old commit has been replaced by a new one with the corrected commit message.

The Git log showing the fixed commit message

If we want to add a file that we forgot to prepare, we can commit that file so that it appears as part of the previous commit.

we will use add to prepare the file, then make a commit with the --amend option. he --no-edit option means we don’t need to provide a new commit message. The previous commit message is preserved.

git add jibber.c
git commit --amend --no-edit

Send a file to the above commit

Remove changes from a commit

If you inadvertently prepared and committed a file that you didn’t intend, you can remove that file from the commit using the reset domain. We will reset the commit back to the staging area or index. Then we’ll delete the file and re-commit the rest of the files.

To reset the last commit in the staging area, we use the reset --soft domain. HEAD~ is short for “the commit behind the HEAD of the project commit timeline”, or in English, “the last commit”.

git reset --soft HEAD~

Send the last commit back to the staging area

To remove the file that should not have been included, we use the reset --mixed domain. This resets those changes back to the working directory, recreating the changed file as an unprepared and uncommitted file.

git reset --mixed jibber.c

Deleting a file from the Git staging area

We need to commit the other files left in the index.

git commit -m "Experimental tweaks"

Confirm the files that remain in the staging area

The other two files that were in the original commit are recommitted for us.

RELATED: How to fix, edit, or undo Git commits (change Git history)

Revert a full commit

Sometimes undoing an entire commit is the easiest thing to do. Returns the working directory and repository to the state they were in before committing.

We need to use the hash reference ID from the commit. We can find this using git log:

The Git log with a commit identifier highlighted

Copy that reference and use it in the revert domain:

git revert e5bd4560aef8164c6ca9d6d4620b9db7f66aecc8

Revert a commit identified by its commit ID

This will open your default editor so you can edit a revert message. There is a default message entered for you. You can use this or edit it to your liking.

Editing the rollback message, with the default message highlighted

When you’re happy with your rollback message, save the file and exit the editor. In nano, you do this with “Ctrl+O” and “Ctrl+X”.

The rollback action completed, undoing a full commit

Wearing git log once again, we can see that a new commit has been added that undoes the changes from the reverted commit.

The Swiss Army Knife Git

Obviously, commit is one of the most important Git commands. You can do a lot, so there’s a lot to learn. Getting familiar with its less-used features is time well spent. When you need to fix a mistake, right now, you’ll be glad you prepared ahead of time.

RELATED: How to use Git merge

The post Git Commit: A Masterclass appeared first on Journal Report.

[ad_2]