简体   繁体   中英

Cloning Updates on Git and Github

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?

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/

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 (eg 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM