- 解決協同開發時改到相同檔案的衝突 (solve conflict)
- 每個開發階段的commit都被詳實記錄 (for review)
- 各自擁有自己的repo可單機獨立作業 (avoid lock)
.gitignore
# Ignore all files under directory/ include this folder.
directory/*
# Exception
!directory/iam.here
Ignore specific env confg
git update-index --assume-unchanged
Config file location
~/.gitconfig
Set global config
git config --global user.name tonypai
git config --global user.email [email protected]
git config --global color.ui true
Remote related command
# Fetch all remotes
git fetch --all
# Push your changes to origin by force (include replace commits)
git push --force
# Fetch + Merge
git pull
# Change origin from https to ssh
git remote -v
git remote set-url origin [email protected]:user/repo.git
# Change upstream of specific branch
git branch branch_name --set-upstream-to your_new_remote/branch_name
Sort git tree
# Interactive mode
git rebase -i
# Edit previous commit
git commit --amend -m "new message"
# Same as...
git reset HEAD^ --soft
git commit
# Continue
git rebase --continue
# Resolve conflicts
git checkout <commit> path/to/file
Stash changes
# unstaged
git stash -k
# all include untracked files
git stash save -u
# pop the last stash then delete
git stash pop
# apply the last stash
git stash apply
Manully Edit Hunk
@@ -1,4 +1,5 @@
NO CHANGE LINE
- EXTRA OLD LINE
- OLD LINE
+ EXTRA NEW LINE
+ NEW LINE
+ NEW LINE
NO CHANGE LINE
@@ -1,3 +1,4 @@
NO CHANGE LINE
- OLD LINE
+ NEW LINE
+ NEW LINE
NO CHANGE LINE
Ref: Selectively select changes to commit with git (or Imma edit your hunk)
Overwrite local branch on pull
git reset --hard origin/your_branch
Discard all changes
# Removes staged and working directory changes
git reset --hard
# Remove untracked files
git clean -f -d
# CAUTION: as above but removes ignored files like config.
git clean -f -x -d
Discard specific file
git checkout -- file
Revert a file to specific revision
git checkout <commit> path/to/file
Ref: Reset or revert a specific file to a specific revision using Git?
Delete remote branch
git push origin :<branch-name>
Delete all branches which have been merged
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
Ref: Delete Local Git Branches After Deleting Them On The Remote Repo
View the change log of a file
git log -- path/to/file
gitk path/to/file
See the last 5 commits from specific author
git log -n 5 --author=tony
You have not concluded your merge (MERGE_HEAD exists)
- Undo the merge and pull again.
- Resolve the conflict.
- Don't forget to add and commit the merge.
- git pull now should work fine.
git merge --abort [Since git version 1.7.4]
git reset --merge [prior git versions]
Ref: http://stackoverflow.com/questions/11646107/you-have-not-concluded-your-merge-merge-head-exists
Could not execute editor
git config --global core.editor "/usr/bin/vim"
Refs: http://stackoverflow.com/questions/4092432/could-not-execute-editor#comment6489808_4096135
Edit root commit message
# Checkout the root commit
git checkout <sha1-of-root>
# Amend the commit
git commit --amend
# Rebase all the other commits in master onto the amended root
git rebase --onto HEAD HEAD master
Refs: http://stackoverflow.com/questions/2119480/edit-the-root-commit-in-git
How do you control the order in which files appear in a GitHub gist
ASCIIbetical order
- All uppercase come before lowercase letters, for example, "Z" before "a"
- Digits and many punctuation marks come before letters, for example, "4" precedes "one"
- Numbers are sorted naïvely as strings, for example, "10" precedes "2"
Ref: stackoverflow
如何寫得一手好 git commit message
- 標題與內文之間需間隔一空白行
- 標題字數限制50字
- 標題第一個字大寫
- 標題不需要句點
- 標題使用命令/祈使句
- 內文每72字換行
- 內文用做了什麼以及為什麼取代如何做
git 檔案衝突符號詳解
衝突檔案內容如下:
<<<<<<< HEAD:file.txt
Hello world
=======
Goodbye
>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
本地端的原資料:
<<<<<<< HEAD:file.txt
Hello world
=======
merge 後的衝突資料:
=======
Goodbye
>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
Ref: git conflict markers
Add the remote, call it "upstream":
git remote add upstream https://github.com/whoever/whatever.git
Fetch all the branches of that remote into remote-tracking branches, such as upstream/master:
git fetch upstream
Make sure that you're on your master branch
git checkout master
Rewrite your master branch so that any commits of yours that aren't already in upstream/master are replayed on top of that other branch:
git rebase upstream/master
Ref: stackoverflow
Manually Compile Git From Source
apt-get update
apt-get install build-essential libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev
// update git client version
git clone git://git.kernel.org/pub/scm/git/git.git
cd git
git checkout v2.7.4
// download package
wget https://www.kernel.org/pub/software/scm/git/git-2.11.0.tar.gz
tar zxvf git-2.11.0.tar.gz
cd git-2.11.0
make prefix=/usr/local all
sudo make prefix=/usr/local install
Rename Branch
git branch -m <oldname> <newname>