Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
Committing in Git
Next article icon

Committing in Git

Last Updated : 26 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 CommandsAction Performed
git add --allAdd all the files present in the repository
git add .Add all the files present in the current repository
git add -uTo 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:

SymbolAction Performed 
yYes add this hunk 
nNo don't add this hunk
dNo, 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
ssplit the hunk into smaller hunks if possible
eManually 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

Next Article
Committing in Git

A

ankitmahajan852
Improve
Article Tags :
  • Git
  • Web Technologies

Similar Reads

    What is Git Commit?
    Git is a powerful version control system that helps developers manage and track changes in their code. One of the fundamental concepts in Git is the "git commit."This command is important for recording changes to a repository and forms the backbone of version control. In this article, we will explor
    5 min read
    Git - Squash Commits
    In Git, squashing refers to combining two or more commits into a single commit. This is typically done to improve the organization and clarity of the commit history, especially when working on a collaborative project with other developers. In this article, we will review the feature git squashing, w
    2 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 tr
    5 min read
    How to Force Commit in Git?
    Git is a powerful version control system used by developers worldwide to manage and track changes in their codebases. However, there are times when you might need to force commit changes in Git, overriding previous commits or pushing changes to a remote repository with conflicts. This guide will exp
    3 min read
    Examining Git
    Git is a powerful version control system that helps to manage projects efficiently. Initially created by Linus Torvalds in 2005 for the development of the Linux kernel, Git has since become the go-to tool for version control in software development. Its distributed nature allows multiple developers
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences