Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6829919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:33:25+00:00 2026-05-26T22:33:25+00:00

I created a new file (with quite a bit of code and hard work)

  • 0

I created a new file (with quite a bit of code and hard work) and committed it to the master branch of my git repo. A moment later I realized that I probably should’ve created a branch for the file. I thought I could revoke the commit from master and commit again on a branch, but I think I did things in a silly order. For some reason I created the branch first, then went to revoke the commit, using is it possible to revoke commits? as a guide.

Here’s essentially what I did:

echo "Lots of code and hard work" > newfile.txt
git commit -m "thoughtless commit" newfile.txt
# Oh wait, I should've branched!
git branch thoughtful
git checkout thoughtful
# Hmm, I didn't mean for that last commit to be in master
git checkout master
# get the previous commit's hash
oldhead=$(git log | awk 'BEGIN{commitcount=0; } /^commit/{ if (commitcount++ == 1) { print $2 }; }')
git reset --hard "$oldhead"
git checkout thoughtful

So now I’ve apparently trashed a commit on master (indeed, ‘newfile.txt’ no longer exists on master), but it still exists in thoughtful (and I guess reflog).

Would someone be so kind as to explain what I did here, and why no commit on thoughtful is required?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:33:26+00:00Added an answer on May 26, 2026 at 10:33 pm

    When you created your branch thoughtful, it started from the current state of master, which already contained the commit. From that point onwards, the two branches are unrelated. Whatever you do to master, it won’t affect thoughtful. The commit that introduces the file will still be part of thoughtful regardless.

    You should try git log thoughtful and git log master to review the history of commits. On thoughtful you’ll still see the commit you tried to remove from master.

    A more complete breakdown follows

    Assuming we’re currently on master…

    echo "Lots of code and hard work" > newfile.txt
    git commit -m "thoughtless commit" newfile.txt
    

    You’ve created a commit on master, containing newfile.txt.

    # Oh wait, I should've branched!
    git branch thoughtful
    

    You’ve created a branch thoughtful, which is at the current state master, but is otherwise completely unrelated to master. The branch’s starting point is master, because you didn’t explicitly specify a starting point and master is currently checked out.

    git checkout thoughtful
    

    Now you’re on thoughtful; you don’t have to checkout new branches to make them real, this step has absolutely no effect since you do the following:

    # Hmm, I didn't mean for that last commit to be in master
    git checkout master
    

    Now you’re on master.

    # get the previous commit's hash
    oldhead=$(git log | awk 'BEGIN{commitcount=0; } /^commit/{ if (commitcount++ == 1) { print $2 }; }')
    

    You’ve jumped through a ton of hoops to get the previous commit, when you could have just used master~ or HEAD~. That is how you get the "previous" commit, not by inspecting git log. In the future, to undo the last commit, please use git reset --hard HEAD~.

    git reset --hard "$oldhead"
    

    You’ve changed the commit master points to. Now it’s whatever commit the above command discovered, presumably the commit before newfile.txt was introduced. Using --hard causes Git to update the working directory as well, so newfile.txt goes away; had you omitted --hard, newfile.txt would still be in the directory, but listed as a new file in git status.

    git checkout thoughtful
    

    You’ve gone back to thoughtful, which contains newfile.txt, updating your working directory and reintroducing newfile.txt.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using git through Eclipse. I created a new file, as well as making
I have created a new JSP file called loggedOut.jsp and after I click logout
There is a new file [project-name].otares created in Delphi XE2. The wiki says: File
Today I created a new staging table and a BCP .fmt file. I created
I've created a new project in VS2008, and added a .MDF file to the
I have created a file with XmlDocument xmldoc = new XmlDocument(); Can I make
I created a javascript application with all of the code in one file. The
I have a very simple bit of code that won't work, and have no
I have (after quite a bit of work) a web test that will upload
I have written quite a bit of code of the past few years. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.