Git常用命令
- git init #使用当前目录作为Git仓库
git init newrepo #使用指定目录作为Git仓库 - git clone git.example.com:repositories/hello.git #获取
- git checkout -b branchname #创建并切换到分支
git checkout -f $filename #还原被修改文件
git checkout -f && git clean -df #清除当前所有修改 - git branch -D branchname #删除分支
git branch -a #查看所有分支 - git add/rm #从本地仓库增删,结果将会保存到本机的缓存里面
git add -A #提交全部修改 - git status #查看状态
- git commit -m “注释” #提交,把本机缓存中的内容提交到本机的 HEAD 里面
- git pull origin master #从远程仓库中下载新的改动
git push origin master #把本地的 commit(提交) push 到远程服务器上 - git log #查看提交信息
git log -n -2 #查看最近两次的提交 - git whatchhange #查看每个提交修改的文件
- git reset HEAD read.md #还原已经提交的文件
- git remote -v #查看下载地址
- git commit --amend -m #“修改 提交 说明” 修改最后一次提交注释
- git reset --hard “commit ID号” #回退到指定版本
- 标签操作
- git tag -a 0.1.3 -m “Release version 0.1.3” #详解:git tag 是命令, -a 0.1.3是增加 名为0.1.3的标签, -m 后面跟着的是标签的注释
- git push origin --tags #提交标签到远程服务器上 –tags参数表示提交所有tag至服务器端,普通的git push origin master操作不会推送标签到服务器端。
- git tag -d 0.1.3 #删除标签的命令
- git push origin :refs/tags/0.1.3 #删除远端服务器的标签
- git show v1.0.0 #查看 tag 详情
- git cherry-pick #合并某次提交到另一个分支(必须同一个仓库)
- git fetch <远程主机名> #将某个远程主机的更新,全部取回本地
- git rebase #从父线上拉下来最新的代码到自己的分支
- git merge branch_name #从子分支的修改合并到父线上
- git submodule #开发过程中,经常会有一些通用的部分希望抽取出来做成一个公共库来提供给别的工程来使用,而此命令可以管理公共代码库的版本
- git submodule add 仓库地址 路径 #为当前工程添
其中,仓库地址是指子模块仓库地址,路径指将子模块放置在当前工程下的路径。注意:路径不能以 / 结尾(会造成修改不生效)、不能是现有工程已有的目录(不能順利 Clone) - git rm –cached #删除
submodule的删除稍微麻烦点:首先,要在“.gitmodules”文件中删除相应配置信息。然后,执行“git rm –cached ”命令将子模块所在的文件从git中删除 - git submodule update --init --recursive #下载的工程带有submodule
当使用git clone下来的工程中带有submodule时,初始的时候,submodule的内容并不会自动下载下来的,此时,只需执行此命令
- 直接Clone连接时,设置记住密码
- git config –global credential.helper cache #设置记住密码(默认15分钟)
- git config credential.helper ‘cache –timeout=3600’ #自己设置时间
- git config –global credential.helper store #长期记住密码
- 忽略提交某个指定的文件(不从版本库中删除)
- git update-index --assume-unchanged test.c (忽略提交)
- git update-index --no-assume-unchanged test.c (取消忽略指令)
- 差分包制作
- git archive -o test.zip HEAD $(git diff ID1 ID2 --name-only)
- 推送所有分支
- git push --all origin -u
- 待续 …
Git安装
-
安装git
ubuntu: sudo apt-get install git
windows: 下载git安装 -
打开终端
ubuntu: 直接打开终端
windows: 在git工作目录下—右键—Git Base Here -
配置本机git的两个重要信息,user.name和user.email,中终端输入如下命令即可设置
git config --global user.name “hanbo” #用户名
git config --global user.email “[email protected]” #用户邮箱
git config --list #查看是否设置成功。 -
配置ssh key上传代码时使用这个 sshkey 来确认是否有上传权限
ssh-keygen -t rsa -C “[email protected]” #创建ssh
会生成.ssh目录中两个文件id_rsa和id_rsa.pub。id_rsa:私钥;id_rsa.pub:公钥,打开id_rsa.pub赋值里面的内容;
ubuntu路径/home/username/.ssh/id_rsa.pub
windows路径C:\Documents and Settings\Administrator.ssh\id_rsa.pub -
进入你的github,进入Profile Settings->SSH keys->Add SSH Keys,然后在Key那栏下面将公钥拷贝的内容粘贴进去,最后点击 Add key按钮添加。
-
验证:ssh -T [email protected]
出现 Hi xxx! You’ve successfully authenticated, but GitHub does not provide shell access. 说明配置成功 -
如果使用局域网域名,则需要添加本地域名解析
ubuntu: vi /etc/hosts
windows: 管理员打开C:\Windows\System32\drivers\etc\hosts在最后添加: 192.168.1.11 git.example.com #IP 域名