{"id":554913,"date":"2023-02-21T20:00:31","date_gmt":"2023-02-21T17:00:31","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/git-commit-a-master-class\/"},"modified":"2023-02-21T20:00:31","modified_gmt":"2023-02-21T17:00:31","slug":"git-commit-a-master-class","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/","title":{"rendered":"#Git Commit: A Master Class"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a26ec29aa90b\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a26ec29aa90b\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#%E2%80%9CGit_Commit_A_Master_Class%E2%80%9D\" >&#8220;Git Commit: A Master Class&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#What_Is_a_Commit_in_Git\" >What Is a Commit in Git?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#Configuring_the_commit_Command\" >Configuring the commit Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#Using_the_commit_Command\" >Using the commit Command<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#A_Basic_Commit\" >A Basic Commit<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#Auto-Staging_Files\" >Auto-Staging Files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#Staging_and_Committing_at_the_Same_Time\" >Staging and Committing at the Same Time<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#Committing_to_a_Different_Branch\" >Committing to a Different Branch<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#Making_Changes_to_Commits\" >Making Changes to Commits<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#Removing_Changes_From_a_Commit\" >Removing Changes From a Commit<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#Reverting_an_Entire_Commit\" >Reverting an Entire Commit<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/buradabiliyorum.com\/en\/git-commit-a-master-class\/#The_Git_Swiss_Army_Knife\" >The Git Swiss Army Knife<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CGit_Commit_A_Master_Class%E2%80%9D\"><\/span>&#8220;Git Commit: A Master Class&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-770699\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/shutterstock_1278851809.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Laptop on a blue background showing a Linux command prompt.\" width=\"1200\" height=\"675\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/command-line-interface-cli-programming-language-1278851809\">fatmawati achmad zaenuri\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>The Git commit command stores copies of the changes from your working directory in your Git repository. But it can also be used to amend existing commits and to revert commits, too.<\/p>\n<p>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 <code>commit<\/code>. Here\u2019s everything you need to know.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Is_a_Commit_in_Git\"><\/span><a rel=\"nofollow noopener\" target=\"_blank\" name=\"autotoc_anchor_0\">What Is a Commit in Git?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Commits are the series of snapshots made throughout the lifecycle of a project 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?<\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Version_control\">Version control systems<\/a>\u00a0(VCSs) 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 chapters of a book.<\/p>\n<p>You might not want every file in your project directories to be handled by the VCS, so you need to be able to nominate the files you want to be version controlled. This adds them to the version control view of the project. They\u2019ll be monitored for changes.<\/p>\n<p>Another way to achieve this is\u00a0to use an ignore list. This tells Git which files, directories, or types of files it should always ignore.<\/p>\n<p>Over time, as new files are added to the project, some will require being added to the version control system. In Git, this is handled by the <code>add<\/code> command. Actually, the <code>add<\/code> command does double service, as we\u2019ll see.<\/p>\n<p>In order to maintain a history of the changes that have been made to the project, you\u2019ll periodically ask Git to store a snapshot of the state of the project, using the <code>commit<\/code> command. This is where the <code>add<\/code> command reappears in our workflow. We use the <code>add<\/code> command to tell Git which\u00a0<em>changed<\/em>\u00a0files we want to have included in the snapshot. Then we use <code>commit<\/code> to tell Git to create the snapshot.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Configuring_the_commit_Command\"><\/span>Configuring the commit Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Information about the commit is stored with it, so that it\u2019s always possible to know who made the commit, when, and what the commit contains. Some of this metadata is captured at commit-time, such as the commit message.<\/p>\n<p>Metadata relating to the identity of the members of the development team can be configured by each user, to prevent repeatedly providing the same information.<\/p>\n<p>To set your name globally for all repositories on your computer, use this command.<\/p>\n<pre>git config --global user.name \"Dave McKay\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869330\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/1-6.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Setting the global Git user name\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To verify your name has been set, use this command.<\/p>\n<pre>git config --global user.name<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869331\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/2-5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Checking the Git global user name\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you need to use a different name on a particular repository, change into the project\u2019s directory and use the same command without the <code>--global<\/code> option.<\/p>\n<pre>git config user.name \"McKay, David\"<\/pre>\n<pre>git config user.name<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869334\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/3-4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Setting a repository-specific Git user name\" width=\"644\" height=\"110\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We\u2019ve now got a different default user name for this repository, and our global name is still used for other repositories.<\/p>\n<p>In a similar fashion, we can set an email address either globally or for a single repository by including or omitting the <code>--global<\/code> option.<\/p>\n<pre>git config user.email \"dave@phony-domain.com\"<\/pre>\n<pre>git config --global user.email \"dave@fake-domain.com\"<\/pre>\n<pre>git config user.email<\/pre>\n<pre>git config --global user.email<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869335\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/4-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Setting global and repository-specific Git default user email addresses\" width=\"644\" height=\"230\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>These settings are held in config files. Global Git settings are held in \u201c~\/.gitconfig\u201d, and repository-specific settings are held in the repository\u2019s \u201c.git\/config\u201d file.<\/p>\n<p>The <code>commit<\/code> command references and uses these values as it operates.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_the_commit_Command\"><\/span><a rel=\"nofollow noopener\" target=\"_blank\" name=\"autotoc_anchor_2\">Using the commit Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The basic use of the <code>commit<\/code> command is to take the files that are in the staging area, known as the index, and store them as a commit in the current <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/devops\/how-do-git-branches-work\/\">branch of the repository.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"A_Basic_Commit\"><\/span>A Basic Commit<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>We\u2019ve got a project with a changed file. We\u2019ll use the <code>add<\/code> command to stage the file, then commit it. We\u2019re using the <code>-m<\/code> (commit message) option so that we can provide a short description of the purpose of the changes. If we don\u2019t use this option, we\u2019re prompted for a commit message as the commit takes place. It\u2019s more convenient to add one on the command line.<\/p>\n<pre>git add jibber.c<\/pre>\n<pre>git commit -m \"Updated help text\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869337\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/5-6.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Staging and committing a single file\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If we use the <code>git log<\/code> command we can <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/devops\/how-to-view-commit-history-with-git-log\/\">review the details of the commits, in chronological order, with the most recent commit at the top of the list.<\/p>\n<pre>git log<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869338\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/6-5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Checking the Git repository log\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The commits are displayed in <code>less<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869339\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/7-5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The latest commit at the top of the Git log\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The commit has been tagged with the name and email address we provided earlier, and our commit message is recorded too.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Auto-Staging_Files\"><\/span>Auto-Staging Files<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Staging many files can take a little time. A different approach is to use the <code>-A<\/code> (all) option with <code>add<\/code>.<\/p>\n<p>This automatically stages all <em>modified<\/em> files along with all currently <em>untracked<\/em> files. The staging of untracked files respects the settings in your \u201c.gitignore\u201d file. Git won\u2019t stage files you\u2019ve told it you don\u2019t want to be included. Finally, files in the index that are no longer in the working directory are <em>removed<\/em> from the index.<\/p>\n<p>Plainly, the <code>-A<\/code> option can cause a lot to happen all at once. The <code>--dry-run<\/code> option gives you a preview of the changes without actually performing them.<\/p>\n<pre>git add -A --dry-run<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869341\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/8-5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using --dry-run to preview the files that will be staged and unstaged\" width=\"644\" height=\"150\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In our example, it\u2019ll stage two modified existing files, and two new files. Let\u2019s go ahead and use the <code>-A<\/code> option before we use the <code>commit<\/code> command.<\/p>\n<pre>git add -A<\/pre>\n<pre>git commit -m \"Enhanced parsing\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869345\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/9-5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Committing changed and newly created files, after using the add -A option\" width=\"644\" height=\"170\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We can see that altogether four files are changed. Two of them are the newly created files, which are listed.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Staging_and_Committing_at_the_Same_Time\"><\/span><a rel=\"nofollow noopener\" target=\"_blank\" name=\"autotoc_anchor_5\">Staging and Committing at the Same Time<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The <code>commit<\/code> command has a lowercase <code>-a<\/code> (all) option. This performs the staging and the committing of files in one step.<\/p>\n<p>The <code>commit -a<\/code> option stages and commits modified <em>existing<\/em> files, and <em>removes<\/em> files from the index if they have been removed from your working directory. It <em>doesn\u2019t<\/em> automatically stage untracked files.<\/p>\n<p>Like the <code>add<\/code> command, the commit command has a <code>--dry-run<\/code> option that allows you to preview its actions before executing it.<\/p>\n<pre>git commit -a --dry-run<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869348\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/10-5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using the commit -a option to preview changes without performing them\" width=\"644\" height=\"250\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Now let\u2019s carry out the command.<\/p>\n<pre>git commit -a --dry-run<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869349\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/11-5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using the commit -a option to stage and commit in one step\" width=\"644\" height=\"110\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The files are staged and committed for us.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Committing_to_a_Different_Branch\"><\/span><a rel=\"nofollow noopener\" target=\"_blank\" name=\"autotoc_anchor_6\">Committing to a Different Branch<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If you\u2019ve made some changes to files in your work directory then realize you didn\u2019t checkout the correct branch, you need to get your changes committed to the correct branch without affecting the current branch.<\/p>\n<p>Git doesn\u2019t have a command for committing to a different branch. But you can rectify this situation with a little bit of Git dexterity.<\/p>\n<p>We\u2019ll use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/777899\/how-to-stash-changes-in-git\/\">the Git <code>stash<\/code> command to make a copy of the changes. Then we\u2019ll check out the correct branch and apply the changes from the stash. To apply the stashed changes we\u2019re using the <code>pop<\/code> command rather than the <code>apply<\/code> command. The <code>pop<\/code> command applies the changes and also removes them from the stash.<\/p>\n<p>We\u2019ve made some changes in our repository\u2019s <code>new-parser<\/code> branch. They should have been made in the <code>classic-parser<\/code> branch.<\/p>\n<pre>git stash<\/pre>\n<pre>git checkout classic-parser<\/pre>\n<pre>git stash pop<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869355\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/12-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Stashing changes, checking out a branch, and applying the changes from the stash\" width=\"644\" height=\"355\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We can now perform a <code>commit<\/code>, and update this branch.<\/p>\n<pre>git commit -a -m \"Added pre-parser functions\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869357\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/13-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Committing the changes that were retrieved from the stash\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If we return to the <code>new-parser<\/code> branch we can see that it is up to date, meaning the changes have been removed from your working directory, and your repository and files are in sync.<\/p>\n<pre>git checkout new-parser<\/pre>\n<pre>git status<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869358\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/14-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Checking the state of a branch to make sure it is up to date\" width=\"644\" height=\"150\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How To Update and Maintain Separate Git Branches<\/em><\/strong><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Making_Changes_to_Commits\"><\/span>Making Changes to Commits<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If you need to improve your commit message\u2014perhaps you\u2019ve spotted a typo in it\u2014or you forgot to stage a file that should have been included in the commit, you can use the <code>--amend<\/code> option to put things right. The caveat is, this shouldn\u2019t be used on commits that have been pushed to a remote repository.<\/p>\n<p>In our last commit message, \u201cfraze\u201d should have been \u201cphrase.\u201d If we use <code>git log<\/code> we can see this.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869362\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/15-4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The Git log with a type highlighted\" width=\"644\" height=\"237\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To correct this, we\u2019ll use the <code>--amend<\/code> option like this.<\/p>\n<pre>git commit --amend -m \"Optimized phrase identification\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869363\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/16-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using the commit --amend option to correct a commit message\" width=\"644\" height=\"150\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If we use <code>git log<\/code> once more, we can see the old commit has been replaced by a new one with the corrected commit message.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869366\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/17-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The Git log showing the corrected commit message\" width=\"644\" height=\"236\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If we want to add a file that we forgot to stage, we can commit that file so that it appears as part of the previous commit.<\/p>\n<p>We\u2019ll use <code>add<\/code> to stage the file, then make a commit with the <code>--amend<\/code> option. The <code>--no-edit<\/code> option means we don\u2019t need to provide a new commit message. The previous commit message is retained.<\/p>\n<pre>git add jibber.c<\/pre>\n<pre>git commit --amend --no-edit<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869369\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/18-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Committing a file to the previous commit\" width=\"644\" height=\"150\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Removing_Changes_From_a_Commit\"><\/span><a rel=\"nofollow noopener\" target=\"_blank\" name=\"autotoc_anchor_8\">Removing Changes From a Commit<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If you\u2019ve inadvertently staged and committed a file that you didn\u2019t intend to, you can remove that file from the commit using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/devops\/how-does-git-reset-actually-work-soft-hard-and-mixed-resets-explained\/\">the <code>reset<\/code> command. We\u2019ll reset the commit back to the staging area, or index. Then we\u2019ll remove the file, and re-commit the rest of the files.<\/p>\n<p>To reset the last commit to the staging area, we use the <code>reset --soft<\/code> command. <code>HEAD~<\/code> is shorthand for \u201cthe commit behind the HEAD of the project commit timeline\u201d, or in English, \u201cthe last commit.\u201d<\/p>\n<pre>git reset --soft HEAD~<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869376\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/19.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Sending the last commit back to the staging area\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To remove the file that shouldn\u2019t have been included, we use the <code>reset --mixed<\/code> command. This resets those changes back into the working directory, recreating the modified file as an unstaged, uncommitted file.<\/p>\n<pre>git reset --mixed jibber.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-869381 size-full\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/20-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Removing a file from the Git staging area\" width=\"644\" height=\"110\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We need to commit the other files that are left in the index.<\/p>\n<pre>git commit -m \"Experimental tweaks\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869382\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/21-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Committing the files that remain in the staging area\" width=\"644\" height=\"110\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The other two files that were in the original commit are re-committed for us.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Fix, Edit, or Undo Git Commits (Changing Git History)<\/em><\/strong><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Reverting_an_Entire_Commit\"><\/span>Reverting an Entire Commit<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Sometimes undoing an entire commit is the easiest thing to do. It puts your working directory and repository back to the state they were in before you committed.<\/p>\n<p>We need to use the commit\u2019s hashed reference ID. We can find this using <code>git log<\/code>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869385\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/22-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The Git log with a commit identifier highlighted\" width=\"644\" height=\"221\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Copy that reference and use it in the <code>revert<\/code> command:<\/p>\n<pre>git revert e5bd4560aef8164c6ca9d6d4620b9db7f66aecc8<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869387\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/23-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Reverting a commit identified by its commit ID\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This will open your default editor so you can edit a revert message. There is a default message entered for you. You can either use this, or edit it to your liking.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869388\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/24.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Editing the revert message, with the default message highlighted\" width=\"644\" height=\"360\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>When you\u2019re happy with your revert message, save the file and exit the editor. In <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/42980\/the-beginners-guide-to-nano-the-linux-command-line-text-editor\/\">nano, you do this with \u201cCtrl+O\u201d, and \u201cCtrl+X.\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-869389\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2023\/01\/25.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The completed revert action, undoing an entire commit\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Using <code>git log<\/code> once more, we can see that a new commit has been added that undoes the changes of the reverted commit.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"The_Git_Swiss_Army_Knife\"><\/span>The Git Swiss Army Knife<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Obviously, <code>commit<\/code> is one of the most important Git commands. It can do a lot, so there\u2019s a lot to learn. Getting to grips with its lesser-used features is time well spent. When you need to correct a mistake\u2014right now\u2014you\u2019ll be glad you\u2019ve prepared in advance.<\/p>\n<p><strong>RELATED:<\/strong> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/853521\/git-merge\/\"><strong><em>How to Use Git merge<\/em><\/strong><\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/869305\/git-commit\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;Git Commit: A Master Class&#8221; fatmawati achmad zaenuri\/Shutterstock.com The Git commit command stores copies of the changes from your working directory in your Git repository. But it can also be used to amend existing commits and to revert commits, too. A basic requirement of any version control system is to store different versions of files&#8230;<\/p>\n","protected":false},"author":1,"featured_media":554914,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/shutterstock_1278851809.png?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-554913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/554913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=554913"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/554913\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/554914"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=554913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=554913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=554913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}