Git collaboration works by connecting your local repository to a remote one (like GitHub, GitLab, or Bitbucket), authenticating your identity, and then syncing commits with push and pull. You connect by adding a remote URL, authenticate via SSH keys or a personal access token, and sync using git push to upload and git pull to download changes.
What Is a Remote and Why Do You Need One?
A remote is a copy of your repository hosted on a server that everyone on your team can access. It acts as the central hub where commits are shared. Without a remote, Git is purely local—you can commit and branch, but you cannot collaborate with others. The most common remotes are hosted services like GitHub, GitLab, and Bitbucket, but you can also host your own with a bare repository on a server.
When you clone a repository, Git automatically sets up a remote named origin pointing to the URL you cloned from. If you started a project locally, you can add a remote manually.
How to Connect Your Local Repository to a Remote
Connecting is simply telling Git where the remote lives. The command is:
git remote add origin <remote-url>
Replace <remote-url> with the HTTPS or SSH URL of your remote repository. For example, on GitHub you would use something like https://github.com/username/repo.git (HTTPS) or git@github.com:username/repo.git (SSH).
To verify the remote is set up correctly:
git remote -v
This lists all remotes and their URLs. You should see origin with both fetch and push URLs.
If you cloned a repo, the remote is already configured, so you can skip this step.
How Authentication Works in Git
Authentication proves who you are to the remote server. Git supports two main methods:
- HTTPS with a personal access token (PAT) – When you push or pull over HTTPS, Git asks for your username and password. Modern services no longer accept account passwords; instead, you generate a PAT from your account settings and use it as the password. You can also cache the token so you don’t re-enter it every time.
- SSH keys – You generate a public/private key pair on your computer, add the public key to your Git service account, and Git uses the private key to authenticate. This is more secure and convenient once set up, as you don’t need to type credentials.
To set up SSH keys:
- Generate a key:
ssh-keygen -t ed25519 -C "your_email@example.com" - Add the public key to your Git service (usually under Settings → SSH keys).
- Test the connection:
ssh -T git@github.com(or your provider).
For HTTPS, you can use a credential helper to store the token. On Windows, Git Credential Manager is built in; on macOS, the keychain works; on Linux, you might use libsecret or a plain text file (less secure).
How to Sync: Push, Pull, and Fetch
Syncing is the process of updating your local repository with remote changes and vice versa. The core commands are:
git push– Uploads your local commits to the remote branch.git pull– Downloads remote changes and merges them into your current branch. It is a combination ofgit fetchandgit merge.git fetch– Downloads remote changes but does not merge them. You can review them before merging.
When you push for the first time on a new branch, you need to set the upstream:
git push -u origin branch-name
The -u flag links your local branch to the remote one, so subsequent pushes can just be git push.
When pulling, if there are conflicting changes, Git will ask you to resolve merge conflicts. To avoid surprises, it’s good practice to pull before pushing, especially if you’re working on a shared branch.
Common Workflows and Branching
Collaboration usually involves multiple branches. A typical workflow:
- Create a feature branch:
git checkout -b feature/awesome - Make commits on that branch.
- Push the branch to the remote:
git push -u origin feature/awesome - Open a pull request (PR) on the hosting service to merge into the main branch.
- After review and approval, merge the PR.
- Update your local main:
git checkout mainthengit pull.
This keeps the main branch stable and allows multiple people to work independently.
Resolving Merge Conflicts
Merge conflicts happen when two people change the same line in different ways. Git cannot decide which change to keep, so it marks the file as conflicted. You must manually edit the file to resolve the conflict, then commit the resolution.
To see which files have conflicts:
git status
Open the conflicted file; you’ll see markers like <<<<<<< HEAD and >>>>>>> branch-name. Edit the file to keep the correct content, remove the markers, then:
git add file.txt git commit
If you are not comfortable resolving manually, many editors and Git GUIs offer visual merge tools.
Choosing Between HTTPS and SSH
Both methods work, but they have trade-offs:
- HTTPS – Easier to set up behind firewalls and proxies; works with credential managers; uses a token that can be revoked. Good for beginners.
- SSH – More secure (key-based), no need to enter credentials each time, but requires key management. Good for frequent committers and those on trusted machines.
If you are behind a corporate firewall that blocks SSH port 22, HTTPS may be your only option. Conversely, if you want to avoid typing tokens repeatedly, SSH is more convenient.
Frequently Asked Questions
Why do I get “Permission denied (publickey)” when pushing?
This means your SSH key is not set up correctly. Check that your public key is added to your Git service account and that your private key is in the default location (~/.ssh/id_ed25519 or similar). Test with ssh -T git@github.com to see if the connection works.
How do I change the remote URL from HTTPS to SSH?
Use git remote set-url origin git@github.com:username/repo.git. Verify with git remote -v. This is useful if you originally cloned with HTTPS but want to switch to SSH.
What is the difference between git pull and git fetch?
git fetch downloads the remote changes but leaves your working directory untouched. git pull fetches and then merges the changes into your current branch. Use fetch when you want to inspect changes before integrating them.
How do I undo a push?
If you pushed a commit with mistakes, you can revert it with git revert <commit-hash> (creates a new commit that undoes the changes) or force-push after resetting locally, but force-push can overwrite teammates’ work—use with caution. Prefer revert for shared branches.
Why do I have to enter my username and password every time I push?
Your credential helper is not configured. Run git config --global credential.helper cache to cache for a few minutes, or use store to save permanently (less secure). On Windows, Git Credential Manager usually handles this automatically.
Can I collaborate without a hosting service?
Yes. You can set up a bare repository on a server and use SSH to access it. However, hosting services add features like pull requests, issue tracking, and access control, which simplify collaboration.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Valheim Server Not Showing in Community List Fixes
- DisplayPort vs HDMI: Which Cable Should You Use?
- Soundbar vs 2.1 Speaker System: Which Is Better?
- Surface Won’t Turn On: Quick Fixes1.8 h
- Survive Lava for Brainrot Tier List170 points
- MySQL Dump Import: Fix Common Errors