您的位置:首页 > 其它

Git命令

2016-03-02 11:24 232 查看
我的GitHub

全局配置

//安装Git后的第一步,配置用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
//删除配置
git config --unset --global <config>


创建版本库

//初始化本地版本库
git init

//克隆远程版本库到本地
git clone <url>


“.gitignore”文件

#我是注释,git会忽视这里所选择的文件或文件夹
# 忽略*.o和*.a文件
*.[oa]
# 忽略*.b和*.B文件,my.b除外
*.[bB]
!my.b
# 忽略dbg文件和dbg目录
dbg
# 只忽略dbg目录,不忽略dbg文件
dbg/
# 只忽略dbg文件,不忽略dbg目录
dbg
!dbg/
# 只忽略当前目录下的dbg文件和目录,子目录的dbg不在忽略范围内
/dbg


修改和提交

//跟踪文件
git add .    //跟踪所有改动过的文件
git add <file>    //跟踪指定的文件
git add -f <file>    //强制添加.gitignore忽视的文件

//提交
git commit -m "commit message"    //提交跟踪过的文件并提示
git commit --amend    //修改最后一次提交

//查看变更内容
git diff

//查看状态
git status

//删除
git rm <file>    //删除工作区的文件
git rm --cached <file>    //停止跟踪文件但不删除

//文件改名
git mv <old> <new>


查看提交历史

//查看提交历史
git log    //查看所有提交历史
git log -p <file>    //查看指定文件的提交历史
git log --pretty=oneline    //一行显示历史记录

//查看所有提交记录(含版本号)
git reflog

//以列表方式查看指定文件的提交历史
git blame <file>


撤销

//撤销工作目录中所有未提交文件的修改内容
git reset --hard HEAD^    //有多少个^返回多少版本
git reset --hard HEAD~<number>   //返回前第<number>个版本
git reset --hard <commit_id>    //返回<commit_id>这个版本
git reset HEAD <file>    //把暂存区的修改撤销掉,重新放回工作区

//撤销未`git add`的文件
git checkout -- <file>    //用版本库里的版本替换工作区的版本
git checkout HEAD <file>    //撤销指定的未提交文件的修改内容

//还原
git revert <commit_id>    //撤销指定的提交


分支与标签

//分支
git branch   //显示所有本地分支
git branch <branch_name>   //创建<branch_name>分支
git branch -d <branch_name>   //删除<branch_name>分支

//切换
git checkout <branch_name>   //切换到<branch_name>分支
git checkout <tag_name>   //切换到<tag_name>标签

//标签
git tag   //显示所有本地标签
git tag <tag_name>   //基于最新提交创建标签
git tag -d <tag_name>   //删除<tag_name>标签


合并与衍合

//合并
git merge <branch_name>   //"暴力"合并指定分支到当前分支,不管有无差别

//衍合
git rebase <branch_name>   //一件件衍合指定分支到当前分支,有差别时提示


远程操作

//添加远程库之前需要添加SSH密匙
ssh-keygen -t rsa -C "youremail@example.com"

//远程库
git remote add remoteName git@github.com:YourGitHubName/remoteName.git   //添加远程库
git remote -v   //查看远程版本库信息
git remote show <remote>   //查看指定远程版本库信息
git remote rm remoteName    //删除远程库

//从远程库获取代码
git fetch <remote>

//下载代码及快速合并
git pull <remote> <branch>

//上传代码及快速合并
git push -u <remote> <branch>   //使用-u选项指定一个默认主机,这样后面就可以不加任何参数使用git push
git push <remote> <branch>   //将本地master分支推送到<remote>的<branch>分支
git push <remote>:<branch/tag>   //删除远程分支或标签
git push --tags   //上传所有标签
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: