Github - make maste branch up to date with my local files?

Viewed 32

I'm a github beginner so don't judge :). I've a github project with a few others with only the main branch. I cloned the repository and made many changes. While I did these changes, someone else pushed their changes on github. They pushed one file and some minor changes in the main file.

What is the best way to push my changes to github without losing any (or the least amount) of code?

2 Answers

You should rebase your local commits on top of the (now updated) remote main branch, using git rebase.

cd /path/to/my/local/cloned/repo
git fetch
git rebase origin/main
git push

That way, you are:

  • resolving any potential merge conflict locally (during the rebase, which replays your commits on top of origin/main)
  • are pushing only new commit on top of the most recent version of origin/main

Even if there is only a master branch now, in your terminal, you can make a sub-branch with:

git checkout -b 'subBranchName'

Then push that branch into GitHub:

git add .
git push --set-upstream origin subBranchName

Go to that repository in GitHub and click on branches. There will most likely just 2: the default master and the new one you created. Click on "New Pull Request", then you can compare the changes there. I prefer this method, because you can share the info with your team and have them approve or reject it. You can also take notes and make more edits all within GitHub.

VonC's answer with fetch and rebase will be a lot quicker and easier, but adding a branch and merging it will also teach you a lot about GitHub. As a beginner myself, I highly recommend branching to easily discuss with your team, find mistakes, and backtrack if necessary.

Another tip is hit

Ctrl `

while in VS to pull up the terminal.

Make a goal to push to GitHub daily, even if you push a messed up branch that you plan on deleting without merging. You'll get used to in in about 2~3 weeks.

Related