Git常见命令
目录
简单罗列一下本人日常使用到的一些git命令
branch
git branch -l # 列出本地分支
git branch -a # 列出分支(远程和本地)
git branch <new branch name> # 从当前分支创建新分支
git checkout -b <new branch name> # 从当前分支创建新分支,并切换到新分支
git branch <new branch name> <commit_hash> # 从特定commit创建分支
git branch --unset-upstream # 移除本地分支和远程分支的关联
git branch -d <branch name 1> <branch name 2> ... # 删除分支
#( be careful with option -D, it will delete branch even not fully merged to master )
# 重命名本地branch然后push到远程(rename branch)
git branch -m <new name> # rename current branch to new name
git push -u origin <new name> # push local new branch to remote
git push origin --delete <old name> # delete remote old branch
diff
git diff # compare workplace with index
git diff --staged # same as --cached, compare index(staged) with HEAD(local repository)
git diff HEAD # compare workspace with repository
git diff <branch1> <branch2> # diff with two branch (index tree, not working directory)
git diff <branch1>..<branch2> # same as above
git diff <your merge base branch name>...<branch>
# diff between two branch, merge base version (index tree, not working directory)
git diff <your merge base branch name>...
# same with above, but with current branch (index tree, not working directory)
git diff origin/master... --name-only
git diff master # diff with master (working tree)
git diff master --staged # diff with master ( index tree)
git diff master -- filename # diff with file in another branch (specified file)
git diff origin/mow -- filename # diff with file in remote repo
git diff <commit> <filename> # diff current file with file in commit
git diff <commit 1> <commit 2> -- <filename> # diff file in commit2 (new commit) to commit1 (old commit)
git diff --no-index <file1 > <file2> # 直接diff磁盘上的两个文件(repo之外,或当任一file不在Index中时,no-index可以忽略)
# Useful diff option: -b ( ignore whitespace )
git show -1 filename.txt # to compare against the last revision of file
git show -2 filename.txt # to compare against the 2nd last revision of file
–name-only只显示文件名 –name-status 显示文件名和状态
Pull from another branch to local
git checkout master
git pull
或
git checkout master
git fetch # 将远程仓库的最新commit下载到本地,以及后续进行其它操作
git merge master
show specific version of file
git show <commit_hash> --name-only # see what files was modified in that commit, name only
git show <commit_hash> --name-status # see what files was modified in that commit, name and their status
git show <commit_hash> <file_path> # see commit diff for the file in that commit
git show <commit_hash>:<file_path> # show file content in specified commit, can redirect to save content
git show <other branch>:<file_path> # show file content in other branch
git show HEAD~4:src/main.c # show scr/main.c 4 commit ago
commit
git commit -m '<commit_msg>'
git commit -amend -m '<commit_msg>' # change last commit message
show history
git log <file_name> # show file history (only if file in working tree)
git log -- <file_name> # show file history, even for deleted/moved files
git log --follow <file_path> # include rename
git log --name-status -- '<file_pattern>' # show all history for files matched with pattern (inclued deleted files)
更多log相关的设置可以查看git alias中的内容
设置alias之后,上面的git log都可以替换为git ll,结果会更紧凑和直观一些
go back to history
revert/checkout/reset
git revert <commit hash> # 创建一个进行相反操作的提交,不会覆盖修改历史
git commit -amend -m'xxx' # 修改上一次的提交信息
git reset <commit hash> # 将index回到指定的commit状态,指定commit之后的历史都将被丢弃,若加上--hard选项,working tree也会被修改
git reset <commit hash> <file path> # 和上面一样,但仅限于指定的文件
git checkout <commit hash> [--] <file name path> # update index with commit hash version
git checkout <branch name> <file name paths> # replace files from other branch
git checkout <commit hash> # checkout to a 'detached HEAD' status,can inspect files and do experiment
git checkout <branch name> # checkout back to normal branch
git checkout -- <file name> # discard local change for file name
git checkout . # discard all local change
git checkout *.py # discard all py files
git clean -i / git clean -d -i # delete all untracked files/folders
Note:如果有和其他人共用branch,git reset慎用,最好仅在将新commit push到remote之前用
rebase
git rebase -i <commit hash> ^
# remember to backup your branch before re-base
# interactively modify history from a specific commit hash (not include that commit)
# can remove/keep/merge commit message/..., selectively replay or discard all commit
# drop/d option can remove specific commit in middle of history
# note: reset is to remove all commit after specific commit hash
git rebase <target branch name>
# 将当前分支上新的提交移动到目标分支
fix a commit via rebase (useful)
git commit --fixup <commit hash>
git rebase -i --autosquash <commit hash>^
以上命令适用场景:fix或者说replace一个之前的commit,而不是再commit一次,让commit history更整洁
step to handle conflict
merge the release branch to current branch, in the temp merge branch modify as required, then commit and push again
Note: 处理冲突还是推荐用图形化工具