How to work with two devises and one repository

Hello,

I am novice with Github. So I am using it since a moment, but it first time, I upload my code to my own repository that I created.

On a Rasberry I created a folder

mkdir /opt/project/

I have created a pythion script with a class.

First time I use it, I was helped with this

sudo git init
sudo git add script-py3.py
sudo git commit -m "first commit"
sudo git config --global user.email "email@email"
sudo git config --global user.name "username"
sudo git commit -m "first commit"
sudo git remote add origin https://github.com/username/projet-v3.git
sudo git push -u origin master

When I have done with a lot of code or when I wanted to upload new changes, I have used to do it with

sudo git add script-py3.py
sudo git commit -m "update script-py3.py"
sudo git push -u origin master

and my changes was uploaded at github.

My worries now, I have a new Raspberry and I would like to download the same script. To do it, it’s easy, but I am going to add new line of code and then pusing the change to the same repository, whith the command I know.

Later, I would like to update the script on my first Raspberry, but what will happen in my repository if I push again from my first Raspberry? Do the repository will be overwritten by my old script from my first Pi?

The idea is both Raspberry will download the lastest script version from the repository.

Then from my second Raspberry, after downloading the repository, shoud I

sudo git init
sudo git add script-py3.py
sudo git commit -m "first commit"
sudo git config --global user.email "email@email"
sudo git config --global user.name "username"
sudo git remote add origin https://github.com/username/projet-v3.git
sudo git push -u origin master

and then after new changes

sudo git add script-py3.py
sudo git commit -m "update script-py3.py"
sudo git push -u origin master

But then from my first, how can I update the change from the depository to the Raspberry?

And if I add changes from the first Pi and I push, how to update the second without overwrite the latest version of the repository from a local version?

I am starting to install my second Raspberry but I am afraid of a wrong git manipulation :slight_smile:

Many thanks

Hi pierrot10! It looks like you’ve got the hang of making changes to your code, committing it, and pushing the code up to the GitHub remote!

The missing piece of the puzzle is git pull!

Each time you start working with either one of the Raspberry Pis, you can run git pull to update your local copy of the repository with any changes the GitHub version might have had pushed to it since the last time you worked on that Pi.

This help page has more details:

https://help.github.com/en/github/using-git/getting-changes-from-a-remote-repository

Hello
Thanks for your reply. So do you mean, I should do that?

sudo git pull -u origin master

That’s correct? But if I want to pull just on file, I supposed I can do as the following?

sudo git pull https://github.com/user/repo/README.md

Hi there!

The -u flag is short for --upstream - it tells Git “remember this relationship for future use”, so when you have used it once (usually on your first push) you don’t need to use it again.

git pull origin master is the most common workflow, and it will get all changes to the master branch and merge them in to your local copy. Mostly people prefer to get all changes from origin at the same time.

git pull is actually a combination of two commands, fetch and merge, that you can run separately.

git fetch origin master will find out about all available changes to the master branch.

git checkout FETCH_HEAD -- <specific file>will grab only the specified file.

You’ll then need to commit that file separately.

As I mentioned though, it’s much more usual to just grab all changes, so git pull does all of that in one command.