Search code examples
gitgit-pushgit-commit

Git - exclude specific commit and push


How to exclude specific commit from series of commits. I mean if I have 5 commits, and I only want to push 4 commits. How to do this. Please help to resolve this.


Solution

  • You will need to have a new branch with the desired commits.

    You can do it in several ways


    git cherry-pick

    Checkout a new branch to the specific sha-1 you want to start from:

    git checkout <origin branch>
    git checkout -b <new branch>
    
    # now cherry-pick the desired commits onto the new branch
    git cherry-pick commit1 commit2 ... commitN
    
    # now push to remote
    git push origin remote <branch name>
    

    Other options:

    git revert

    git revert SHA-1
    

    Use git revert to undo the changes you have made in the unwanted commit, the result will be branch with the old code and the new code but the current state will be the original code


    git rebase -i

    Interactive rebase. choose the commit you don't want and remove it.

    # X is the number of commits you wish to squash
    git rebase -i HEAD~X
    

    Once you squash your commits - choose the e for edit and place the code you want it to be, add and commit

    enter image description here


    git filter-branch

    Filter branch can be used to filter any content you want.

    git filter-branch --env-filter '<do what ever you want on this commit range' SHA1..SHA1