Are there any Git command to combine all our ugly commits together to one?












9















When I write the code in local, sometimes I commit the code that isn't clean yet, or, with an ugly message as temporary revision.



However, when I want my code to merge with the others, I would like the only final snapshot that the other can see, (hidden the revisions that look ugly)



Ex.
I fork 0 to my local repository, I make change and test and commit with unclean code



0->1->2->3->4->5->6 (Final code)



when the others pull the code, is it possible to make other see only final state? and not see the tree from 1..5



I want users will see like



0------------------->6



One way I can think of is making a patch file, but it not good enough if there is some file have to be deleted or create.










share|improve this question

























  • stackoverflow.com/questions/5308816/how-to-use-git-merge-squash

    – Gerry
    Sep 7 '11 at 10:26
















9















When I write the code in local, sometimes I commit the code that isn't clean yet, or, with an ugly message as temporary revision.



However, when I want my code to merge with the others, I would like the only final snapshot that the other can see, (hidden the revisions that look ugly)



Ex.
I fork 0 to my local repository, I make change and test and commit with unclean code



0->1->2->3->4->5->6 (Final code)



when the others pull the code, is it possible to make other see only final state? and not see the tree from 1..5



I want users will see like



0------------------->6



One way I can think of is making a patch file, but it not good enough if there is some file have to be deleted or create.










share|improve this question

























  • stackoverflow.com/questions/5308816/how-to-use-git-merge-squash

    – Gerry
    Sep 7 '11 at 10:26














9












9








9


3






When I write the code in local, sometimes I commit the code that isn't clean yet, or, with an ugly message as temporary revision.



However, when I want my code to merge with the others, I would like the only final snapshot that the other can see, (hidden the revisions that look ugly)



Ex.
I fork 0 to my local repository, I make change and test and commit with unclean code



0->1->2->3->4->5->6 (Final code)



when the others pull the code, is it possible to make other see only final state? and not see the tree from 1..5



I want users will see like



0------------------->6



One way I can think of is making a patch file, but it not good enough if there is some file have to be deleted or create.










share|improve this question
















When I write the code in local, sometimes I commit the code that isn't clean yet, or, with an ugly message as temporary revision.



However, when I want my code to merge with the others, I would like the only final snapshot that the other can see, (hidden the revisions that look ugly)



Ex.
I fork 0 to my local repository, I make change and test and commit with unclean code



0->1->2->3->4->5->6 (Final code)



when the others pull the code, is it possible to make other see only final state? and not see the tree from 1..5



I want users will see like



0------------------->6



One way I can think of is making a patch file, but it not good enough if there is some file have to be deleted or create.







git






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 18 '18 at 5:02









jiexishede

1,06421635




1,06421635










asked Sep 7 '11 at 10:20









scalopusscalopus

1,35621129




1,35621129













  • stackoverflow.com/questions/5308816/how-to-use-git-merge-squash

    – Gerry
    Sep 7 '11 at 10:26



















  • stackoverflow.com/questions/5308816/how-to-use-git-merge-squash

    – Gerry
    Sep 7 '11 at 10:26

















stackoverflow.com/questions/5308816/how-to-use-git-merge-squash

– Gerry
Sep 7 '11 at 10:26





stackoverflow.com/questions/5308816/how-to-use-git-merge-squash

– Gerry
Sep 7 '11 at 10:26












5 Answers
5






active

oldest

votes


















5














You can use reset --soft to squash multiple commits into a single new clean commit. With your branch at the final code with no staged changes you can do:



git reset --soft <sha1-of-0>
git commit


When git commit prompts, give a clear commit message describing the complete changes being introduced.






share|improve this answer































    3














    The most flexible way to do this is to do an interactive rebase to squash together or amend your commits. You need to be careful not to rewrite any history that's already public, however. For example, if you're working on the master branch, and you want to rewrite and linearize all your commits that aren't in origin/master (which typically means the work that you haven't pushed yet) you can do:



    git rebase -i origin/master


    That will launch a text editor where each commit you have that isn't in origin/master is shown, from oldest to newest. If you want want to combine any commit with the previous one, you can just change the start of the line from pick to squash. Or, if you want to amend that commit somehow, you can change pick to edit.



    Using interactive rebase is the most flexible way of doing what you ask for, and it's a very useful tool to get to know, but in the exact situation you describe, it's probably overkill. First, check that the output of git status is clean, so that you're sure that your staging area represents the same state as HEAD. You can then just do a "soft reset" back to 0, which resets HEAD to that commit, but leaves your working tree and index (i.e. the staging area) intact. If you then do a commit, your index will be used to create a new commit with the same state as 6, but with 0 as a parent. i.e.



     git status # Check that your status is clean
    git reset --soft 0
    git commit -m 'Work that is totally flawless'





    share|improve this answer































      2














      Yes, use git rebase -i, and squash the commits. Check this for more info and some examples.






      share|improve this answer































        2














        Assuming you haven't already pushed your commits to the remote then there are several ways to do this.



        One option is to use



        git rebase -i 


        which gives you the interactive rebase editor. You can then mark all the commits except the last as 'squash'






        share|improve this answer

































          0














          Git squash merge is designed for this purpose. The way you would do this is to create a branch, make all your changes, and then merge back using squash merge.



          How to use git merge --squash?



          However, this isn't usually recommended because you will lose history this way.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7332260%2fare-there-any-git-command-to-combine-all-our-ugly-commits-together-to-one%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            You can use reset --soft to squash multiple commits into a single new clean commit. With your branch at the final code with no staged changes you can do:



            git reset --soft <sha1-of-0>
            git commit


            When git commit prompts, give a clear commit message describing the complete changes being introduced.






            share|improve this answer




























              5














              You can use reset --soft to squash multiple commits into a single new clean commit. With your branch at the final code with no staged changes you can do:



              git reset --soft <sha1-of-0>
              git commit


              When git commit prompts, give a clear commit message describing the complete changes being introduced.






              share|improve this answer


























                5












                5








                5







                You can use reset --soft to squash multiple commits into a single new clean commit. With your branch at the final code with no staged changes you can do:



                git reset --soft <sha1-of-0>
                git commit


                When git commit prompts, give a clear commit message describing the complete changes being introduced.






                share|improve this answer













                You can use reset --soft to squash multiple commits into a single new clean commit. With your branch at the final code with no staged changes you can do:



                git reset --soft <sha1-of-0>
                git commit


                When git commit prompts, give a clear commit message describing the complete changes being introduced.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 7 '11 at 10:23









                CB BaileyCB Bailey

                514k78555611




                514k78555611

























                    3














                    The most flexible way to do this is to do an interactive rebase to squash together or amend your commits. You need to be careful not to rewrite any history that's already public, however. For example, if you're working on the master branch, and you want to rewrite and linearize all your commits that aren't in origin/master (which typically means the work that you haven't pushed yet) you can do:



                    git rebase -i origin/master


                    That will launch a text editor where each commit you have that isn't in origin/master is shown, from oldest to newest. If you want want to combine any commit with the previous one, you can just change the start of the line from pick to squash. Or, if you want to amend that commit somehow, you can change pick to edit.



                    Using interactive rebase is the most flexible way of doing what you ask for, and it's a very useful tool to get to know, but in the exact situation you describe, it's probably overkill. First, check that the output of git status is clean, so that you're sure that your staging area represents the same state as HEAD. You can then just do a "soft reset" back to 0, which resets HEAD to that commit, but leaves your working tree and index (i.e. the staging area) intact. If you then do a commit, your index will be used to create a new commit with the same state as 6, but with 0 as a parent. i.e.



                     git status # Check that your status is clean
                    git reset --soft 0
                    git commit -m 'Work that is totally flawless'





                    share|improve this answer




























                      3














                      The most flexible way to do this is to do an interactive rebase to squash together or amend your commits. You need to be careful not to rewrite any history that's already public, however. For example, if you're working on the master branch, and you want to rewrite and linearize all your commits that aren't in origin/master (which typically means the work that you haven't pushed yet) you can do:



                      git rebase -i origin/master


                      That will launch a text editor where each commit you have that isn't in origin/master is shown, from oldest to newest. If you want want to combine any commit with the previous one, you can just change the start of the line from pick to squash. Or, if you want to amend that commit somehow, you can change pick to edit.



                      Using interactive rebase is the most flexible way of doing what you ask for, and it's a very useful tool to get to know, but in the exact situation you describe, it's probably overkill. First, check that the output of git status is clean, so that you're sure that your staging area represents the same state as HEAD. You can then just do a "soft reset" back to 0, which resets HEAD to that commit, but leaves your working tree and index (i.e. the staging area) intact. If you then do a commit, your index will be used to create a new commit with the same state as 6, but with 0 as a parent. i.e.



                       git status # Check that your status is clean
                      git reset --soft 0
                      git commit -m 'Work that is totally flawless'





                      share|improve this answer


























                        3












                        3








                        3







                        The most flexible way to do this is to do an interactive rebase to squash together or amend your commits. You need to be careful not to rewrite any history that's already public, however. For example, if you're working on the master branch, and you want to rewrite and linearize all your commits that aren't in origin/master (which typically means the work that you haven't pushed yet) you can do:



                        git rebase -i origin/master


                        That will launch a text editor where each commit you have that isn't in origin/master is shown, from oldest to newest. If you want want to combine any commit with the previous one, you can just change the start of the line from pick to squash. Or, if you want to amend that commit somehow, you can change pick to edit.



                        Using interactive rebase is the most flexible way of doing what you ask for, and it's a very useful tool to get to know, but in the exact situation you describe, it's probably overkill. First, check that the output of git status is clean, so that you're sure that your staging area represents the same state as HEAD. You can then just do a "soft reset" back to 0, which resets HEAD to that commit, but leaves your working tree and index (i.e. the staging area) intact. If you then do a commit, your index will be used to create a new commit with the same state as 6, but with 0 as a parent. i.e.



                         git status # Check that your status is clean
                        git reset --soft 0
                        git commit -m 'Work that is totally flawless'





                        share|improve this answer













                        The most flexible way to do this is to do an interactive rebase to squash together or amend your commits. You need to be careful not to rewrite any history that's already public, however. For example, if you're working on the master branch, and you want to rewrite and linearize all your commits that aren't in origin/master (which typically means the work that you haven't pushed yet) you can do:



                        git rebase -i origin/master


                        That will launch a text editor where each commit you have that isn't in origin/master is shown, from oldest to newest. If you want want to combine any commit with the previous one, you can just change the start of the line from pick to squash. Or, if you want to amend that commit somehow, you can change pick to edit.



                        Using interactive rebase is the most flexible way of doing what you ask for, and it's a very useful tool to get to know, but in the exact situation you describe, it's probably overkill. First, check that the output of git status is clean, so that you're sure that your staging area represents the same state as HEAD. You can then just do a "soft reset" back to 0, which resets HEAD to that commit, but leaves your working tree and index (i.e. the staging area) intact. If you then do a commit, your index will be used to create a new commit with the same state as 6, but with 0 as a parent. i.e.



                         git status # Check that your status is clean
                        git reset --soft 0
                        git commit -m 'Work that is totally flawless'






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Sep 7 '11 at 10:29









                        Mark LongairMark Longair

                        290k60354299




                        290k60354299























                            2














                            Yes, use git rebase -i, and squash the commits. Check this for more info and some examples.






                            share|improve this answer




























                              2














                              Yes, use git rebase -i, and squash the commits. Check this for more info and some examples.






                              share|improve this answer


























                                2












                                2








                                2







                                Yes, use git rebase -i, and squash the commits. Check this for more info and some examples.






                                share|improve this answer













                                Yes, use git rebase -i, and squash the commits. Check this for more info and some examples.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Sep 7 '11 at 10:22









                                GeoGeo

                                45.9k91287466




                                45.9k91287466























                                    2














                                    Assuming you haven't already pushed your commits to the remote then there are several ways to do this.



                                    One option is to use



                                    git rebase -i 


                                    which gives you the interactive rebase editor. You can then mark all the commits except the last as 'squash'






                                    share|improve this answer






























                                      2














                                      Assuming you haven't already pushed your commits to the remote then there are several ways to do this.



                                      One option is to use



                                      git rebase -i 


                                      which gives you the interactive rebase editor. You can then mark all the commits except the last as 'squash'






                                      share|improve this answer




























                                        2












                                        2








                                        2







                                        Assuming you haven't already pushed your commits to the remote then there are several ways to do this.



                                        One option is to use



                                        git rebase -i 


                                        which gives you the interactive rebase editor. You can then mark all the commits except the last as 'squash'






                                        share|improve this answer















                                        Assuming you haven't already pushed your commits to the remote then there are several ways to do this.



                                        One option is to use



                                        git rebase -i 


                                        which gives you the interactive rebase editor. You can then mark all the commits except the last as 'squash'







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Sep 7 '11 at 17:08

























                                        answered Sep 7 '11 at 10:27









                                        BenBtgBenBtg

                                        5161518




                                        5161518























                                            0














                                            Git squash merge is designed for this purpose. The way you would do this is to create a branch, make all your changes, and then merge back using squash merge.



                                            How to use git merge --squash?



                                            However, this isn't usually recommended because you will lose history this way.






                                            share|improve this answer




























                                              0














                                              Git squash merge is designed for this purpose. The way you would do this is to create a branch, make all your changes, and then merge back using squash merge.



                                              How to use git merge --squash?



                                              However, this isn't usually recommended because you will lose history this way.






                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                Git squash merge is designed for this purpose. The way you would do this is to create a branch, make all your changes, and then merge back using squash merge.



                                                How to use git merge --squash?



                                                However, this isn't usually recommended because you will lose history this way.






                                                share|improve this answer













                                                Git squash merge is designed for this purpose. The way you would do this is to create a branch, make all your changes, and then merge back using squash merge.



                                                How to use git merge --squash?



                                                However, this isn't usually recommended because you will lose history this way.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 21 '18 at 5:18









                                                Melbourne DeveloperMelbourne Developer

                                                1,7541235




                                                1,7541235






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7332260%2fare-there-any-git-command-to-combine-all-our-ugly-commits-together-to-one%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    MongoDB - Not Authorized To Execute Command

                                                    How to fix TextFormField cause rebuild widget in Flutter

                                                    Npm cannot find a required file even through it is in the searched directory