新手常用的git命令及问题分析

[TOC]

专有名词

  • Workspace:工作区
  • Index / Stage:暂存区
  • Repository:仓库区(或本地仓库)
  • Remote:远程仓库,指托管在网络上的项目仓库

.gitignore文件中列出要忽略的文件模式,例如:.class,忽略.class文件

image1

本地项目上传github

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 //设置用户
git config --global user.name
git config --global user.email
cd d:
mkdir bootstrap
cd bootstrap

git init //初始化版本仓库
touch READM //新建readme文件
echo "hello world!" >>README

git add . //将所有文件添加暂存区域
git reset file //取消已暂存文件
git commit -m 'first commit' //提交缓存,并添加描述‘first commit’
// 添加名字为origin的远程库,注意需提前建立好版本仓库,再次修改后提交跳过此命令
git remote add origin https://github.com/chen1218chen/bootstrap.git

git push origin master //提交到github仓库
git push //提交到local
git pull //抓取远程数据并自动合并
git fetch //抓取远程数据,需要手动合并

git clone <url> //自动将远程库归于origin下


git status //查看提交状态
git status -s //更为紧凑的格式输出
git status --short
git log -p -2//-p显示每次提交的内容差异,-2最近两次更新

git tag //列出所有tag
//新建一个tag在当前commit
git tag [tag]
//新建一个tag在指定commit
git tag [tag] [commit]
//查看tag信息
git show [tag]
//提交所有tag
$ git push [remote] --tags

// 新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]

免密码

git 配置SSH即可提交免密码

  1. 生成秘钥
ssh-keygen -t rsa -C "cc.2008.com@163.com"

按3个回车,密码为空。(不要输密码)。然后到.ssh文件夹下面将id_rsa.pub里的内容复制出来粘贴到github个人中心的账户设置的ssh key里面

  1. 设置用户名密码
$ git config --global user.name "chen1218chen"
$ git config --global user.email "cc.2008.com@163.com"

# 查看
git config --list
  1. 测试
ssh -T git@github.com 

成功提示:

Hi chen1218chen! You've successfully authenticated, but GitHub does not provide shell access.  

命令分析

git help

git log

提交记录过长,按q键退出

git mv

修改文件名:

git mv file_from file_to

git rm

删除文件

rm <file>
git rm <file>

git commit

git commit 提交时强制要求输入提交说明,不能省略,使用-m参数输入提交说明,如果没有,git会自动打开一个编辑器,要求在其中输入提交说明。
参数说明:
-m 输入提交说明
–amend 对刚刚的提交进行修补
–allow-empty 空白被允许提交
–reset-author 将author的ID同步修改

最后一次commit信息写错了

如果只是提交信息写错了信息,可以通过以下命令单独修改提交信息

git commit --amend

注意: 通过这样的过程修改提交信息后,相当于删除原来的提交,重新提交了一次。所有如果你在修改前已经将错误的那次提交push到服务端,那在修改后就需要通过 git pull 来合并代码(类似于两个分支了)。
通过 git log –graph –oneline 查看就会发现两个分支合并的痕迹

最后一次commit少添加一个文件

git add file1
git commit --amend

最后一次commit多添加一个文件

git rm --cached file1
git commit --amend

git clone

# 最简单的命令
git clone xxx.git

# clone到指定目录
git clone xxx.git "指定目录"

# clone时创建新的分支替代默认Origin HEAD(master)
git clone -b [new_branch_name]  xxx.git

git checkout

下载历史版本

# 查看历史版本的sha-1(也是commit版本号)
git log
# 获取
git checkout 75adcf5d3a33bb7266adfa7ae3d20ee6e841c68b

单个文件恢复到历史版本

git reset commit_id 文件路径
git checkout 文件路径

git push

推送数据到远程仓库

git push [remote-name] [branch-name]

git remote

git默认使用origin作为远程库名。master为默认主分支名,自动建立的,版本库初始化以后,默认在master主分支进行保存。

git remote //查看远程库名
git remote -v //查看远程库地址
git remte show [remote-name] //查看远程仓库的详细信息
git remote rename aa cc //修改远程仓库aa的名称
git remote rm cc  //删除远程仓库cc

删除远程仓库报错,如下图所示:

enter description here

原因是用法错误,git remote rm <主机名>用来删除添加的remote路径,正确用法如下图所示:

enter description here

常用命令:
enter description here

git config

git config --list //查看配置信息
git config -e  //版本库级配置文件
git config -e --global   //全局配置文件
git config -e --system  //系统级配置文件
git config --global core.editor
git config --global core.ui true //为终端的内容着色

user.name、user.email

删除全局配置中的user.name和user.email

1
2
git config --unset --global user.name
git config --unset --global user.email

这样一来,查看

1
2
git config user.name
git config user.email

将看不到输出。

git branch

enter description here

git tag

# 查看标签
git tag
# 打标签
git tag -a 'V1.1' -m 'first version'
# 轻量级标签
git tag V1.1

-a: 创建标签
-m: 存储标签信息

# 查看标签信息
git show V1.1
# 推送标签到远程服务器
git push origin V1.1
# 把所有不在远程仓库上的标签推送上去
git push origin --tags

# 补打标签
git log --pretty=oneline //查看版本号
git tag -a v1.2 9fceb02...(commit提交版本号)
git push origin v1.2 //提交tag

git reset

enter description here

git reset HEAD <file> //HEAD指针指向当前分支的最新的提交
git reflog  //查看所有日志包括撤销的操作
git reset --hard 2d87c9b  //回复到2d87c9b这个版本
git reset HEAD^ 
git reset HEAD^^
git reset HEAD~1
git reset HEAD~n

git merge

查看支持的工具

git mergetool --tool-help

enter description here
如果工具有效,设置merge.tool

git config --global merge.tool p4merge

设置路径

git config --global mergetool.p4merge.path c:/Users/my-login/AppData/Local/Perforce/p4merge.exe

案例

回退单个文件的历史版本

#查看历史版本
git log 1.txt

#回退该文件到指定版本
git reset [commit_id] 1.txt
git checkout 1.txt

#提交
git commit -m "回退1.txt的历史版本"

移除add过的文件

#方法一
git rm --cache [文件名]

#方法二
git reset head [文件/文件夹]

常见问题

error:failed to push som refs to

1
error:failed to push som refs to ......

解决方法:
1、先输入 git pull origin master ,先把远程服务器github上面的文件拉下来
2、再输入git push origin master

fatal: remote origin already exists.

1
fatal: remote origin already exists.

解决方法:
1、先输入 git remote rm origin
2、再输入git remote add origin https://github.com/chen1218chen/bootstrap.git 就不会报错了!

fatal: The remote end hung up unexpectedly

1
fatal: The remote end hung up unexpectedly

The problem is due to git/https buffer settings.
解决方法:

1
git config http.postBuffer 524288000

获取项目

1
git clone https://github.com/chne1218chen/bootstrap.git

更新时与本地文件冲突

报错:

error: Your local changes to the following files would be overwritten by merge:
        _config.yml
        layout/_partial/footer.ejs
Please, commit your changes or stash them before you can merge.
Aborting

解决方法:

# 暂存
git stash
git pull
# 恢复
git stash pop
# 查看暂存
git stash list
# 清空暂存
git stash clear
文章目录
  1. 1. 专有名词
  2. 2. 本地项目上传github
  3. 3. 免密码
  4. 4. 命令分析
    1. 4.1. git help
    2. 4.2. git log
    3. 4.3. git mv
    4. 4.4. git rm
    5. 4.5. git commit
      1. 4.5.1. 最后一次commit信息写错了
      2. 4.5.2. 最后一次commit少添加一个文件
      3. 4.5.3. 最后一次commit多添加一个文件
    6. 4.6. git clone
    7. 4.7. git checkout
      1. 4.7.1. 下载历史版本
      2. 4.7.2. 单个文件恢复到历史版本
    8. 4.8. git push
    9. 4.9. git remote
    10. 4.10. git config
      1. 4.10.1. user.name、user.email
    11. 4.11. git branch
    12. 4.12. git tag
    13. 4.13. git reset
    14. 4.14. git merge
  5. 5. 案例
    1. 5.1. 回退单个文件的历史版本
    2. 5.2. 移除add过的文件
  6. 6. 常见问题
    1. 6.1. error:failed to push som refs to
    2. 6.2. fatal: remote origin already exists.
    3. 6.3. fatal: The remote end hung up unexpectedly
    4. 6.4. 更新时与本地文件冲突