reading-notes

Code Fellows Notes

View the Project on GitHub stephnitis/reading-notes

Learn Git Branching

Git is a DevOps tool used for source code management. It is a free and open-source version control system used to handle small to very large projects efficiently. Git is used to tracking changes in the source code, enabling multiple developers to work together on non-linear development.

Git Commits

Git Commit

Git Branches

Git Branch

Git Checkout

Branches and Merging

Git Merge

Git Merge Two

Rebasing

Git Rebase Demo

Git HEAD

Git Detach HEAD

Relative Refs

Realtive Ref Caret

Relative Ref Caret and HEAD

Relative Ref Tilde

Branch Forcing

Branch Forcing

Reversing Changes in Git

Two primary ways to undo changes:

  1. git reset
  2. git revert

git reset

git revert

Moving Work Around

Git Cherry-pick

git cherry-pick <Commit1> <Commit2> <...>

git cherry-pick

Git Interactive Rebase

git interactive rebase demo

Locally Stacked Commits

Grabbing Just 1 Commit:

Juggling Commits

The first command is git rebase -i HEAD~2

We will overcome this difficulty by doing the following:

Git Tags

git tag demo

Git Describe

Git describe takes the form of:

git describe <ref>

Where <ref> is anything git can resolve into a commit. If you don’t specify a ref, git just uses where you’re checked out right now (HEAD).

The output of the command looks like:

<tag>_<numCommits>_g<hash>

Where tag is the closest ancestor tag in history, numCommits is how many commits away that tag is, and <hash> is the hash of the commit being described.

git describe demo

Specifying Parents

git specify demo 1

git specify demo 2

git specify demo 3

git specify demo 4

COMPLETED

Git Remotes

git clone demo

Git Remote Branches

<remote name>/<branch name>

git origin demo

Git Fetch

git fetch demo

Git Pull

git pull demo

git pull demo two

Git Push

git push demo

Diverged Work

git push reject demo

git push fix demo

git merge fix demo

git push fix demo

git pull fix demo

Remote Rejected

If you work on a large collaborative team it’s likely that main is locked and requires some Pull Request process to merge changes. If you commit directly to main locally and try pushing you will be greeted with a message similar to this:

! [remote rejected] main -> main (TF402455: Pushes to this branch are not permitted; you must use a pull request to update this branch.)

Merging Feature Branches

Some developers only push and pull when on the main branch – that way main always stays updated to what is on the remote (o/main).

So for this workflow we combine two things:

refresher

Why Not Merge?

There’s a lot of debate about the tradeoffs between merging and rebasing in the development community. Here are the general pros / cons of rebasing:

Pros:

Cons:

For example, commit C1 can be rebased past C3. It then appears that the work for C1' came after C3 when in reality it was completed beforehand.

Some developers love to preserve history and thus prefer merging. Others prefer having a clean commit tree and prefer rebasing. It all comes down to preferences.

git merge solution

Remote-Tracking branches

This also explains why you may see the following command output when cloning:

local branch "main" set to track remote branch "o/main"

There are two ways to set this property. The first is to checkout a new branch by using a remote branch as the specified ref. Running

git checkout -b totallyNotMain o/main

Creates a new branch named totallyNotMain and sets it to track o/main.

new branch option one

Another way to set remote tracking on a branch is to simply use the git branch -u option. Running

git branch -u o/main foo

will set the foo branch to track o/main. If foo is currently checked out you can even leave it off:

git branch -u o/main

new branch option two

Push Arguments

git push can optionally take arguments in the form of:

git push <remote> <place>

git push origin main

translates to this in English:

Go to the branch named “main” in my repository, grab all the commits, and then go to the branch “main” on the remote named “origin”. Place whatever commits are missing on that branch and then tell me when you’re done.

By specifying main as the “place” argument, we told git where the commits will come from and where the commits will go. It’s essentially the “place” or “location” to synchronize between the two repositories.

Keep in mind that since we told git everything it needs to know (by specifying both arguments), it totally ignores where we are checked out

<place> argument details

In order to specify both the source and the destination of <place>, simply join the two together with a colon:

git push origin <source>:<destination>

This is commonly referred to as a colon refspec. Refspec is just a fancy name for a location that git can figure out

LEVEL COMPLETE