[Git] 06. Branch

Branch

List branches

$ git branch
Parameter in short Value Description
(no parameters) List local branches
–remote -r List remote branches
–all -a List local and remote branches

Create new branch

$ git branch {new_branch_name}

Checkout branch or create new branch if not exist

$ git checkout -b {branch_name}
```

## Rename branch

$ git branch -m {old_branch_name} {new_branch_name}


## Delete branch

$ git branch -d {branch_name}


## Delete branch (force)

$ git branch -D {branch_name}


## Checkout certain commit and create branch from it

$ git checkout {sha-1 code}
$ git branch {new_branch_name}


or one line like this

$ git branch {new_branch_name} {sha-1 code}




# Merge branches

$ git checkout {source_branch}
$ git merge {target_branch}


if conflict...

- Apply source ones

$ git checkout –ours xxx.file
$ git add xxx.file
$ git commit -m “….”


- Apply target ones

$ git checkout –theirs xxx.file
$ git add xxx.file
$ git commit -m “….”


- Merge certain commit from other branch

$ git cherry-pick {sha-1 code 1} {sha-1 code 2}


- Pick certain commit to index

$ git cherry-pick {sha-1 code} –no-commit





# Rebase branches

$ git checkout {source_branch}
$ git rebase {target_branch}


if conflict, use the same commands on *Merge Branch* and then

$ git rebase –continue
`

[Git] 05. Log

Log

$ git log --oneline --graph
$ git log --author="JB"
$ git log --after="2018-03-02" --since="10am" --until="18pm"
$ git log --pretty=format:"%H %an %ad"
$ git log --pretty=format:"%h %an %ad"
$ git log --after="2018-07-11" --pretty=format:"%h %an %ad"

Search commit messages

$ git log --grep="Fix bugs"

See commit logs for every line in a file

$ git blame xxx.file

See commit logs for certain lines in a file

$ git blame -L 10,20 xxx.file

Verifies the connectivity and validity of the objects

$ git fsck

and for unreachable objects

$ git fsck --unreachable

reflog: show commit and head moving logs

$ git reflog

reflog: clean logs (nake forbidden objects to unreachable)

$ git reflog expire --all --expire=now

list files

$ git ls-files -s

[Git] 04. Reset

Reset

Checkout file for latest commit

$ git checkout xxx.file

Reset to relative commit (^: previous commit)

$ git reset {sha1-code}^
```

or

$ git reset {sha1-code}~1


> *Git reset modes*
>
> * mixed (Default)
>
> Revert current changes index, but keep them on working tree
>
> * soft
>
> Revert current changes index, but keep them on working tree
>
> * hard
>
> Revert current changes on working tree and index
>

## Reset modes example

Commit 3 times as following,

![](../assets/001.png)

![](../assets/002.png)


Then make changes and add to index. The diff results ...

`git diff` (Compare working tree to index)

No difference

`git diff --cached` (Compare index to HEAD)

![](../assets/003.png)

`git diff HEAD` (Compare working tree to HEAD)

![](../assets/004.png)


Now reset the the 1st commit and see the differences between 3 modes

* mixed

$ git reset –mixed af01615


![](../assets/005.png)

![](../assets/006.png)

ps. `git diff --cached` will get nothing.


* soft

$ git reset –soft af01615


![](../assets/007.png)

![](../assets/008.png)

ps. `git diff` will get nothing.


* hard

$ git reset –hard af01615
`

ps. git diff, git diff --cached, git diff HEAD will all get nothing.