$ git init
$ vim README.md
$ git add README.md
$ git commit
$ git config --global user.email "you@example.com"
$ git config --global user.name "Your Name"
$ git commit
$ git config --global core.editor vim
$ git config --global merge.tool vimdiff
$ git config --global color.ui true
$ git config --global alias.st status
$ git config --global alias.ck checkout
$ git config --global alias.rst reset HEAD
$ vim ~/.gitconfig
$ git st
# change some files
$ git ck <THE_CHANGE_FILES>
# change is discarded
# add Not checkout files
$ git add UNADD_FILES
# change files already exist
$ git add -p load.cpp
# git commit (50/72 rule)
$ git commit
-
Large files
-
Generated files
-
Files with secret data
$ vim .gitignore
# We can choose some files we don't want to see
$ git add .gitignore
$ git reset HEAD .gitignore
# uncheck .gitignore (turn back to the status before we add)
- reset modification made by add/commit
- All change become 'unstaged'
- Use reset --hard carefully
# See the log start at SOME_HASH_VALUE and the number before will display 1
# --Oneline mean display only one line
$ git log SOME_HASH_VALUE -n1 --oneline
git manage references to commit : HEAD, Branch, Tag, Remote
We can indicate commit by ^, ~
$ git log -n1 <ONE_HASH_VALUE_BEFORE_HEAD> --online
$ git log -n1 HEAD^ --online
$ git log -n1 HEAD~1 --oneline
# The above command is same
$ git log <SOME_FILE> --oneline
# display the commit relate to SOME_FILE
$ git log --stat <SOME_FILE>
$ git log --patch <SOME_FILE>
# --stat display the CHANGE_FILE and change line
# --patch display the code which is changed
$ git diff
# Display what is changedon current commit (not add to stage)
$ git diff --staged
# Display what is changedon current commit (already stage)
$ git diff master
# Display what is changed compared to master
# Always use it
$ git add -p
# Will display Stage this hunk[y,n,q,a,d,/,K,j,J,g,e,?]?
s : slice the section thinner e : edit , go inside the editor K : go previous J : go next q : quit a : add all the hunk after it
$ git commit
# change subtle
$ git commit --amend
# replace the current HEAD with new HEAD (Not delete, just move to parent and create new commit)
$ git checkout <commit>
# Move to HEAD
$ git checkout <path or file>
# discard change of this path or file
$ git branch
# list the branch now
$ git branch <some_branch>
# create <some_branch>
$ git checkout <some_branch>
# switch to <some_branch>
$ git checkout -b <another_branch>
# (same as the above commands)
# create <another_branch> and switch to <another_branch>
$ git log --graph
# show the current commmit in easy graph
$ git branch -d <some_branch>
# deleted branch <some_branch>
$ git ck master
$ git merge <some_branch> --no-ff
# if just use git merge <some_branch>, git may just use <some_branch> as reference
# may appear conflict
Note:
-
Prevent very long development
-
Split source code clearly
$ git rebase <new_base>
# Rebase current branch to <new_branch>
$ git rebase --onto <new base-commit> <current base-commit>
# Rebase from common ancestor of <ref_commit> and <branch>
If conflict happen, you have to fix it and use
$ git st
# check which files need to be fixed,=
$ git add <both modified file>
$ git rebase --continue
$ git rebase --abort
# if out of control
$ git rebase -i <some_branch>^^^^ <some_branch>
# As the previous four commit as base and put on <some_branch>
More explanation and reference: https://blog.yorkxin.org/2011/07/29/git-rebase.html
$ git clone <repo_url>
$ git branch -a
# show all the branch form the remote
$ git remote show origin
# show the information of the orgin
$ git ck origin/<some_branch_from_remote>
# will error since it is the detached HEAD state
# use below
$ git ck -b <some_branch_from_remote>
$ git fetch <remote_name>
$ git ck master
# Use fast-forwarded
$ git rebase <remote_name>/master
$ git push <remote> <local_branch>:<remote_branch>
# Push local branch to remote branch
# --set-upstream
$ git push <remote> :<branch>
# Delete remote branch
- First fork to your repository
- Clone
$ git clone <url>
- Add
$ git remote add upstream <url>
$ git fetch upstream
$ git ck develop
$ git rebase upstream/develop
# Doing backup
$ git reset HEAD^
$ git ck -b <some_fixing_branch>
-
Start to fix or development
-
Local add and commit
$ git add <file>
$ git commit
- Push
$ git push origin <some_fixing_branch>
-
Go to project and Pull Request
-
If you success, update the project
$ git ck develop
$ git rebase upstream/develop
- Update your fork github
$ git push origin develop
- Delete the branch in your repo
$ git push origin :<some_fixing_branch>
Generate a new ssh key and add it to ssh agent: https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent Add ssh key to your own github account: https://help.github.com/en/articles/adding-a-new-ssh-key-to-your-github-account