Tools

[Git] Pretty tree log

This is an easy way to show a pretty git log using alias.

  • Command:
git log --oneline --decorate --all --graph
  • Add to alias:
git config --global alias.tree "log --oneline --decorate --all --graph"

Then, use git tree to show the tree view:

[Git] 使用rebase整理merge commits

註解 2020-05-27 112920

使用指令:

git rebase -i 61d3628 -r
註解 2020-05-27 113038

onto為rebase指定的commit,此例為61d3628 。
直接編輯各commit的順序及是否生成merge commit,即可隨心所欲整理merge commits.
下圖為將b2的merge拉直的結果

註解 2020-05-27 113304

[Docker] 常用指令

Image

  • List all stored images
docker image ls
  • Pull an image
docker pull xxx
  • Build an image
$ docker build -t imagename:2.0 .
  • Delete specific images
docker rmi -f $(docker images -q myname)
# or
docker rmi -f image-name
  • Delete all images
docker rmi -f $(docker images -aq)

Container

  • Show all containers
docker ps -a
  • Run/stop a container
docker run -it ubuntu bash
# -d: Run in detached mode
# -v: HOST_PATH:CONTAINER_PATH
# --restart=always

# Stop a specific container
docker stop container_name

# Stop all containers
docker kill $(docker ps -q)
  • Enter/exit a container
# Enter an exited container
docker start myname
docker exec -it myname bash

# Detach from a container
Ctrl-p + Ctrl-q inside the container shell
# Re-attach into an up container shell
docker attach myname
  • Delete a specific container
docker rm -f container_name
  • Delete all containers
docker rm -f $(docker ps -aq)
  • Mirror ports
# HOST_PORT:CONTAINER_PORT
docker run -p 8000:8000 myimage
  • Copy file from/to container
docker cp test.txt mycontainer:/app/test.txt

Volume

  • List all volumes
docker volume ls
  • Mount a volume from another container
--volumes-from container_name

Networking

  • Link containers (legacy)
--link container_name (legacy)
# Then you can ping container_name
# It’s better to create a network and add the containers to the network as below
  • Create a network
docker network create network-name
# Creates a bridge network by default
  • network mode (–net)
    • none: No network connection
    • container: Mirror a specific container
    • host: Mirror the host
    • bridge: Default. Like NAT
    • custom created network: Overlay or bridge

Tag

  • Tag an image for pushing
docker tag image-name DockerHubAcc/image-name

Save/Export and Load/Import

  • save (for image)
docker save myimage > myimage.tar

# Using gzip for compression
docker save myimage:tag | gzip > myimage_tag.tar.gz
  • load (for image)
docker load < myimage.tar
  • export (for container)
docker export mycontainer > mycontainer.tar
  • import (for container)
docker import mycontainer.tar image-name

Logging/Stats

  • Show log for a container
docker logs container_name
# Use -f to watch the logs
  • Show HW stats
docker stats

Cleanup

  • Delete all stopped containers
docker container prune
  • Clear all space used by images, containers and volumes
docker system prune --volumes  # Only remove dangling images
# -a: Remove all images with no container associated

Dockerfile

  • FROM
  • RUN

Use && and \ to chain commands to avoid too many intermediate images get created.

  • ADD/COPY

ADD can auto extract .tar.gz and handle remote URL, otherwise use COPY.

  • ENV
  • VOLUME

Declare auto created volumes using container paths

  • EXPOSE

Only for declaration purpose. docker run -P takes a random port to map to this exposed port.

  • CMD/ENTRYPOINT

Most likely the same if used alone. If ENTRYPOINT is used, commands are appended as its arguments (either specified in command line or using CMD. CMD will serve as a default argument in this case)

Docker-compose

  • Pull all related images
docker-compose pull
  • Create/delete all objects
# Create all stuffs
docker-compose up -d

# Delete all stuffs
docker-compose down
  • Start/stop the containers
# Start the containers
docker-compose start

# Stop the containers
docker-compose stop
  • Show information
docker-compose logs
docker-compose ps

[Git] tag相關

  • 列出所有tag
git tag -l
  • 新增tag
git tag -a v1.0 -m "my version message"
  • 上傳tag資訊到遠端
git push origin --tags
  • 刪除遠端tag “v1.0”
git push origin :refs/tags/v1.0

[Git] 取消追蹤檔案(但是保留檔案)

Step1:

將欲取消追蹤的檔案加入.gitignore

Step2:

在repo中刪除該檔案:

git rm -r --cached .
git commit -m "Untrack and delete filename in repo."

Step3:

push到遠端

[Git] 解決conflict (官方)

Step 1: From your project repository, bring in the changes and test.

git fetch origin
git checkout -b steven origin/steven
git merge master

Step 2: Merge the changes and update on GitHub.

git checkout master
git merge --no-ff steven
git push origin master

Conflict with binary files:

git checkout --theirs -- path/to/conflicted-file.txt
git checkout --ours -- path/to/conflicted-file.txt

[Git] 合併commit

由新到舊 D -> C -> B -> A
要把D和C合併成單一個C’

Step1.

rebase -i <B的SHA-1>
若要rebase初始commit(A):
rebase -i --root

Step2.

pick <C的SHA-1> C的commit訊息
squash <D的SHA-1> D的commit訊息

Step3.

編輯C’的commit訊息

完成

P.S.若要push到github上則要強迫push:

git push --force

[Git] 常用指令

  • 初始化使用者資訊
git config --global user.email "my_email"
git config --global user.name "my_name"
  • 初始化git目錄
git init
  • 查看git狀態
git status
  • 一次加入所有更改檔案
git add .
git add -A
  • commit
git commit -m “commit message”
  • 操作遠端repo
# 顯示資訊
git remote -v
git remote show origin
# 刪除目前origin
git remote rm origin
# 新增origin
git remote add origin git@github.com:SongRongLee/xxxxx.git
  • 第一次push/pull
git push -u origin master
or
git branch -u origin/master
#以後只需要:
git push
  • 返回特定commit
git reset --hard HEAD^
git reset --hard HEAD~2
  • branch相關
# 以遠端分支為藍本建立本地new-branch
git checkout -b new-branch origin/master
# Delete a remote branch
git push -d origin bad-branch

# Delete a local branch
git branch -d bad-branch

# Sync remote branch information after deletion
git fetch --prune

# List all remote and local branches
git branch -a
# 修改branch名稱
git branch -m my-new-name
# push本地的local-branch到origin/remote-branch
git push origin local-branch:remote-branch
# Shows changes from from_branch to to_branch
git diff from_branch to_branch
  • stash相關
# pull但是保留local change
git stash
git pull
git stash pop

# Stash untracked files
git stash -u

# List all stashes
git stash list

# Pop a certain stash
git stash pop stash@{n}

# Stash with a given message
git stash -m my_message
  • 刪除first commit
git update-ref -d HEAD
  • Unstage all changes
git reset
  • Discard changes on one file
git checkout -- filename
  • Force overwrite local repo
git reset --hard origin/master
  • Move to next commit
git reset HEAD@{1}
  • Rename
git mv
  • Show ignored files
git status --ignored
  • Remove untracked files and directories
git clean -dn  # Dry run
git clean -df  # Actual remove
  • Check SHA-1 of commits from history
git reflog