Git

[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