Home > OS >  How can I copy in my local branch everything from main branch in github?
How can I copy in my local branch everything from main branch in github?

Time:06-23

I'm currently working on a project. I did my first commit and then other people changed a lot from the beginning. When I go into iTerm i'm on the main branch and can see everything from the folder. Then I want to change something and go to my personal branch, but some files are deleting from the local folder.

How can I update my local folder in my personal branch with everything that is already on Git ?

CodePudding user response:

If you need to sync your main branch with another. You can do

git checkout <main-branch>
git pull
git checkout <local-branch>
git merge <main-branch>

Keep in mind that if you have conflict, you must resolve them manually

CodePudding user response:

I'd recommend rebasing your branch onto the main branch instead of doing a merge. Rebasing it allows you to basically move your branch to any point in the history, and keeps the option open for you to move it again in the future (if, for example, other people keep making changes to the main branch). Merging essentially ties your branch to a specific timeline: it gets you all the code changes, but it eliminates your options in the future.

Make a backup of your branch before trying any of this.

There are two ways to do this:

  • the easy way

    Check out your branch, and then run git rebase main. If your situation is not complicated, this will work automatically.

  • the precise way

    If the easy way gives you bad results, you may need to explicitly tell git where your branch begins, and where to move it to:

    git rebase --onto NEW_BASE OLD_BASE YOUR_BRANCH_NAME

    Where:

    • NEW_BASE is the name of the other branch that you wish to put your branch on top of; that would probably be main
    • OLD_BASE is the commit-hash of the latest commit in your branch's current history that is not part of your branch; this is essentially the place where you were when you originally cut your branch
    • YOUR_BRANCH_NAME is the name of your branch, the branch that is being moved
  • Related