@Zjmainstay
2015-12-06T15:32:21.000000Z
字数 4540
阅读 2993
git 教程
git config --global user.email admin@zjmainstay.cngit config --global user.name "Zjmainstay"git config --list //查看配置信息git config --global --unset user.name //删除user.name
安装git,打开Git Bashssh-keygen -t rsa -C "admin@zjmainstay.cn"一路回车,之后会生成id_rsa与id_rsa.pubid_rsa.pub为公钥,把它贴到托管网站(如github, git@OSC)在http://git.oschina.net/keys添加一个公钥,把id_rsa.pub内容全部拷贝进去。使用:启用Git Bash,输入命令git clone `SSH地址`(别用HTTPS地址),在提示认证的时候输入yes,则可通过SSH认证访问
mkdir Git-Test //创建目录cd Git-Test //进入目录git init //初始化,自动生成.git目录touch README.md //创建README.md文件git add README.md //添加文件到提交索引git commit -m "first commit" //提交,提交信息为"first commit"git remote add origin https://git.oschina.net/Zjmainstay/Git-Test.git //增加一个远程服务器端版本库,名称为origingit push -u origin master //将本地master主枝代码更新到名为origin的远程版本库中
远程git链接本地简化
git remote add zjcaptcha git@git.oschina.net:Zjmainstay/Joomla-Captcha-Plugin.git
将本地git档案与远程同步
git push origin #将本地代码更新到名为origin的远程版本库中git push origin current-branch-name:remote-branch-name #将当前分支(current-branch-name)推送到远程版本库(remote-branch-name)中,remote-branch-name可以随意命名,当最好不要以“origin/”为前缀
将远程git档案与本地同步
git pull origin master #将origin这个版本库更新到本地的master主枝git pull #如果有track,可以直接这么操作
将这个URL地址的远程版本库完全克隆到本地some_project目录下面
git clone git://github.com/someone/some_project.git some_project
从当前工作空间和索引中删除文件
git rm app/some_files注意:要删除文件必须在git控制台中用上述命令删除一遍再同步到远程
查看历史日志
#普通版本git log#格式版本1(时间更人性化)git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cd [%cr]) %C(bold blue)<%an>%Creset' --abbrev-commit --date=iso --stat#格式化版本2git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative --stat
一次添加所有新增、修改文件
git add .注:使用`git add *` 可能报错:`The following paths are ignored by one of your .gitignore files:`git add path_to_file #添加一个文件git add path_to_dir #添加一个目录及其目录中所有内容
查看当前git预提交状态
git status
获取帮助文档
git help git
忽略文件
修改 .gitignore文件,加入文件如:dir1/file1.php.project
关于remote的操作
git remote --help 可以看到所有用法。
git config
git config --list可以看到所有的配置。git config --global 配置的参数,删除的时候也要带上global,如:git config --global --unset remote.origin.url普通参数,直接:git config --unset remote.origin.url 即可
下载一个tag
git tag -l 可以查看tag列表git clone urlgit checkout -b v1.0.1
回复rm删除的文件
git ls-files -d | xargs git checkout --
fork版本合并
(1)clone 自己账号里fork的分支$ git clone https://github.com/Zjmainstay/CurlMulti.git(2)增加远程原始分支到本地(可以用 git remote -v 命令查看远程分支列表)$ git remote -v$ git remote add ares333 https://github.com/ares333/CurlMulti.git(3)fetch原始源分支的新版本到本地$ git fetch ares333(4)合并两个版本的代码$ git merge ares333/master(5)把最新的代码提交到github自己的账号上$ git push origin master参考:http://www.tuicool.com/articles/MzMJre
移除 $ git config --system http.sslcainfo的设置
$ git config --system http.sslverify false
暂存修改 (最后一个index=0)
$ git stash
恢复暂存修改
# 查看所有的暂存数据$ git stash list#按index恢复$ git stash apply --index=0#恢复stash@{0}的数据(不会删除该暂存内容)或$ git stash pop#恢复最后一个stash,即stash@{0},同时删除暂存内容
查看暂存内容
$ git stash show stash@{0}
删除暂存内容
$ git stash drop stash@{0}
git比较
#比较两个分支git diff branch_name1 branch_name2#比较两个分支的一个文件git diff branch_name1 branch_name2 filepath_to_filename#比较两个提交(commit)git diff commit_id1 commit_id2注:git log 可以得到commit_id#比较两个提交(commit)的文件git diff commit_id1 commit_id2 filepath_to_filename
获取一个commit作为分支
#1. 获取commit_idgit log#2. 使用该提交创建新分支git co -b new_branch commit_id
修改内容回复
git co -- path_to_file
从某个分支或者commit中下载某个文件
git co branch-name path_to_filegit co commit-id path_to_file
删除本地分支
git br -D branch-name
删除远程分支
git push origin :remote-branch-name
git区分大小写
git config core.ignorecase false
使用git alias命令git st(参考这里的配置时提示如下错误:fatal: cannot exec git st: Permission denied参考stackoverflow上的解答, 使用如下方法解决.安装straceapt-get install strace使用strace执行git ststrace -f -e execve git st根据输出结果进行修正, 我的错误在于$PATH变量设置有误.来源: <http://blog.atime.me/note/git-problems.html>
error: there are still refs under 'refs/remotes/origin/app2.0'error: Cannot lock the ref 'refs/remotes/origin/app2.0'.From git.jiudouyu.com.cn:root/9douyu-server解决:git remote prune origin
.gitconfig.git-completion.bash (https://www.zybuluo.com/Zjmainstay/note/199893).gitignore_globalvi .bashrc 加入 source ~/.git-completion.bash
[user]name = Zjmainstayemail = admin@zjmainstay.cn[core]excludesfile = ~/.gitignore_globaleditor = /usr/bin/vim[mergetool "sourcetree"]cmd = /usr/local/bin/ksdiff --merge --output \"$MERGED\" --base \"$BASE\" -- \"$LOCAL\" --snapshot \"$REMOTE\" --snapshottrustExitCode = true[alias]co = checkoutci = commitst = statusbr = branchlg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cd [%cr]) %C(bold blue)<%an>%Creset' --abbrev-commit --date=iso --stat[color]branch = trueui = truediff = trueinteractive = truestatus = true