3. Working with Remote Repositories

So far we have worked locally. It's time to connect our work with remote repositories and collaborate with others.

What is GitHub?

GitHub is a hosting platform for Git repositories. Alternatives include GitLab and Bitbucket. We'll use it for its popularity and because it's also free.

Having remote hosting, or a Git Server, lets us synchronize our work with other developers. It's not essential but it simplifies collaboration. Otherwise, we would have to share our patches, or changes, manually by email or other means (git format-patch and git am).

In addition, GitHub adds very useful features on top of Git:

  • Web interface to visualize code
  • Pull Requests (code reviews)
  • Issues (bug and task tracking)
  • Actions (automation)
  • Wikis and documentation

And much more.

Create a GitHub account

  1. Go to github.com
  2. Click "Sign up"
  3. Follow the instructions

Tips:

  • Choose a professional username (future bosses, coworkers, etc. will see it)
  • Use an email you check regularly
  • Enable two-factor authentication (2FA)

Create a repository on GitHub

  1. Click the "+" button in the top right corner
  2. Select "New repository"
  3. Choose a descriptive name
  4. Add a description (optional but recommended)
  5. Choose whether it will be public or private
  6. Do NOT initialize with a README if you already have a local repo
  7. Click "Create repository"

git remote

A "remote" is a reference to a repository elsewhere (usually on GitHub). The most common remote is called origin.

# View the configured remotes
git remote -v

# Add a remote
git remote add origin https://github.com/user/repo.git

# Change the URL of a remote
git remote set-url origin https://github.com/user/new-repo.git

# Remove a remote
git remote remove origin

git clone

Cloning is copying an entire repository from a remote location to your local machine.

# Clone a repository
git clone https://github.com/user/repo.git

# Clone into a specific directory
git clone https://github.com/user/repo.git my-folder

# Clone only the latest version (faster)
git clone --depth 1 https://github.com/user/repo.git

git push

Sends your local commits to the remote repository.

# Push to the current branch
git push

# First time (sets up tracking)
git push -u origin main

# Push to a specific branch
git push origin branch-name

# Push all branches
git push --all

# Force push (be careful!)
git push --force

Complete example:

# In your local repo
echo "New content" > file.txt
git add file.txt
git commit -m "Add new file"

# Send to the remote
git push origin main

git pull

Downloads changes from the remote repository and merges them with your local branch.

# Pull the current branch
git pull

# Pull a specific branch
git pull origin main

git fetch vs git pull

This is an important distinction:

  • git fetch: Downloads changes but does NOT merge them
  • git pull: Downloads changes AND merges them (equivalent to fetch + merge)
# Fetch: only download
git fetch origin

# See what changes exist without merging
git log origin/main

# Decide to merge
git merge origin/main

When to use each one: - fetch: When you want to see the changes before merging them - pull: When you trust the changes and want to update quickly

SSH vs HTTPS: authentication

There are two ways to authenticate with GitHub:

HTTPS

Simpler to set up, but you need to enter your username and password (or token) every time you push.

git clone https://github.com/user/repo.git

SSH

More secure and convenient once configured.

Configure SSH:

# 1. Generate an SSH key
ssh-keygen -t ed25519 -C "you@email.com"

# 2. Add the key to the SSH agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

# 3. Copy the public key
cat ~/.ssh/id_ed25519.pub
  1. Go to GitHub → Settings → SSH and GPG keys → New SSH key
  2. Paste your public key

Clone with SSH:

git clone git@github.com:user/repo.git

Use SSH if you work frequently with Git.

Basic workflow

A typical workflow with remotes:

# 1. Clone the repository
git clone git@github.com:user/repo.git
cd repo

# 2. Make changes
echo "Change" >> README.md
git add README.md
git commit -m "Update README"

# 3. Before pushing, get changes
git pull

# 4. Resolve conflicts if any

# 5. Send changes
git push
Activity 1
  1. Create a new public repository called learning-git.
  2. Clone the repository to your local machine.
  3. Create a README.md file with a brief description.
  4. Make a commit with the message "Add initial README".
  5. Use git pull to bring changes to your local machine (there are none, but it's good practice).
  6. Run git push to upload your changes to the remote repository.
  7. Review the commit history with git log.
Activity 2
  1. Clone a public repository of your choice (for example, https://github.com/torvalds/linux).
  2. Explore the history with git log.

This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.

Desafíos de programación atemporales y multiparadigmáticos

Desafíos de programación atemporales y multiparadigmáticos

Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.

Buy the book

Will you buy me a coffee?

This is how I keep writing without ads or paywalls.

Comments

There are no comments yet.