upstream
: This is the repo you want to contribute to. Ex:github.com/konveyor/move2kube
origin
: This is your fork of theupstream
repo. Ex:github.com/myusername/move2kube
local
: This is a repo on your local machine (laptop/desktop). Usually this refers to the clone of your forkorigin
.
This only needs to be done once:
- Fork the repo on Github.
- Clone your fork:
git clone <my fork url>
- Add the
upstream
repo to the set of remote repos:git remote add upstream <upstream url>
- Check that you have both the
origin
andupstream
set correctly:git remote -v
When in doubt git log --graph --all
and scroll around with the arrow keys.
This shows you the graph of commits and the commit each branch is pointing to.
If there are a lot of commits you can also add --one-line
to make it easier to see.
Often our local
repo and our fork origin
will lag behind the main repo upstream
.
It is important to sync up with upstream
before we rebase and submit a pull request on Github.
- Get the latest code from upstream:
git fetch upstream
- Switch to the main branch:
git checkout main
- Fast forward your local main branch to catch up with upstream:
git merge --ff-only upstream/main
- Push the changes to your fork:
git push
ALWAYS BRANCH OUT OF MAIN
- Follow the steps to sync up with
upstream
. - Switch to the main branch:
git checkout main
- Create a new branch and check it out:
git checkout -b my-feature-or-bug-fix-branch
- Make some changes.
- Add all changes to the staging area before committing:
git add -A
- Commit and sign off on the commit:
git commit -s -m 'My commit message'
- Push the new commits to your fork:
git push
- Repeat steps 3 to 7 until you are ready to submit a pull request.
- Make sure the code passes build and test:
make ci
- Make sure all the changes are committed and the working tree is clean:
git status
- Follow the steps to sync up with
upstream
. git checkout my-feature-or-bug-fix-branch
- Rebase onto the
upstream/main
branch:git rebase upstream/main
. Fix any merge conflicts that occur. - Make sure the rebased code passes build and test:
make ci
- After a successful rebase push the changes to your fork:
git push --force
- Submit a pull request on Github between your branch
my-feature-or-bug-fix-branch
on your forkorigin
and themain
branch onupstream
.
You can change the commit message of the current commit using: git commit --amend -m 'my new commit message'
--amend
can also be used to make code changes:
- Make some changes.
git add -A
git commit --amend
- If you want those changes to show up on your fork:
git push --force
As we keep creating new branches for each pull request, eventually you can end up with a lot of old branches.
This doesn't affect anything other than visual clutter when doing git log --graph --all
.
You may also want to reuse an old branch name such as bugfix
.
- Checkout a branch you aren't going to delete:
git checkout main
- Delete the old branch locally:
git branch -d oldbranch
- Delete it on the fork:
git push -d origin oldbranch
git
opens the default text editor on your system when it needs you to edit commit messages, rebase interactively, etc.
By default this opens vi
. You can/should change it to something you are more familiar with 1.6 Getting Started - First-Time Git Setup
- Commits are immutable. Yes, even commands like
git commit --amend
simply create new commits. - Commits are NOT diffs. Commits are snapshots of the entire repo.
- A
patch
is a diff between 2 commits. - A
branch
is a pointer/reference to a commit.
Branches are also referred to as heads (not to be confused with the special valueHEAD
). - The special value
HEAD
is a pointer to a branch. HEAD
can also point to a commit. Ex: if you dogit checkout <a particular commit hash>
In this situationgit status
will tell you that you are in a detachedHEAD
state. Dogit checkout somebranch
to reattach theHEAD
.- Each commit points to its parent commit. This connects them forming a directed acyclic graph.
git
sees the graph through the branches.
A branch points to a commit, the commit points to its parents, the parent points to its grandparents, etc.
This way every commit gets referenced. Any commits that are not referenced in this way are invisible togit
.
They will eventually be garbage collected.local
,origin
, andupstream
are independent repositories.
Likewisesomebranch
,origin/somebranch
andupstream/somebranch
are also independent.
It is your reponsibility to keep them in sync.
Videos:
- Dives into
git
internals to give a better understanding Lecture 6: Version Control (git) (2020) - Only 7 minutes and straight to the point. Perhaps a bit overwhelming Git Internals - Git Objects
- Long but goes into much more depth, including a look at the actual files and folders inside
.git
Deep Dive into Git - Edward Thomson
Books:
- The official git book https://git-scm.com/book/en/v2
Interactive guide for fixing mistakes: