A git repository is a way of storing a directory of files and track the changes to the files (so that we can go back or see what has happened later in simple or complicated ways). Information that git uses is stored in the .git folder, organised in a way that git understands.
Changes
Once we’re happy with the changes to the files in our directory, we Commit the changes for tracking. Sort of like a checkpoint.
Branches
A Branch can be thought of like a deviation in the list of changes, or a way of grouping sets of changes together, as part of a particular task. Branches have a name, and are usually is intended to be merged into the master branch of changes later.
Git remembers the current branch you are working on.
Since a branch can be made anywhere along the list of changes and can also have it’s own branches the repo structure looks like a tree.
When we Commit, we track those changes with the current branch.
To change the current branch (and change your local files to have the latest changes of the branch) we Checkout that branch.
Remote Repository
While the local repository tracks changes you have made, the remote repo is a copy of it, that others also interact with.
Git remembers the current remote repo.
Typically the source repo is stored using the name origin.
To copy our repos local changes to the remote repo we Push the current branch to the remote repo. To bring changes from the remote to ours, we Pull.
Standard procedure is:
Pull the branch from the remote repo that you want to use as a base
-> Make a branch based on that branch (your task) -> Switch your local files to that branch (checkout)
-> Make changes to your files
-> Update your local repo (commit)
-> push the changes to the same named branch on the remote repo (push)
-> request for your branch in the remote to be merged into the main branch (will take the latest branch when resolving)
- merge
Branch 1 + Branch 2 -> Branch 1 (with changes from 2)
(if you are not the owner request the merge and the owner of the repo handles it) - branch Create a new local branch
- clone
This is the starting point, it takes the whole repo and makes another repo locally - fork
made popular by github, makes a remote copy of a remote repo,
pushing to the orignal repo doesn’t happen, instead they use pull requests
(esentially it’s another layer of control, this time on the entire repo itself)
Cherry Picking
https://www.atlassian.com/git/tutorials/cherry-pick
This lets you take a single commit and add it to a branch. Handy if you started on the wrong branch.
Rebase
Rebasing is useful for backfilling commits to catch up to the HEAD, if other people have made commits in the meantime. Unlike merge, rebase sort of rewrites history.