git clone <url> [path]
git init
git status
git log
git log --name-status
: show filename with modification statusgit log --stat
: show filename with more info
git diff <file>
git diff <commit_or_branch> <commit_or_branch>
git diff -–name-only <commit_or_branch> <commit_or_branch>
git status
first, see if you are in troublegit add .
to stage all modificationsgit commit -m "<message>"
to wrap changes and push the HEAD to new progressgit push
to send changes to the cloud
git pull
Grab progress from remote, auto merge when theres changes on localgit pull --rebase
Same asgit pull
but use rebase when theres changes on local
git checkout <path>
git checkout <branch_or_commit>
git checkout -b <new_branch_name>
git branch -u <remote>/<new_branch_name> [new_branch_name]
to set (bind) the remote branch for push and pull
git remote -v
: see the remote registry of the repogit remote add|rm <name> <url>
git config ...
git log --oneline --graph --decorate --all
git diff <old_commit_or_branch> <new_commit_or_branch> > my.patch
git apply my.patch
git commit --amend
git checkout <B>
to detach head and move to Agit reset --soft <A>
to move HEAD to the old commit, but leave the index and working tree as for the commit keptgit commit -C <B>
to Redo the commit kept re-using the commit message, but now on top of the old commitgit rebase --onto HEAD <B> <branch_name>
to re-apply everything from the olds onwards onto this new placegit push --force
to overwrite remote repo
First the following things need to be defined:
HEAD
: thehistory
, the current branch is pointing atINDEX
/Stage
: thecache
, the added changesWorking directory
: the code and files in your directory
Then
git reset --soft [target]
: setHEAD
onlygit reset [--mixed] [target]
: setHEAD
andINDEX
git reset --hard [target]
: setHEAD
,INDEX
andWorking directory
git checkout <history_node>
setHEAD
,INDEX
andWorking directory
, but leaving the original branch- So it is a detached state, you can go back by
git checkout <original_branch>
- Or create a branch by
git checkout -b <branch_name>
, I foundgit checkout <history_node> -b <branch_name>
will do the two steps together
- So it is a detached state, you can go back by
- When cloning a repo from remote, the submodule is not initualized and the submodule directory is empty. To solve this
- add
--recursive
option when cloning to clone submodules in the target repo, for example:git clone --recursive <url>
- use
git submodule init
andgit submodule update --recursive
if you already cloned and found that the submodule is empty
- add
git submodule add <url> <path>
to add a submodulegit submodule foreach --recursive git pull origin master
to pull the latest code for each submodule- to delete the submodule, it is required to do it manually. Edit
.gitmodules
and.git/config
to remove related information and remove the files in working directory
- Code Smart, Don't Code hard by crboy