Saturday, January 15, 2022

Tutorial Git branch by examples

Git branch by examples

Git branch sound confusing. This is a jumpstart version, I try to make it as easy as possible

Working with Git branch is easy. Git branch is light-weight. When working with new feature or bug fix, just create a new branch.

Use 'git branch' and 'git checkout':

  Git branch to create branch and show branches:
  format: git branch branch_name
  
  Git checkout to switch between branch:
  format git checkout [-b] branch_name
  

*Important*: Changes before commit exist in the directory (and all branches), after committed the changes will not appear in other branches. The changes after commit will be isolated from other branches. (Will be explained later by example)

Scenario

While working on feature_x, a hotfix needs to be done and released without the feature_x. Should commit feature_x before working on hotfix.

Steps:
1. create branch feature_x and hotfix (from master)
make changes and commit each branch
2. switch between the branches and observe changes are isolated
Note: feature_x started first but not merged yet. Hotfix release first.

      +-- hotfix (add 'hot fix' in README.md file)
  master
      +-- feature_x (add 'feature x' in featurex file)

Git branch and git checkout

In the working directory

$ git branch  # or git branch [--list|-l]
* master  # only 1 main branch
($ git branch -a, shows remote repository branch as well)
$ echo 'Hello, world' > README.txt
  

Create branch feature_x

Create branch:
$ git branch feature_x
$ git branch
feature_x  # feature_x branch was created
* master   # still at master branch
$ git checkout feature_x
$ git branch
* feature_x  # switch to feature_x
master
  

Note: Use 'git checkout -b featurex' to create and switch to branch feature_x.

The new changes are just in the file featurex.

(feature_x)$ echo 'feature x' >> featurex  # make some changes
(feature_x)$ git add featurex
(feature_x)$ git commit -m 'feature x'

(feature_x)$ git log --oneline
be85a75 (HEAD -> feature_x) feature x
9462eec (master) Initial commit

$ git checkou master; git log --oneline
Switched to branch 'master'
9462eec (HEAD -> master) Initial commit# no feature_x committed in the master branch.

The feature_x is not merged into master branch yet. Suddently there is an adhoc bug fix:

(master)$ git checkout -b hotfix
(hotfix)$ ls
README.md  # there is no featurex in this branch
(hotfix)$ echo 'hot fix' > README.md
(hotfix)$ git commit -am 'hot fix'
[hotfix 8cbc155] hot fix
 1 file changed, 1 insertion(+), 1 deletion(-)

(hotfix)$ git branch
  feature_x
* hotfix
  master
(hotfix) git checkout master
(master) cat README.md
==>Hello, world  # changes in hotfix not in master branch, it was isolated.
(master)$ git log --oneline
9462eec (HEAD -> master) Initial commit  # and the commit in other branch is not merge yet.

Merge and delete branch

(master)$ git log --oneline --branches
8cbc155 (hotfix) hot fix
be85a75 (feature_x) feature x
9462eec (HEAD -> master) Initial commit

(master)$ git log --oneline --branches --graph 
* 8cbc155 (hotfix) hot fix
| * be85a75 (feature_x) feature x
|/  
* 9462eec (HEAD -> master) Initial commit

There are 2 branches, merge hotfix just move the HEAD pointer to hotfix, later merge featurex branch, system need a new commit after merging changes in featurex.

(master)$ git merge hotfix
Updating 9462eec..8cbc155
Fast-forward
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(master)$ git branch -d hotfix  # it is safe to use -d

Note: It is save to use -d, if the branch is not merged, it can not be deleted. Use -D to delete branch that is not merged.

(master)$ git log --oneline --branches
8cbc155 (HEAD -> master, hotfix) hot fix
be85a75 (feature_x) feature x
9462eec Initial commit

You can try to merge feater_x, it needs a new commit, because it need to merge then changes into master+hotfix.

Wednesday, January 05, 2022

Fibonacci One Line Code with Python

Fibonacci One Line Code with Python



This is using recursive and shorthand if to implement fibonacci. Fibonacci is a sequence, the value of the sequence is the sum of the previous 2 numbers in the sequence: eg. 1, 1, 2, 3, 5, 8, 13, 21, ...

The normal Python function:

  def fibonacci(n):
	return 1 if n<=1 else fibonacci(n-1) + fibonacci(n-2)
  

To output:

    for n in range(8):
        print(fibonacci(n))
  

Ok, with the function define, that's 2 lines of code.