The Distributed Model
One of Git’s defining characteristics is that it is a Distributed Version Control System (DVCS). Unlike Centralized VCS (like Subversion or CVS) where clients checkout the latest version of files, in Git, every client mirrors the repository fully.
This means that if the server dies, any of the client repositories can be sent up to the server to restore it. Every clone is really a full backup of all the data.
Managing Remotes
To collaborate with others, you use “remotes”. A remote is simply a reference to a version of your project that is hosted on the internet (e.g., GitHub, GitLab) or network.
Viewing Remotes
To see which remotes you have configured:
git remote -v
This will list the shortnames (like origin) and the URLs that Git has stored for reading and writing.
Adding a Remote
If you initialized a repository locally, you need to add a remote explicitly to push your code.
git remote add origin https://github.com/user/repo.git
Here, origin is just the standard default name for your primary remote, but you could name it anything (e.g., upstream, backup, production).
Cloning a Repository
If you are starting a project that already exists remotely, you clone it.
git clone https://github.com/liberuniversity/course-content.git
This command does several things:
- It initializes a new
.gitdirectory. - It pulls down all the data for that repository.
- It checks out a working copy of the latest version of the default branch.
- It automatically sets up your local
mainbranch to track the remoteorigin/mainbranch.
Synchronizing Changes
Collaboration involves sending your commits to a remote and downloading commits from others.
Fetching vs. Pulling
There is a crucial distinction between fetch and pull that often confuses beginners.
git fetch downloads the data from the remote project that you don’t have yet. It updates your “remote-tracking branches” (like origin/main), but it does not merge changes into your working directory. It is safe and non-destructive.
git fetch origin
git pull is essentially a git fetch followed immediately by a git merge. It downloads the changes and tries to merge them into your current branch.
git pull origin main
Recommendation: If you want to see what others have done before integrating it, use fetch then log (or diff), and finally merge. If you just want to get up to date and resolve conflicts later, use pull.
Pushing Changes
When you have commits that you want to share, you must push them upstream.
git push origin main
This command takes your local main branch and pushes it to the origin remote. This only works if you have write access and if no one else has pushed in the meantime. If someone else has pushed, you must pull their changes and merge them before you can push.
Setting Upstream
The first time you push a new branch, you often need to set the “upstream” (or tracking) relationship. This links your local branch to the remote branch so you can use git pull and git push without arguments later.
git push -u origin feature-login
The -u (or --set-upstream) flag establishes this link.
Remote Tracking Branches
Remote-tracking branches are references to the state of remote branches. They are local and you cannot move them; Git moves them for you whenever you do network communication. They act as bookmarks.
They take the form <remote>/<branch>. For example, origin/main.
If you are on your local main branch and you do git fetch origin, Git updates origin/main to reflect the latest commit on the server. Your local main pointer stays where it was.
Deleting Remote Branches
Once a feature is merged and the branch is deleted on the server, you might still have the reference locally. To clean up your local references:
git fetch --prune
To explicitly delete a branch on the remote server:
git push origin --delete feature-login
Which command downloads changes from the remote but does NOT modify your working directory?
Adding a Remote
# Add a remote named 'upstream' with url 'https://git.example.com/repo.git' git remote upstream https://git.example.com/repo.git