软件版本:
操作系统:ubuntu10.04
内核版本:Linux version 2.6.32-36-generic
git 版本:git version 1.7.0.4
目录:
1. 文件状态
2. 提交
2.1 git commit 与 git commit -a
2.2 添加提交信息
3. 修改/取消
4. 参考资料
1. 文件状态
一般仓库中的文件可能存在于这三种状态:
1)Untracked files → 文件未被跟踪;
2)Changes to be committed → 文件已暂存,这是下次提交的内容;
3) Changes bu not updated → 文件被修改,但并没有添加到暂存区。如果 commit 时没有带 -a 选项,这个状态下的文件不会被提交。
$git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: file2
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: file
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# file3
2. 提交
git 提交的命令为:git commit 。
2.1 git commit 与 git commit -a
git commit 提交的是暂存区里面的内容,也就是 Changes to be committed 中的文件。
$git commit
[master 5b61c29] run git commit
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file2
$git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: file
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# file3
git commit -a 除了将暂存区里的文件提交外,还提交 Changes bu not updated 中的文件。
$git commit -a
[master bd77524] run git commit -a
1 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 file2
$git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# file3
2.2 添加提交信息
如果直接运行 git commit (-a) 则会默认使用 vi 添加描述。也可以使用 git config --global core.editor
命令更改为你喜欢的编辑器。还有一个方法就是使用 -m 选项直接添加提交信息。
$git commit -a -m "commit info"
3. 修改/取消
有时候我们会发现有几个文件漏了提交或者想修改一下提交信息,又或者忘记使用 -a 选项导致一些文件没有被提交,我们希望对上一次提交进行修改,或者说取消上一次提交,这时候我们需要使用 --amend 选项。
$git commit --amend
可以对上一次提交进行修改,比如我们发现漏了 file3 没有提交,我们可以运行一下操作:
$git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# file3
nothing added to commit but untracked files present (use "git add" to track)
$git add file3
$git commit --amend
[master 671f5cc] commit --amend, add file3
1 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 file2
create mode 100644 file3
$git status
# On branch master
nothing to commit (working directory clean)
又或者我们发现在提交时忘记使用 -a 选项,导致 Changes bu not updated 中的内容没有被提交,我们可以使用:
$git commit --amend -a
git 撤销commit
如果不小心commit了一个不需要commit的文件,可以对其进行撤销。
先使用git log 查看 commit日志
- commit 422bc088a7d6c5429f1d0760d008d86c505f4abe
- Author: zhyq0826 <zhyq0826@gmail.com>
- Date: Tue Sep 4 18:19:23 2012 +0800
- 删除最近搜索数目限制
- commit 8da0fd772c3acabd6e21e85287bdcfcfe8e74c85
- Merge: 461ac36 0283074
- Author: zhyq0826 <zhyq0826@gmail.com>
- Date: Tue Sep 4 18:16:09 2012 +0800
找到需要回退的那次commit的 哈希值,
git reset --hard commit_id
使用上面的命令进行回退
以下是豆瓣的
起因: 不小新把记录了公司服务器IP,账号,密码的文件提交到了git
方法:
git reset --hard <commit_id>
git push origin HEAD --force
其他:
根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:
git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息
git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可
git reset –hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容
HEAD 最近一个提交
HEAD^ 上一次
<commit_id> 每次commit的SHA1值. 可以用git log 看到,也可以在页面上commit标签页里找到