Local and Remote Repositories
Local and remote repositories, push and pull, how teams collaborate with branches and pull requests, and resolving merge conflicts.
Local and remote repositories
Every person working with Git has a complete copy of the repository on their own machine, called a local repository. To share work, the team uses a remote repository, usually hosted on a service such as GitHub or GitLab. You push your commits to the remote, and pull other people's commits from it.
Git is distributed: there's no single central copy that everyone must be connected to. Each clone has the full history, so you can commit, branch, and look through history offline. The remote is simply the copy the team agrees to treat as the shared, official one.
| Command | What it does |
|---|---|
git clone <url> | Copies a remote repository, with its full history, to your machine. You do this once. |
git push | Sends your new commits to the remote. |
git fetch | Downloads new commits from the remote without changing your files, so you can look before combining. |
git pull | Fetches new commits and merges them into your current branch, in one step. |
git remote -v | Lists the remotes this repository knows about. The default one is called origin. |
# get the team's repository
$ git clone https://github.com/acme/dba-scripts.git
$ cd dba-scripts
# later: get the latest changes, then share yours
$ git pull
$ git push
How teams collaborate
Most teams follow a simple workflow, often called GitHub flow or feature branching: main always holds reviewed, working code, and every change happens on its own branch and arrives through a pull request.
Here's Kofi adding an index script for the daily sales report:
# 1-2. start from the latest main, on a new branch named after the Jira item
$ git switch main
$ git pull
$ git switch -c DBA-142-sales-report-index
# 3. do the work and commit it
$ git add migrations/V7__add_order_index.sql
$ git commit -m "DBA-142 Add index on orders(order_date, region) for daily sales report"
# 4. push the branch, then open a pull request on GitHub or GitLab
$ git push -u origin DBA-142-sales-report-index
- Pull request (PR) or merge request (MR)
- A request to merge one branch into another, with a page showing the changes, a discussion, and the results of automated checks. GitHub and Bitbucket call it a pull request; GitLab calls it a merge request.
- Code review
- Teammates read the change, comment on specific lines, and approve or request changes. For a database change, reviewers check things like locking on large tables, the rollback script, and whether the index really helps the query.
- Automated checks
- The pipeline runs on every pull request, for example checking SQL style and applying the migration to a test database. A PR can only be merged when checks pass.
- Merge
- Once approved, the branch is merged into
main, often by a button on the pull request page, and the branch is deleted. Merging tomaincan then trigger deployment.
Merge conflicts
When two people change the same lines of the same file on different branches, Git can't decide which version to keep. That's a merge conflict. Git marks the conflicting section in the file, and a person decides what it should say:
-- roles.sql, after a conflicting merge
<<<<<<< HEAD
GRANT SELECT ON sales.orders TO sales_ro, finance_ro;
=======
GRANT SELECT, REFERENCES ON sales.orders TO sales_ro;
>>>>>>> ama-add-references
The part between <<<<<<< and ======= is your version; the part after it is the incoming one. Edit the file to the correct final result, here probably both changes combined, remove the markers, then git add and git commit. Editors such as VS Code show conflicts side by side with buttons to accept either version or both.
Key takeaways
- Every clone is a full local repository; the remote, usually called
origin, is the team's shared copy. git clonecopies a remote;git pushshares your commits;git fetchandgit pullget other people's.- Teams work on short-lived branches and merge them into
mainthrough pull requests (merge requests in GitLab). - Pull requests bring code review and automated checks before anything reaches
main. - Merge conflicts happen when two branches change the same lines; a person resolves them and commits the result.