15. Removing a commit from a branch (2024)

Goals

  • Learn to delete the branch's latest commits.

Revert is a powerful command of the previous section that allows you to cancel any commits to the repository. However, both original and cancelled commits are seen in the history of the branch (when using git log command).

Often after a commit is already made, we realize it was a mistake. It would be nice to have an undo command which allows the incorrect commit(s) to be immediately deleted. This command would prevent the appearance of one or more unwanted commits in the git log history.

01 The reset command

We have already seen the reset command and have used it to set the staging area to be consistent with a given commit (we used the HEAD commit in our previous lesson).

When you run the reset command along with a commit reference (HEAD, branch or tag name, commit hash, etc.), the command will...

  1. Point the current branch to the specified commit.
  2. Optionally reset the staging area so it will comply with the specified commit.
  3. Optionally reset the working directory so it will match the specified commit.

02 Check our history

Let us do a quick scan of our commit history.

Run

git log

Result

$ git log86364a1 2023-11-28 | Revert "Oops, we didn't want this commit" (HEAD -> main) [Alexander Shvets]6a44bec 2023-11-28 | Oops, we didn't want this commit [Alexander Shvets]b7614c1 2023-11-28 | Added HTML header (tag: v1) [Alexander Shvets]46afaff 2023-11-28 | Added standard HTML page tags (tag: v1-beta) [Alexander Shvets]78433de 2023-11-28 | Added h1 tag [Alexander Shvets]5836970 2023-11-28 | Initial commit [Alexander Shvets]

We see the last two commits in this branch are "Oops" and "Revert Oops". Let us remove them with the reset command.

03 Mark this branch first

Let us mark the last commit with tag, so you can find it after removing a commit(s).

Run

git tag oops

Result

$ git log86364a1 2023-11-28 | Revert "Oops, we didn't want this commit" (HEAD -> main, tag: oops) [Alexander Shvets]6a44bec 2023-11-28 | Oops, we didn't want this commit [Alexander Shvets]b7614c1 2023-11-28 | Added HTML header (tag: v1) [Alexander Shvets]46afaff 2023-11-28 | Added standard HTML page tags (tag: v1-beta) [Alexander Shvets]78433de 2023-11-28 | Added h1 tag [Alexander Shvets]5836970 2023-11-28 | Initial commit [Alexander Shvets]

04 Reset commit to before oops

In the history log above, the commit tagged v1 is before the "Oops" and "Revert Oops" commits. Let us reset the branch to that point. As the branch has a tag, we can use the tag name in the reset command (if it does not have a tag, we can use the hash of the commit).

Run

git reset --hard v1git log

Result

$ git reset --hard v1HEAD is now at b7614c1 Added HTML header$ git logb7614c1 2023-11-28 | Added HTML header (HEAD -> main, tag: v1) [Alexander Shvets]46afaff 2023-11-28 | Added standard HTML page tags (tag: v1-beta) [Alexander Shvets]78433de 2023-11-28 | Added h1 tag [Alexander Shvets]5836970 2023-11-28 | Initial commit [Alexander Shvets]

Our main branch is pointing at commit v1 and the "Revert Oops" and "Oops" commits no longer exist in the branch. The --hard parameter makes the working directory reflect the new branch head.

05 Nothing is ever lost

What happened to the wrong commits? They are still in the repository. Actually, we can still refer to them. At the beginning of the lesson, we created the oops tag for the canceled commit. Let us take a look at all commits.

Run

git log --all

Result

$ git log --allb7614c1 2023-11-28 | Added HTML header (HEAD -> main, tag: v1) [Alexander Shvets]86364a1 2023-11-28 | Revert "Oops, we didn't want this commit" (tag: oops) [Alexander Shvets]46afaff 2023-11-28 | Added standard HTML page tags (tag: v1-beta) [Alexander Shvets]6a44bec 2023-11-28 | Oops, we didn't want this commit [Alexander Shvets]78433de 2023-11-28 | Added h1 tag [Alexander Shvets]5836970 2023-11-28 | Initial commit [Alexander Shvets]

We can see that the wrong commits are not gone. They are not listed in the main branch anymore but still remain in the repository. They would still be in the repository if we did not tag them, but then we could reference them only by their hashes. Unreferenced commits remain in the repository until the garbage collection software is run by system.

06 Reset dangers

Resets on local branches are usually harmless. The consequences of any "accident" can be reverted by using the proper commit.

However, other users sharing the branch can be confused if the branch is shared on remote repositories.

15. Removing a commit from a branch (2024)
Top Articles
What is customer experience management? - Zendesk
Keeping SEO Rankings During Platform Migration to Shopify
What Time Subway Open
Pobierz Papa's Mocharia To Go! na PC za pomocą MEmu
A Qué Hora Cierran Spectrum
Colossians 2 Amplified
manhattan cars & trucks - by owner - craigslist
Old Navy Student Discount Unidays
Free Shredding Events Near Me 2023
Where Is The Nearest Five Below
New Haven Music Festival
12 Week Glute Program to Transform Your Booty with Free PDF - The Fitness Phantom
Fingerfang Rock Conan
Www.binghamton Craigslist.com
Suman Gharti on LinkedIn: Yashoda [2022] Hindi Dubbed Full Movie In 4K UHD | Samantha, Unni…
Bowser's Fury Coloring Page
Clarksville.craigslist
Watch The Most Popular Video Of Mikayla Campinos Online
Liquor World Sharon Ma
Zillow Group, Inc. Aktie (A14NX6) - Kurs Nasdaq - MarketScreener
Colt Gray and his father, Colin Gray, appear in court to face charges in Georgia school shooting
Advance Auto Parts Near Me Open Now
Kickflip Seeds
Junior's Barber Shop & Co — Jupiter
Craiglist Galveston
Tryst Independent
Aflac Dulles Synergy
Erica Mena Net Worth Forbes
Israel Tripadvisor Forum
Craigslist Gigs Wichita Ks
Does Wanda Sykes Use A Cane
Dawson Myers Fairview Nc
Patient Portal Bayfront
247 Transfer Portal Rankings Basketball
Bj's Gas Price Victor Ny
Mission Impossible 7 Showtimes Near Regal Willoughby Commons
Encore Atlanta Cheer Competition
Best Upscale Restaurants In Denver
Strip Clubs In Hayward Ca
Joy Ride 2023 Showtimes Near Amc Ward Parkway
What Do Manta Rays Eat In Ark
Bloxburg Bedroom Ideas That Will Make Your Kid's Jaw Drop
Hourly Weather Forecast for Amsterdam, North Holland, Netherlands - The Weather Channel | Weather.com
Bob Wright Yukon Accident
5613192063
Embu village mines precious coltan for years 'without knowing its value’
Sesame Street 4323
Knock At The Cabin Showtimes Near Alamo Drafthouse Raleigh
Nordstrom Rack Glendale Photos
Ktbs Payroll Login
Christian Publishers Outlet Rivergate
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 5710

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.