Reputation:
I cloned a specific repo on Github to my local computer.
I created a new repository on my own github using git clone <repo url>
and then made changes on my copied repo and uploaded that to my new repo via git push and commit.
Then the owner of the repo added new files. If I cloned that specific repo it might overwrite my local copy?
How can I update my local file without affecting the changes I've made so far? What are the commands I can use?
Upvotes: 1
Views: 195
Reputation: 7063
If you clone a project and the URL didn't change just test
git remote show origin
and you can connect. If origin
is unknown, you must git remote add origin
; if the origin changed you can use git remote add origin_new
. Afterwards you just proceed
git branch -av # list all existing branches
git fetch origin # update all refs
git checkout -b localbranch origin/remotebranch # create new branch (-b) and checkout.
Then you can work locally using merge - as you you are used to.
If you want to check the impact of changes, have a look at git merge --no-ff --no-commit
Upvotes: 0
Reputation: 2897
You can incorporate new changes from the original repository by adding it as an additional remote to your repository:
git remote add upstream <url-of-original-repo>
That will create a new remote called upstream
. Then fetch from the new remote:
git fetch upstream
That will add new remote tracking branches prefixed with the name of the new remote (e.g. upstream/master
). Incorporate changes from the original repository by merging one of the new remote tracking branches into your current branch:
git merge upstream/master
You won’t lose any of your changes. You will be required to resolve any conflicts that may exist between your changes and the original repository.
Once you’re confident how fetch and merge from an alternate remote work, you can combine them into one equivalent pull command:
git pull upstream master
Upvotes: 0
Reputation: 551
My git workflow is as follows: from the main branch (in my case it's usually 'development' or something):
git branch -b 'NameOfFeatureImAdding
git checkout NameOfFeatureImAdding'
This creates a new branch and switches to it, so all of your changes are recorded in that branch.
When there's new code in development to pull down, I switch over to the branch, pull in the code, then if it's small (as it should be) I merge it into my feature branch.
git checkout development
git pull
git checkout NameOfFeatureImAdding
git merge development
That way, my feature branch keeps up with development, making the future merge easier.
GitHub has a handy guide: https://guides.github.com/introduction/flow/
Upvotes: 1