Connecting the local repository to the remote repository– Source Code Management with Git and GitOps

You will need to add a remote entry using the following command to connect with the remote repository from the local repository:

$ git remote add origin [email protected]:<your-github-username>/first-git-repo.git

You can also find these details on the Quick Setup page of your GitHub repository.

Now that we’ve set up the connection, let’s look at how we can push our changes to the remote repository.

Pushing changes from the local repository to the remote repository

To push the changes from the local repository to the remote repository, use the following command:

$ git push -u origin master

Enumerating objects: 6, done.

Counting objects: 100% (6/6), done.

Delta compression using up to 12 threads

Compressing objects: 100% (2/2), done.

Writing objects: 100% (6/6), 474 bytes | 474.00 KiB/s, done.

Total 6 (delta 0), reused 0 (delta 0), pack-reused 0

To github.com: <your-github-username>/first-git-repo.git

* [new branch]          master -> master

Branch ‘master’ set up to track remote branch ‘master’ from ‘origin’.

Now, refresh the page on your remote repository. You should see that the code was synced, as shown in the following screenshot:

Figure 2.2 – Code synced in the remote repository

You can also use the inline editor to make further changes to the file using the GitHub web portal. While this is not recommended, we’ll do this to simulate a situation where another developer changed the same file you were working on.

Click on file1 and then click on the pencil icon to edit the file, as shown in the following screenshot:

Figure 2.3 – Editing the file in the remote repository

Upon doing this, an editing window will open where you can make changes to the file. Let’s add This is third line within the file, as shown in the following screenshot:

Figure 2.4 – Adding a new line

Scroll down – you should see a Commit changes section, where you can add a commit message field and click on the Commit button, as shown in the following screenshot:

Figure 2.5 – The Commit changes section

Once you’ve clicked on that button, you should see the third line, as shown in the following screenshot:

Figure 2.6 – Changes committed remotely

At this point, changes have been made to the remote repository, but you have also been working on your changes. To simulate this, let’s make a change in the local repository as well using the following commands:

$ echo “This is fourth line” >> file1

$ cat file1

This is first line

This is second line

This is fourth line

$ git add file1

$ git commit -m “Added fourth line”

[master e411e91] Added fourth line

1 file changed, 1 insertion(+)

Now that we’ve committed the changes in our local repository, let’s try to push the code to the remote repository using the following commands:

$ git push

To github.com:<your-github-username>/first-git-repo.git

! [rejected]              master -> master (fetch first)

error: failed to push some refs to ‘github.com:<your-github-username>/first-git-repo.git’ hint: Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first integrate the remote changes.

hint: (e.g., ‘git pull …’) before pushing again.

hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.

Wait, what happened? Well, the remote repository rejected the changes as we tried to push changes while someone else made some commits in the remote repository, and our changes are not current. We would need to pull the changes in our local repository first to apply our changes on top of the existing ones in the remote repository. We’ll look at this in the next section.

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like these