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

  • SEARCH
  • Home
  • 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 1106165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:44:58+00:00 2026-05-17T01:44:58+00:00

I just read amending a single file in a past commit in git but

  • 0

I just read amending a single file in a past commit in git but unfortunately the accepted solution ‘reorders’ the commits, which is not what I want. So here’s my question:

Every now and then, I notice a bug in my code while working on an (unrelated) feature. A quick git blame then reveals that the bug has been introduced a few commits ago (I commit quite a lot, so usually it’s not the most recent commit which introduced the bug). At this point, I usually do this:

git stash                      # temporarily put my work aside
git rebase -i <bad_commit>~1   # rebase one step before the bad commit
                               # mark broken commit for editing
vim <affected_sources>         # fix the bug
git add <affected_sources>     # stage fixes
git commit -C <bad_commit>     # commit fixes using same log message as before
git rebase --continue          # base all later changes onto this

However, this happens so often that the above sequence is getting annoying. Especially the ‘interactive rebase’ is boring. Is there any shortcut to the above sequence, which lets me amend an arbitrary commit in the past with the staged changes? I’m perfectly aware that this changes the history, but I’m doing mistakes so often that I’d really love to have something like

vim <affected_sources>             # fix bug
git add -p <affected_sources>      # Mark my 'fixup' hungs for staging
git fixup <bad_commit>             # amend the specified commit with staged changes,
                                   # rebase any successors of bad commit on rewritten 
                                   # commit.

Maybe a smart script which can rewrite commits using plumbing tools or so?

  • 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-17T01:44:59+00:00Added an answer on May 17, 2026 at 1:44 am

    UPDATED ANSWER

    A while ago, a new --fixup argument was added to git commit which can be used to construct a commit with a log message suitable for git rebase --interactive --autosquash. So the simplest way to fixup a past commit is now:

    $ git add ...                           # Stage a fix
    $ git commit --fixup=a0b1c2d3           # Perform the commit to fix broken a0b1c2d3
    $ git rebase -i --autosquash a0b1c2d3~1 # Now merge fixup commit into broken commit
    

    ORIGINAL ANSWER

    Here’s a little Python script I wrote a while ago which implements this git fixup logic I hoped for in my original question. The script assumes that you staged some changes and then applies those changes to the given commit.

    NOTE: This script is Windows-specific; it looks for git.exe and sets the GIT_EDITOR environment variable using set. Adjust this as needed for other operating systems.

    Using this script I can implement precisely the ‘fix broken sources, stage fixes, run git fixup ‘ workflow I asked for:

    #!/usr/bin/env python
    from subprocess import call
    import sys
    
    # Taken from http://stackoverflow.com/questions/377017/test-if-executable-exists-in python
    def which(program):
        import os
        def is_exe(fpath):
            return os.path.exists(fpath) and os.access(fpath, os.X_OK)
    
        fpath, fname = os.path.split(program)
        if fpath:
            if is_exe(program):
                return program
        else:
            for path in os.environ["PATH"].split(os.pathsep):
                exe_file = os.path.join(path, program)
                if is_exe(exe_file):
                    return exe_file
    
        return None
    
    if len(sys.argv) != 2:
        print "Usage: git fixup <commit>"
        sys.exit(1)
    
    git = which("git.exe")
    if not git:
        print "git-fixup: failed to locate git executable"
        sys.exit(2)
    
    broken_commit = sys.argv[1]
    if call([git, "rev-parse", "--verify", "--quiet", broken_commit]) != 0:
        print "git-fixup: %s is not a valid commit" % broken_commit
        sys.exit(3)
    
    if call([git, "diff", "--staged", "--quiet"]) == 0:
        print "git-fixup: cannot fixup past commit; no fix staged."
        sys.exit(4)
    
    if call([git, "diff", "--quiet"]) != 0:
        print "git-fixup: cannot fixup past commit; working directory must be clean."
        sys.exit(5)
    
    call([git, "commit", "--fixup=" + broken_commit])
    call(["set", "GIT_EDITOR=true", "&&", git, "rebase", "-i", "--autosquash", broken_commit + "~1"], shell=True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just read an article on www.songho.ca which indicates that a projection matrix is
I just read a blog which says ...methods that couldn’t be inlined previously because
I just read this question and the accepted answer: What is JavaScript garbage collection?
I just read this question , and a solution states that: The fact that
I just read that the VARCHAR(MAX) datatype (which can store close to 2GB of
I just read this post about why new-line warnings exist, but to be honest
I just read thought php doucmentation for heredocs but I did not see any
I just read http://olstrans.sourceforge.net/release/OLS2000-ext3/OLS2000-ext3.html which is an excellent review of the design issues used
I just read the Wikipedia article on mock objects , but I'm still not
I just read about mysql's SQL_CALC_FOUND_ROWS and FOUND_ROWS() which helps you get the total

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.