Git Concepts
Quick Start Guides
The term commit is fundamental to how Git functions as a version control system. It is important to first understand what a commit is before learning how to perform the git commit command and how to perform other actions related to commits in Git.
In Git, a commit is a snapshot of your repo at a specific point in time.
To help further understand what a Git commit is, we need to review your Working Directory vs your Staging Directory and how files changes are reflected in your Git repository.
Think of your working directory as your “in progress” working area; here, created or modified files are not yet reflected in your Git repo. Changes made to files in your working directly only exist locally on your machine.
In order to apply changes from your working directory to your Git repository, you must first stage them in your staging directory.
From here, your changes can be saved in your repo by performing a Git commit.
Now, each Git commit will represent a snapshot of your repo at that point in time, and all of your commits will come together to form your repository’s history.
Traditionally, a Git workflow will involve the following steps:
OK, now that we’ve answered the question of what is a Git commit, let’s dive into how to Git commit, and the associated actions you can perform with Git commit commands.
While you’re going through your workflow, you can always run the git status command to check which files have been staged and exist in your staging directory.
In this example, after the git status command has been performed, you can see that the trial-activation.md file has been modified, in addition to the other two.
In order to commit these changes, you will need to stage the changes with the git add command, followed by the name of the file.
git add trial-activation.md
This will stage the trial-activation.md file. Ah, but you’re not done yet! You still need to save the changes to your repository by performing the git commit command.
To add a Git commit message to your commit, you will use the git commit command followed by the -m flag and then your message in quotes. Adding a Git commit message should look something like this:
git commit -m “Add an anchor for the trial end sectionnn.”
Now, if you can see from the example above, there is a typo in the last word. So, how do you make a Git commit message change?
To amend your commit, you’re going to use the git commit command followed by --amend and then the amended Git commit message. It should look something like this:
git commit --amend “Add an anchor for the trial end section.”
If you have numerous modified files sitting in your working directory, it can be time-consuming to add them one at a time. Speed up the process by using the git commit command followed by the -a flag. This will add all of the modified or deleted files in your working directory to the current commit. It should look something like this:
git commit -a -m “Update trial-activate page with changes from release.”
Now that you’re familiar with how to Git commit in the command line, let’s see how the experience compares when working with commits with a Git client, like GitKraken.
In GitKraken, when you modify, add, delete, or rename any files in your repository, your Work-In-Progress, or WIP, will display at the top of the graph.
When you click the WIP node in GitKraken, all affected files will be listed in the Commit Panel on the right side. Additionally, GitKraken will display a color-coded icon next to each file to denote the change type: added, modified, renamed, or deleted.
This is really helpful because it provides you immediate context to the files in your repository; no need to run a command!
To stage a file in GitKraken, simply hover over the file name in the right Commit Panel and hit Stage File.
A benefit offered in GitKraken that is absent from the command line is the ability to only stage individual lines or hunks of a file. You can accomplish this by viewing the file’s diff.
To view the file’s diff in GitKraken, simply click a file from the right Commit Panel. From here, you can elect to stage or discard individual lines or hunks of code.
You can discard all changes made to a file in GitKraken by right-clicking the file on the Commit Panel and selecting Discard changes.
Furthermore, you can discard ALL changes made to all files in your WIP by clicking the trash icon 🗑 at the top left of the Commit Panel.
One of the best parts about using a tool like GitKraken to manage your projects is the ability to quickly undo, or revert, an action made by mistake. GitKraken makes it downright magical in just one click.
If you discard a change or file by mistake, simply click the Undo button in the top toolbar to revert the discard.
When you’re ready to commit your staged changes in GitKraken, be sure to type a commit summary and description in the commit message field before clicking the button to commit. You can also use keyboard shortcut Cmd/Ctrl + Enter.
After you have committed your changes, your WIP node will be transformed into a commit . From here, you can click on your commit to review the changes from this specific point in time.
Similar to how you can revert a discard you made by mistake, GitKraken makes it quick and easy to undo a commit using the magical Undo button.
This can be a lifesaver if you realize you’ve made a mistake on your last commit.
Let’s say you’ve made an error in a Git commit messages and need to make a correction. You can amend a commit message for the most recent commit in GitKraken by selecting the commit from the central graph. From here, click on the commit message to start editing the text, then click Update Message to save your changes.
This can be a lifesaver if you realize you’ve made a mistake on your last commit.
The GitKraken Git client will give you more confidence staging, saving, and committing your file changes in Git with its simple user interface and clear organizational structure.