此处只列出常用的操作命令,具体详细命令介绍及参数说明请参看 Git官方手册。
git config --global user.name "[your name]"
git config --global user.email "[email address]"
git config --global color.ui auto
如果使用 SSH 方式连接远端,可以用密钥来实现免密登录。可以参考:CentOS 7 搭建 Git 服务器
HTTPS 方式得用凭证系统来处理。
git config --global credential.helper cache [--timeout <seconds>]
# cache 模式下凭证只会存放在内存中一段时间。密码永远不会被存储在磁盘中。
# 可以用 --timeout 指定过期时间,默认 15 分钟后从内存中清除。
git config --global credential.helper store [--file <path>]
# store 模式下凭证会以明文的形式存放在磁盘中,并且永远不会过期。
# 可以用 --file 指定密码文件的存放路径,默认为 ~/.git-credentials。
.git-credentials 中存储格式为:
一行存储一个账号密码
每行的格式为 协议+用户名+:+密码+主机名/IP
,如 https://user:password@domain.com
,用户名和密码需要使用 urlencode 编码,比如含有 @
的需要编码为 %40
。
git clone ssh://user@domain.com/repo.git
git clone http://domain.com/user/repo.git
git init
git status
git diff
取消追踪文件,并保留在本地:
git rm --cached <file>
取消追踪目录,并保留在本地:
git rm -r --cached <dir>
取消追踪文件,并且删除本地文件:
git rm --f <file>
git add .
git add <file>
git commit -a
git commit -m 'message here'
不要修改已发布的提交记录!
git commit -a --amend
git stash
git checkout branch2
git stash pop
git stash apply
{stash_number} 可以用命令 git stash list
获取
git stash apply stash@{stash_number}
git stash drop
git grep "Hello"
git grep "Hello" v2.5
git log
git log --oneline
git log --author="username"
git log -p <file>
git blame <file>
git reflog
git reflog delete
git branch
git branch -a
git branch -r
git checkout <branch> -- <filename>
git checkout -b <branch>
git checkout <commit-hash> -b <new-branch-name>
git branch <new-branch>
git branch --track <new-branch> <remote-branch>
git branch -d <branch>
git branch -m <new-branch-name>
将会丢失未合并的修改!
git branch -D <branch>
git tag <tag-name>
git tag -a <tag-name> -m 'message here'
git remote -v
git remote show <remote>
git remote add <remote> <url>
git fetch <remote>
git remote pull <remote> <url>
git pull origin master
git pull --rebase <remote> <branch>
git push remote <remote> <branch>
git push <remote> --delete <branch>
git push --tags
git merge <branch>
git add <resolved-file>
git rm <resolved-file>
git reset --hard HEAD
git reset HEAD
git checkout HEAD <file>
git revert <commit>
git reset --hard <commit>
git reset --hard <remote/branch> e.g., upstream/master, origin/my-feature
git reset <commit>
git reset --keep <commit>
git rm -r --cached .
git add .
git commit -m 'remove xyz file'