Skip to main content

Git Basic Commands II – Connection with GitHub

By Ottobre 7, 2023Marzo 5th, 2024No Comments

Previously, we navigated through some basic Git commands which you can revisit here. This time, let’s delve deeper and explore how to efficiently create the connection between our local Git repositories and GitHub, ensuring a seamless flow in our development process.

1. Cloning a Repository

Cloning can be likened to creating a personal copy of a project. When you clone a repository from GitHub, not only do you download the project files, but you also preserve the vital version control history.

# Clone a Repository
git clone <repository_url>

Cloning allows you to work on projects and contribute to open-source while maintaining a link to the original repository, ensuring that you can keep abreast of developments seamlessly.

2. Adding a Remote Repository

Connecting your local repository to GitHub requires you to define a remote repository, a pointer to a different repository location. This connection enables you to synchronize your work with other team members.

# Add a remote repository
git remote add origin <repository_url>

The keyword origin is an alias, representing the URL of the remote repository, which simplifies pushing and pulling changes.

3. Linking Local Branch to Remote Branch

Once you have a remote repository defined (e.g., after cloning a project or adding a remote), you might want to map your local branches to remote branches. This connection enables you to easily push and pull changes without specifying the remote branch each time. Typically, this linking is automatic when you clone a repository or push a new branch. However, in cases where automatic linking did not happen or was disrupted, you may want to manually set it up.

# Link local branch to a remote branch
git branch --set-upstream-to=origin/<remote_branch_name> <local_branch_name>

If you are pushing a new local branch and want to establish a tracking connection:

# Push and link the local branch to a remote branch
git push -u origin <branch_name>

The -u flag sets up the tracking between local and remote branches, making future pushes and pulls for the branch smoother. The local branch will track the remote branch, simplifying the push and pull commands since Git knows where to push to and pull from.

Now, with the local branch linked to its remote counterpart, your development work is streamlined as you won’t need to specify the remote and branch names in subsequent push and pull operations for the linked branches. It ensures an organized and synchronized working routine, crucial for collaborative projects.

4. Fetching and Pulling changes from GitHub

Keeping your local repository updated with the changes in the remote repository is key to avoiding conflicts and ensuring you’re working with the latest codebase. Both fetching and pulling help you achieve this but in slightly different ways.

Fetching Changes

Fetching allows you to view the changes made in the remote repository without altering your local working directory. It brings the remote branches and commits to your local repository, which can be particularly useful to check what’s been done remotely without mixing it with your local changes.

# Fetch changes from the remote repository
git fetch origin

Pulling Changes

In contrast, pulling fetches the changes from the remote repository and merges them into your local branch, ensuring that your current working branch is up to date with the remote.

# Pull changes and merge them into the current local branch
git pull origin <branch_name>

A git pull is essentially a combination of git fetch and git merge, and it’s crucial to be cautious when pulling changes, especially if you have local modifications.

Tip : If you wish to update all branches in your local repository, consider fetching all changes first and then merging the relevant branches as needed. This way, you maintain a high degree of control over the branches and their respective updates.

Why Fetch and Pull Matter

Understanding when to fetch and when to pull is crucial for efficient collaborative development:

  • Use fetch when you want to review changes before merging them into your branch.
  • Use pull when you want to synchronize your branch with the remote immediately, such as after ensuring no conflicting changes through fetch.

Navigating through fetch and pull while working with remote repositories ensures that you stay in tune with the collective progress of your team, while also safeguarding your local developments from unplanned overwrites.

5. Pushing Changes and Handling Merge Conflicts

Sharing your changes and integrating work from different contributors is a pivotal part of collaborative development. Let’s dive into how to push your local changes to GitHub and discuss the essence of merging in version control.

Pushing Changes

Once you’ve committed your changes in your local repository, the next step is to share them with your team by pushing them to the remote repository on GitHub.

# Push changes to the remote repository
git push origin <branch_name>

When you push your changes, the remote branch in GitHub gets updated with your latest commit. This makes your alterations accessible to your team members and safeguards your modifications in the remote repository.

Merging Changes

Merging is a key operation in Git to combine the changes from one branch into another. This can happen locally or in a remote repository like GitHub. It’s a way of integrating different lines of development and is commonly used to combine feature branches into the main code base

Local Merging

When you’ve fetched changes from a remote branch, or developed in a different branch locally, you’ll want to merge these changes into your working branch (commonly main or master)

# Merge changes from <branch_name> into the current branch
git merge <branch_name>

For instance, if you’re on the main branch and want to merge changes from a feature branch, you’d switch to main and run git merge feature.

Handling Merge Conflicts

During a merge, conflicts may arise, especially if the branches being merged have alterations in the same parts of a file. Git will mark the file as conflicted, and you’ll need to resolve it before completing the merge.

Merge conflict example in VSCode

Resolve the conflict by editing the conflicted file to manage how the changes should be merged, and then mark the file as resolved by adding and committing it.

Merging on GitHub

GitHub also allows for merging through Pull Requests (PRs), providing a platform for code review and discussion before integrating changes. After pushing your branch to GitHub:

  1. Open a Pull Request from your branch to the target branch.
  2. Review the changes and discuss with the team.
  3. Resolve any conflicts if they arise.
  4. Merge the Pull Request, thus applying your changes to the target branch.

Ensuring that your contributions are effectively merged, either locally or remotely, and that any resulting conflicts are adeptly resolved, maintains the integrity of the codebase amidst collaborative efforts. It enables seamless incorporation of diversified developments, thereby realizing the collective objective of the project.


Establishing and managing the connection between local and remote repositories is pivotal in collaborative coding environments. This deeper dive into connecting Git with GitHub equips you with a more detailed roadmap to facilitate your ventures in the world of collaborative coding.

For a recap on Git’s basic commands, feel free to revisit our first blog post. Stick around for more insights and exploration into the expansive universe of Git and GitHub in our upcoming posts. Happy coding!


Leave a Reply