How can I combine two commits into one commit?

前端 未结 3 701
情歌与酒
情歌与酒 2021-01-29 19:31

I have a branch \'firstproject\' with 2 commits. I want to get rid of these commits and make them appear as a single commit.

The command git merge --squash

相关标签:
3条回答
  • 2021-01-29 19:48

    Lazy simple version for forgetfuls like me:

    git rebase -i HEAD~3 or however many commits instead of 3.

    Turn this

    pick YourCommitMessageWhatever
    pick YouGetThePoint
    pick IdkManItsACommitMessage
    

    into this

    pick YourCommitMessageWhatever
    s YouGetThePoint
    s IdkManItsACommitMessage
    

    and do some action where you hit esc then enter to save the changes. [1]

    When the next screen comes up, get rid of those garbage # lines [2] and create a new commit message or something, and do the same escape enter action. [1]

    Wowee, you have fewer commits. Or you just broke everything.


    [1] - or whatever works with your git configuration. This is just a sequence that's efficient given my setup.

    [2] - you'll see some stuff like # this is your n'th commit a few times, with your original commits right below these message. You want to remove these lines, and create a commit message to reflect the intentions of the n commits that you're combining into 1.

    0 讨论(0)
  • 2021-01-29 19:58

    You want to git rebase -i to perform an interactive rebase.

    If you're currently on your "commit 1", and the commit you want to merge, "commit 2", is the previous commit, you can run git rebase -i HEAD~2, which will spawn an editor listing all the commits the rebase will traverse. You should see two lines starting with "pick". To proceed with squashing, change the first word of the second line from "pick" to "squash". Then save your file, and quit. Git will squash your first commit into your second last commit.

    Note that this process rewrites the history of your branch. If you are pushing your code somewhere, you'll have to git push -f and anybody sharing your code will have to jump through some hoops to pull your changes.

    Note that if the two commits in question aren't the last two commits on the branch, the process will be slightly different.

    0 讨论(0)
  • 2021-01-29 20:07
    1. Checkout your branch and count quantity of all your commits.
    2. Open git bash and write: git rebase -i HEAD~<quantity of your commits> (i.e. git rebase -i HEAD~5)
    3. In opened txt file change pick keyword to squash for all commits, except first commit (which is on the top). For top one change it to reword (which means you will provide a new comment for this commit in the next step) and click SAVE! If in vim, press esc then save by entering wq! and press enter.
    4. Provide Comment.
    5. Open Git and make "Fetch all" to see new changes.

    Done

    0 讨论(0)
提交回复
热议问题