I learn git from this interactive tutorial. I take note along the way while I was doing it.
Commit to repo
We was on C1 commit
git branch <name>
git checkout <name>
create a new branch and check it out
It can be shorthand:
git checkout -b <name>
Now we are working on main branch
We are going to merge the branch
git merge bugFix
Note that the branch we are on, will ‘change the base’ to another branch.
Request: Move work directly from bugFix onto
We are on bugFix branch
git rebase main
Now we know
bugFix is ahead of
main branch so we have to merge
main branch with
bugFix branch as well
git rebase bugFix
main was an ancestor of
git simply moved the
main reference forward in history.
rebase <branch1> <branch2>
We are on main branch.
git rebase main bugFix
Open up a text editor, show which commits are about to be copied below the target rebase. It also shows their commit hashes and message.
git rebase -i HEAD~4
What is HEAD? HEAD is the work in action. Simply put: what you’re doing now is the
HEAD. Once you commit, the HEAD becomes the branch commit you work on.
Let’s see where the
HEAD is hiding:
git checkout C2
- Moving upwards one commit
- Moving upwards a number of times
git checkout main^
Move 2 times
git checkout HEAD~2
You can directly reassign a branch to a commit with
On bugFix branch, main was on C4 commit.
git branch -f main HEAD~3
Reassign a branch
main to a hash commit, e.g.
git branch -f main C6
Works for local machine
git reset reverses changes by moving a branch reference backwards in time to an older commit, i.e. rewriteing history
git reset HEAD~1
In order to reverse changes and share those reversed changes with others, use
git revert HEAD
A new commit plopped down below the commit we wanted to reverse. This new commit introduces changes - reverse the commit
git cherry-pick <Commit1> <Commit2> <…>
Copy a series of commits below your current location
HEAD. As long as that commit isn’t an ancestor of HEAD.
Suppose we are on
main, and we want
git cherry-pick C2 C4
This will plop down the
main branch twice
git tag <tagname> <Commit>
Tag is used to permanently mark a historical points in the history. It works as an anchor.
git tag v1 C1
git describe <ref>
<tag>_<numCommits>_g<hash>, the hash is where the branch on
git describe main
origin/main is called remote branch. When you check them out, you’re put into detached
HEAD mode. Git does this on purpose because you can’t work on these branches directly.
To be clear: Remote branches are on your local repository, not on the remote repository.
Fetch data from the remote repository
For example, when working on your local codebase, the remote repository has updated and committed few times. In order to download these commits, you can use
git fetch to update your
This update our local
remote branch into synchronization with what the actual remote repository looks like now. It doesn’t change anything about your local state. You have to use
git merge origin/main to merge the code.
Don’t need to put any other arguments to it.
git fetch <source>:<destination>
git push origin main while it’s on opposite direction
Often, when you
git fetch, you’d like to merge it with your local
Use one of the following:
git merge origin/main
git rebase origin/main
git cherry-pick origin/main
git fetch git merge origin/main
git provides a shorthand for it
git push is responsible for uploading your changes to a specified remote and updating that remote to incorporate your new commits. You can think of
git push as a command to “publish” your work.
git push <remote> <place>
The is what branch locally we like to commit to remote. For example,
git push origin main, we simply tell
git that we want the local
main branch to push to remote origin
git push origin main
Note that we don’t have to use
git push origin <source>:<destination>
<source> is where you specify your commit, e.g. C1, C2, etc.
<destination> is the remote branch name
Before git push
git push origin foo^:main
Real word example of
The image above, you cannot
git push because your local repository is on
C3 while the remote repository is on
It’s common for team project. You work on your allocated part and others work on theirs.
Now, how do we push our work to remote repository?
- git fetch
- git rebase origin/main
- git push
git fetch git merge origin/main git push
git pull shorthand
Tired of typing so many commands? Use
git pull !
git pull is shorthand for a fetch and merge. Optionally, you can use
git pull --rebase for a fetch and a rebase
git pull --rebase git push
Alternatively, use merge
git pull git push
Name a different branch to push to remote
git checkout -b totallyNotMain origin/main
Another way to set remote tracking on a branch is to simply use
git branch -u
git branch -u o/main totallyNotMain
totallyNotMain is checked out, it can be shorter:
git branch -u origin/main