The Wayback Machine - https://web.archive.org/web/20240927174708/https://www.geeksforgeeks.org/committing-in-git/
Open In App

Committing in Git

Last Updated : 26 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Committing changes is a fundamental aspect of using Git. The commit process involves recording snapshots of your project at various stages, allowing you to track progress, revert changes, and collaborate with others efficiently. In this article, we’ll explore the complexity of committing in Git, from basic commands to best practices.

What are Commits?

A commit in Git is basically a snapshot of your project at a particular point in time. Each commit captures the state of the files in your working directory and includes a unique identifier (a SHA-1 hash), a commit message, and metadata such as the author and timestamp.

Key Components of a Commit

  • SHA-1 Hash: A unique identifier for each commit.
  • Commit Message: A descriptive message that explains the purpose of the commit.
  • Author: The person who made the commit.
  • Timestamp: The date and time when the commit was made.
  • Parent Commit(s): The preceding commit(s) in the project history.

Steps include in Committing in Git

Suppose if somebody changed something in the file say “a.txt” we need to add those changes in the repository for that we will be using the command git add file_name, and to make a commit we will be using the command. 

git commit -m "Commit_message" file_name 

Adding and Committing a file

Shortcuts: Now if we have changed a lot of files in the directory, so to avoid using git add file_name for every file we can avoid this by writing some shortcuts so now let’s have a look at the commands. Following are some of the shortcuts to add the files.

Git Commands Action Performed
git add –all Add all the files present in the repository
git add . Add all the files present in the current repository
git add -u To only add files that are currently tracked it will not track any other file which is not being tracked by the git

Now if we have modified any already existing files we can use the command git commit -am “Commit_message_here” this command will add and commit a file at the same time.

Adding and Committing a file in a single command

Good commit messages: So now if someone is traversing through git logs he/she should understand are the changes being done, why it has been done, and how it has been done.

Some of the examples of the Good Commit messages

Amending a commit

If some latest commit has been made into the repository that is not yet pushed to the upstream repository on GitHub and if you think that the commit message is incorrect we can edit or amend the commit message by using the below command as follows:

git commit --amend

The above command will open the default text editor for e.g vi, vim, or emacs and when it gets open you can see the commit message which you have used earlier now you can edit that commit message to whatever message you find suitable. It is illustrated via below pictorial aids as follows:

Committing the file c.txt

Seeing git logs

Opens up an editor for changing the commit message

Using the command git commit –amend

If we want the previous commit message only without changing it.

git commit --amend --no-edit

Using  the command git commit –amend –no-edit

Committing without opening an editor:

Git usually opens up a default editor like vi, vim, or emacs on running the git commit command so to avoid this we can also type in the commit message in the git commit command only bypassing the -m option.

git commit -m "Commit message here"

Using the git commit with -m as an option

We can also multiple messages as arguments in one command using the below command as follows:

git commit -m "message_1" -m "message_2" 

The syntax for passing multiple messages in a git commit command

Using git log we can see that the commit messages are in multiple lines now

Committing Changes Directly

For tracking the changes made to a file or a folder we first add it into the staging and then we commit that file but we can do this all in just one the command which will add the changes made to the file along with a commit message The command is basically as follows:  

git commit -am "commit_message here"

Using the git commit -am “commit_message here”

For committing a particular file present in a folder for that we can use the below command as follows: 

git commit /path_of_the_file -m "commit_message here"

For committing a particular file  git commit /path_of_the_file -m “commit_message here”

Now let us discuss which line should be staged for committing for selection

Suppose we have done many changes in one or more files but at last, we only want some of the changes to be committed so to select those changes we can use the below command so that we can select the desired changes.

The command is git add -p  file_name and whatever change we have done will be displayed individually for each change, we will be prompted to choose one of the following options. Let us understand the various options given by the command git add -p command

It is depicted below in tabular format below as follows:

Symbol Action Performed 
y Yes add this hunk 
n No don’t add this hunk
d No, don’t add this hunk, or any other remaining hunks for this file. useful if you have 
already added what you want to, and want to skip over the rest
s split the hunk into smaller hunks if possible
e Manually edit the hunk. This is probably the most powerful option it will open
the hunk in a text editor and you can edit it as needed. 

Exploring the -e option of git add -p

Creating an Empty Commit

As we know that while doing a commit we need to create a file or make changes in a file to commit the file but creating an empty commit will help us without having to edit or create a file we can easily create commits.

The –allow-empty will help us in creating the commit without having to edit or create a file.

Created an empty commit using the –allow-empty parameter

Committing Changes in Specific Files

As we know that for committing changes made to a file for that we need the file to be added in the staging area and then make a commit of those particular here in this explanation the files are already in the staging area that’s why on using the command git commit file_name1 file_name2 command is working.

Committing particular files

Committing at a Specific Date

While committing a file if you want to set a particular date that will appear in the standard output of the git log. For that, the command will be git commit -m ‘commit_message” –date YYYY-MM-date. 

The syntax for committing a file at a particular date

Let us see our commit in the git log

Here we can see that in the git log command output the given commit has the date that we mentioned in the git commit command

The date parameter accepts a lot of flexible formats which are being supported in git.

git commit -m 'commit_message' --date yesterday

Here we can see in the git log it is showing us yesterday’s date

git commit -m 'commit_message' --date '3 days ago'

Using the command git commit -m ‘commit_message’ –date ‘3 days ago’

Using the command git log and here we can see that it showing us the commit date 3 days ago because today is 30th march 

But when we don’t specify time git uses the current time and only the date changes then. Now if you want that the time should be changed.

Amending the Time of commit:

We can amend the time of the commit using the below command as follows: 

git commit --amend --date="day_name month_name date time YYYY -0400"

Or even

git commit --amend --date="now"

The second command shows the current date and time

Here we can see in the git log it is showing the current date and time



Similar Reads

Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
Git - git-show Command Line Utility
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
3 min read
Git LFS: Managing Large Files in Git Repositories
Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
4 min read
Git Subtree vs. Git Submodule
Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
4 min read
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage and track changes in their code. Setting up Git correctly is important for maintaining an organized and efficient workflow. In this article, we'll walk you through the process of setting up Git using the git config command. PrerequisitesBefore we dive into the setup process, make
3 min read
How To Exit Git Log or Git Diff?
When working with Git, the git log and git diff commands are important for viewing commit history and changes between commits, respectively. However, if you're new to Git, you might find yourself stuck in these views, unsure how to exit. This article will guide you through the steps to exit git log or git diff, ensuring a smooth experience while na
2 min read
Difference between “git add -A” and “git add”
When working with Git, the git add command is used to move changes from your working directory to the staging area. However, there are different options and flags that can be used with git add, such as -A, which might lead to some confusion. In this article, we'll explore the differences between git add -A and git add. 1. Git add- CommandThe git ad
2 min read
Git - Difference Between Git Revert, Checkout and Reset
Git offers a range of commands to manage and manipulate your codebase. Among these commands, git revert, git checkout, and git reset are frequently used for different purposes. Understanding the differences between these commands is important for effective version control. In this article, we'll explore the functionalities and use cases of git reve
6 min read
How to Fix Git Error "Unable to create '/path/my_project/.git/index.lock'"?
The Git error "Unable to create '/path/my_project/.git/index.lock'" typically occurs when Git is unable to create or write to the index.lock file. This file is used to prevent simultaneous processes from modifying the repository, which could lead to corruption. Table of Content Remove the Lock File ManuallyUse Git Command to Remove the Lock FileRes
2 min read
Difference Between "git commit" and "git push"?
Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break down their differences. What is git commit?git commit
2 min read
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. Git Fetch and Git Pull are two important commands in Git that help in managing remote repositories. While both commands involve retrieving data from remote repositories, they serve distinct purposes and are used in different contexts. In t
5 min read
Git - git prune
Git prune is used to remove the unreachable objects and clear up the local repository. Git is a powerful version control system that helps developers manage their code efficiently. However, as your project evolves, you may accumulate unnecessary data that clutters your repository. This is where git prune comes in view. Table of Content What is git
6 min read
Git For Non-Developers: How Different Industries Are Adopting Git?
While Git is widely recognized for its role in software development, it's increasingly being adopted by non-developers across various industries. This shift is driven by the growing need for version control, collaboration, and efficient project management in fields like marketing, design, data science, education, and even legal and finance sectors.
8 min read
Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
Git vs. Other Version Control Systems: Why Git Stands Out?
Version control systems (VCS) are an essential tool for developers, enabling them to track changes, collaborate on projects, and manage codebases efficiently. Among the various VCS options available, Git has emerged as the dominant choice, used by millions of developers and organizations worldwide. While other systems like Subversion (SVN), Mercuri
8 min read
Git - Changing History
Git provides a very important option of altering the commit history. As the master of git, you can control and make it function according to you. Git gives you the authority to change the order of commits, change messages, remove commits entirely. Moreover, it also allows you to manage the project's history. However, it is advisable to commit the w
3 min read
Introduction to Git Branch
Branching means diverging from the mainline and continue to work separately without messing with the mainline. Nearly every VCS has some form of branch support. In Git, a branch is simply a reference to the commit, where the following commits will be attached. Git vs SVN: In recent years, the use of git has risen tremendously. Unlike SVN, git allow
3 min read
Git Features
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it which may b
9 min read
Bare Repositories in Git
Repositories in Git are a snapshot of the folder in which you are working on your project. You can track the progress and changes made to the project by making commits and also revert changes if not satisfactory. Repositories can be divided into two types based on the usage on a server. These are: Non-bare Repositories Bare Repositories What is a N
7 min read
Getting changes from a Git Repository
Git allows performing various operations on the Repositories including the local repositories and remote repositories. The user when downloads a project to work upon, a local repository is created to store a copy of the original project. This local repository stores the changes that are being made by the user. This way, the user can edit the change
4 min read
Git | Working with Stash
Pre-requisites: Git Git allows multiple users to work on the same project simultaneously. Suppose a developer is working on a feature in a branch and he needs to pull changes from some other developer's branch or if he has to work urgently on some other feature, but the feature he is currently working on is incomplete. In this case, you cannot comm
8 min read
Handling repositories with Git remote
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Syncing ability of the git allows the developers to access and work on any project anytime anywhere. The collaboration among developers via git takes place when git provides each individual deve
4 min read
Using Git on CommandLine
Git is very important and powerful tool when it comes to the software development life cycle. Now in this article, we are going to see why git is used vastly in the software industry and what kinds of operations we can perform with git. So git is basically a distributed version control system for tracking changes in source code during software deve
5 min read
Working on BitBucket using Git
Git provides various tools and applications to perform better handling of the work environment. Git is a type of version control system that is used to allow more than one developer to access the source code of a specific application and can modify changes to it that may be seen by other developers. BitBucket is a git-based tool that helps with col
5 min read
Working on Git for GUI
Git has its native environment within the terminal. All the new features are updated first at the command line, and only there is the full power of Git. But plain text isn't the simplest choice for all tasks; sometimes some users are much more comfortable with a point-and-click interface, a visual representation is what they need. Note: There's not
4 min read
Git Rebase
Pre-requisites: Git Git rebase can integrate the changes from one branch to another by overcoming the problems that we might have faced while using the git merge command. The changes we will do will be recorded in the form of logs which are useful to go through if any mistakes happen. What is Branching?Branching means splitting from the master bran
12 min read
Recovering Lost Commits in Git
Git is a very powerful tool, and with great power comes great responsibilities. If not used and handles properly, It might cause you to lose your commits. There might be situations when you may find all of your work missing at once. If you have regularly committed your work, there is a way to recover these lost commits. This tutorial will aim at us
2 min read
Using Tags in Git
Tagging in GIT refers to creating specific points in the history of your repository/data. It is usually done to mark the release points. Two main purposes of tags are: Make Release point on your code. Create historic restore points. You can create tags when you want to create a release point for a stable version of your code. You can also create ta
2 min read
What is Collaboration in Git?
Pre-requisite: Git & GitHub Collaboration is the way different people can work on the same project together. It is like creating a group in GitHub just like Groups in other social media. The people added to the collaborator's list can be able to push, merge, and do other kinds of similar things on the project. Steps for Collaboration in GIT Ste
5 min read
Patch Operation in Git
Git patch is a feature in git which enables you to create a patch file from a feature in one branch and apply it in another branch. A patch file has all the differences between the two branches. Using the patch file, we can apply the changes in a different branch. This can be visualized as - Suppose you are working on a project. The main project is
3 min read
Article Tags :