Guapo
Guapo

Reputation: 3482

Update modified git repo?

I have a repository on git, which I am cloning from into a server that compiles and runs it.

Since that server is local on my network, I can further clone the project that is accessible via samba or even open it on my preferable editor and work on it from there, doing the changes I need or anything else.

Here is where things get complicated, I would like to be able to keep the clone I have on my server, synchronized(up to date) with the git repository(which I can't push things to), while retaining the changes I do locally to my server repository.

What is the most common practice if there is one for this specific case or what should be the advised route to follow for this?

If I had to describe the current process I do, it would look something like this:

  1. git reset --hard origin/master
  2. git fetch origin
  3. redo all changes
  4. repeat when new stuff on the origin...

I assume, I would in either case need steps 1 and 2 because if I commit git would complain about changed files not allowing to simple git pull it.

An alternative to the above, I think would be something like:

  1. generate patches
  2. git reset --hard origin/master
  3. git fetch origin
  4. apply patches (could fail here depending on how the files changes, so manually would be easier to identify...)
  5. repeat when new stuff on the origin...

So repeating my question again:

I am not an expert in git, I do know how to use part of it, but the above scenario is something new for me.

Upvotes: 0

Views: 412

Answers (2)

Code-Apprentice
Code-Apprentice

Reputation: 83577

First you should follow the instructions from the GitHub help to create a fork. After you do this, you will have a local repo and a personal clone (or fork) on the GitHub servers. The local repo has two "remotes". origin points to your personal GitHub repo. upstream points to the original main repo.

Now when you want to make your own changes, you should create a branch:

$ git checkout -b mybranch master

This command will create the branch and check it out. Now you can use git add and git commit to commit your changes to your local repo.

If you decide you want to submit your changes to the original repo, then you first need to push your branch to your personal GitHub repo:

$ git push origin mybranch

Then log in to your GitHub account and create a Pull Request. Your changes will be reviewed by the maintainers of the original repo. Most likely they will give suggestions of what you need to do to improve your changes before being accepted.

When the original repo is updated, you can pull those changes to your local repo with

$ git checkout master
$ git pull upstream master

You can also update your personal GitHub repo with

$ git push origin master

And you can merge the changes into your own work with

$ git checkout mybranch
$ git merge master

If there are any conflicting changes between the update to the original repo and your own work, then git will tell you. Then you have to manually resolve these conflicts. You can do this directly in any text editor or use one of the many visual tools which are available. See How to resolve merge conflicts in Git? for details.

Upvotes: 1

toniedzwiedz
toniedzwiedz

Reputation: 18563

Why don't you use a branch?

The moment you cloned the repository, you got a local one. You can create branches and use them to version your code locally, without ever pushing to the remote.

Create your own branch based on master

git checkout master
git checkout -b mybranch

Keep commiting your changes to mybranch. Whenever changes appear in the remote repository, update master and merge it into mybranch.

You could also share your work with others if you set up your own remote. Git allows you to work with multiple remote repositories. If you don't have access to the one you used to clone the project, nothing stops you from creating your own one where you do.

Upvotes: 2

Related Questions