Git

[Git] Change case of a folder name

Make sure you have the ignorecase set to false:

git config core.ignorecase false

Then, do two git mv:

git mv foldername tempname
git mv tempname folderName

[Git] Change commit author

Use git rebase to select commits you want to modify

git rebase -i --root

And then for each rebase step, execute the following:

git commit --amend --reset-author --no-edit
git rebase --continue

[Git] Git submodule basic commands

  • Clone a repo tree
git clone --recurse-submodules remote_url
  • Link a submodule
git submodule add submodule_remote_url local_linking_path
# Then commit & push to the main repo
  • Pull from a submodule
# Pull from specific branch
git pull --recurse-submodules my-branch
# Then commit & push to the main repo
  • Update a submodule
cd submodule_path
# Do the commits and push normally, the changes will be pushed to submodule repo

cd main
# Then do a commit and push to the main module
  • Reset submodule head to currently stored reference
git submodule update --init

[Git] Show logs for specific branch

If you have master merged into your current branch, your git log will be messy because it also shows the commits from the master.

If you want to show your branch logs, use the following command:

git log master..

or

git log master..mybranch

* git log a..b actually means “includes commits that are (backward) reachable from b but exclude those that are reachable from a”

[Git] Resolve conflicts for git stash pop

Assume you have conflicts after executing git stash pop:

  1. Resolve the conflicts manually by yourself
  2. Use git reset to mark conflicts as resolved
  3. Drop the stash using git stash drop because git doesn’t do it if conflicts occur

[Git] Cut commits onto other branches using git rebase –onto

Assume we have following branches:

We want to cut commit_3 and commit_4 on branch_2, onto master (commit_2). We can use the commands:

git checkout branch_2
git rebase --onto b111dc9 34e3359
(git rebase --onto new_base old_base)

The result looks like this:

If we want the commits but not to modify the incoming branch (branch_2 in the above example), we can use cherry-pick :

git checkout master
git cherry-pick 34e3359..321b7c0
(git cherry-pick start_commit_not_included..end_commit_included)

[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

[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到遠端