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 fileShortcuts: 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 commandGood 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 messagesAmending 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 --amendIf we want the previous commit message only without changing it.
git commit --amend --no-edit
Using the command git commit --amend --no-editCommitting 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 optionWe 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 nowCommitting 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 -pCreating 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 filesCommitting 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 dateLet 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 commandThe 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 dategit 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
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
HTML Interview Questions and Answers
HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers
NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
What is an API (Application Programming Interface)
In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Introduction of Firewall in Computer Network
A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa
10 min read
Web Development Technologies
Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.Core Concepts of Web Development TechnologiesWeb
8 min read