digitallyamar / git-visualized

Step by step changes happening within .git directory as we use git for version control.
0 stars 0 forks source link

Git from scratch to Git Porcelain commands #1

Open digitallyamar opened 6 years ago

digitallyamar commented 6 years ago

Step 1: Create a Git Repo

~ $ mkdir alpha ~ $ cd alpha

~/alpha $ mkdir data ~/alpha $ printf 'a' > data/letter.txt

~/alpha $ git init Initialized empty Git repository

The alpha directory now looks like this: alpha ├── data | └── letter.txt └── .git ├── objects etc...

This results in a initial git repo that looks like this:

git-draw

digitallyamar commented 6 years ago

Step 2: Add some files

~/alpha $ git add data/letter.txt

git-draw

~/alpha $ printf '1234' > data/number.txt

alpha └── data └── letter.txt └── number.txt

~/alpha $ git add data git-draw

Make a correction and add the file to the index again:

~/alpha $ printf '1' > data/number.txt ~/alpha $ git add data

git-draw

digitallyamar commented 6 years ago

Step 3: Make a commit

~/alpha $ git commit -m 'a1' [master (root-commit) c81d2ea] a1

git-draw

digitallyamar commented 6 years ago

Step 4: Update the number file again

~/alpha $ printf '2' > data/number.txt

This results in working directory containing content different from index or git database. Add new content to index:

~/alpha $ git add data/number.txt

This results in new object blob created. It points the index entry for data/number.txt at the new blob:

git-draw

digitallyamar commented 6 years ago

Step 5: Commit new content with numbers.txt content changes again

~/alpha $ git commit -m 'a2'

git-draw

digitallyamar commented 6 years ago

Step 6: Checkout A Commit

~/alpha $ git checkout 0956eb

Note: checking out '0956eb'.

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

git checkout -b

HEAD is now at 0956ebb... a2

git-draw

digitallyamar commented 6 years ago

Step 7: Detached HEAD

Update & commit number.txt file content again

~/alpha $ printf '3' > data/number.txt ~/alpha $ git add data/number.txt ~/alpha $ git commit -m 'a3' [detached HEAD d13d2e9] a3

git-draw

digitallyamar commented 6 years ago

Step 8: Create A Branch

This just creates a new file at .git/refs/heads/deputy that contains the hash that HEAD is pointing at: the hash of the a3 commit.

git-draw

digitallyamar commented 6 years ago

Step 9: Check out a branch

Checkout the master branch again:

~/alpha $ git checkout master Switched to branch 'master'

This puts HEAD back to pointing to master:

git-draw

digitallyamar commented 6 years ago

Step 10: Check out the other branch

~/alpha $ git checkout deputy Switched to branch 'deputy'

git-draw

digitallyamar commented 6 years ago

Step 10: Merge an ancestor

~/alpha $ git merge master Already up-to-date.

git-draw

digitallyamar commented 6 years ago

Step 11: Merge a descendent (Fast Forward)

~/alpha $ git checkout master Switched to branch 'master'

~/alpha $ git merge deputy Fast-forward

git-draw

digitallyamar commented 6 years ago

Step 12: Merge two commits from different lineages

~/alpha $ printf '4' > data/number.txt ~/alpha $ git add data/number.txt ~/alpha $ git commit -m 'a4' [master e98bf22] a4

git-draw

Next checkout deputy branch and update content there too!

~/alpha $ git checkout deputy Switched to branch 'deputy' ~/alpha $ printf 'b' > data/letter.txt ~/alpha $ git add data/letter.txt ~/alpha $ git commit -m 'b3' [deputy 98b33e9] b3

git-draw

Finally merge the two branches:

~/alpha $ git merge master -m 'b4' Merge made by the 'recursive' strategy.

git-draw

digitallyamar commented 6 years ago

Step 13: Merge two commits from different lineages that both modify the same file

~/alpha $ git checkout master Switched to branch 'master' ~/alpha $ git merge deputy Fast-forward

git-draw

~/alpha $ git checkout deputy Switched to branch 'deputy' ~/alpha $ printf '5' > data/number.txt ~/alpha $ git add data/number.txt ~/alpha $ git commit -m 'b5' [deputy bd797c2] b5

git-draw

~/alpha $ git checkout master Switched to branch 'master' ~/alpha $ printf '6' > data/number.txt ~/alpha $ git add data/number.txt ~/alpha $ git commit -m 'b6' [master 4c3ce18] b6

git-draw

~/alpha $ git merge deputy CONFLICT in data/number.txt Automatic merge failed; fix conflicts and commit the result.

cat data/number.txt <<<<<<< HEAD 6

5

deputy

Manually fix the merge conflict. In our case, we simply update the number.txt file with a new value

~/alpha $ printf '11' > data/number.txt ~/alpha $ git add data/number.txt

~/alpha $ git commit -m 'b11' [master 5b7f752] b11

git-draw