Search code examples
gitgithuboverwritesmartgit

overwrite other branch(github) in Smartgit


I am new on smartgit-ubuntu and I have a problem. I started to use smartgit for gui. I have a project on github and I have 2 branches which are master and new branch. I cloned project on smartgit and I changed code in new branch. Also, I pushed code to new branch and code is on github-new branch. I did mistake and I want to overwrite new branch with master code. In sum, I want to change new branch code with master code and I want to push it to github-new branch. How can I do? Thanks advance.


Solution

  • You can reset your local new branch to master, then force push it.
    An example of reset with SmartGit is seen here.

    Even without smartGit, you can do that in command-line:

    cd /path/to/my/repo
    git checkout master
    git pull
    git checkout newBranch
    git reset --hard master
    git push --force -u origin newBranch
    

    However, as the OP comments:

    This reset deletes github commits and copy master to other branch.
    Namely I want to commit all master code to other branch in github and I don't want to delete past commits

    In that case, you can:

    That is:

    git checkout newBranch
    git reflog show
    git reset --hard <oldSHA1 of newBranch>
    

    (note the final -- .: "dash-dash-space-dot" at the end of the second checkout)

    • add, commit and push: you will add, commit and push files from master in newBranch, while keeping the history of newBranch.
      If you had already pushed newBranch before, you will need to force the push

      git push --force