<!-- .hkb-article__content -->
管理本地仓库文件
简单的描述,在 Git 中文件有三种状态:已修改(modified
)、已暂存(staged
)、已提交(committed
) ;用 Git 管理文件也可以简单的理解为三个步骤:修改文件、跟踪文件、提交文件 。
以下采取示例的方式简述用 Git 管理本地仓库文件。
修改文件
修改文件即是对文件的添加、编辑、删除等等,和普通修改文件的方法一致。
选择合适的地方创建一个新目录『learn-git』,新建『readme.txt』 和『learn-git.txt』 文件,在中写入『I’m learning git.』这句话并保存。
跟踪文件(git add
)
创建文件和修改文件后需要把文件添加到仓库,即对文件进行跟踪。一次性把一个文件或多个文件添加到仓库都可以,用到的命令都是 git add
。
添加一个文件 时直接在终端输入 git add
后面空一格输入完整的文件名(包含后缀,如.txt):
$ git add readme.txt
添加多个文件 也类似,git add
后面空格输入完整的文件名,文件名之间用空格分隔:
$ git add readme.txt learn_git.txt
添加当前仓库里的所有文件 时直接在终端输入$ git add .
,注意此处末尾的.
不要遗漏。
提交文件(git commit
)
用git commit
命令把文件提交到仓库,一次性会提交所有你已经添加的文件:
$ git commit -m "wrote a readme and a learn_git file"
[master (root-commit) 7c57f05] wrote a readme and a learn_git file
2 files changed, 2 insertions(+)
create mode 100644 learn_git.txt
create mode 100644 readme.txt
提交命令为git commit
,-m
后面引号中的内容是你的提交说明,下面几行是终端的返回结果。每次提交文件时都写个提交说明,以便清楚地了解做了什么修改。
$
符号是使用终端时自动输入的,用户并不需要输入此符号。
查看文件状态(git status
)
如何知道一个文件处于哪种状态?使用git statu
命令查看文件状态。
当前仓库里任何文件都没有被跟踪时 返回结果如下:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'
nothing to commit, working directory clean
当文件已跟踪但没有提交到仓库时 返回结果如下:
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: learn_git.txt
modified: readme.txt
no changes added to commit (use “git add” and/or “git commit -a”)
当文件已跟踪且已经提交到仓库时 返回结果如下:
$ git status
On branch master
nothing to commit, working tree clean
使用 Git 管理文件时,每次结束工作前请依次执行git add
和git commit
命令将文件提交到仓库。
随着用户深入使用 Git,会了解到更多 Git 知识,点击查看 Git 文件状态介绍 和 Git 常用命令速查表 。
推送文件到远程仓库
注册 Coding 账户 浏览器中打开 Coding.net 注册账户,如有问题请看 帮助文档 。
创建项目 查看 创建项目帮助文档 ,不同等级的会员可拥有的项目数量不等,查看 会员等级和权益 。
添加远程仓库 查看 添加远程仓库帮助 。
推送代码到远程仓库 在终端运行命令git push
,将文件推送到远程仓库:
$ git push origin master
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (8/8), 626 bytes | 626.00 KiB/s, done.
Total 8 (delta 0), reused 0 (delta 0)
To https://git.coding.net/Yangconghou/learn_git.git
* [new branch] master -> master
git push
是推送命令,实际上是把本地的master
分支推送到了远程仓库,相当于在远程有了一个代码仓库的备份。
使用 Git 管理文件时,每次结束工作前请依次执行git add
、git commit
和git push
命令将文件推送到 Coding 远程仓库。
Git 文件状态介绍
已修改(modified) ———— 表示修改了文件,但还没保存到数据库中已暂存(staged) ———— 表示对一个已修改文件的当前版本做了追踪,使之包含在下次提交的快照中已提交(committed) ———— 表示数据已经安全的保存在本地数据库中
初次克隆某个仓库时,工作目录中的所有文件都属于已跟踪文件,且状态为未修改。 在编辑过某些文件之后,Git 将这些文件标为已修改。我们逐步把这些修改过的文件放到暂存区域,直到最后一次性提交所有这些暂存起来的文件,如此重复。使用 Git 时的文件状态变化周期如图所示。
Git 常用命令速查表
创建版本库
$ git clone <url> #克隆远程版本库
$ git init #初始化本地版本库
修改和提交
$ git status #查看状态
$ git diff #查看变更内容
$ git add . #跟踪所有改动过的文件
$ git add <file> #跟踪指定的文件
$ git mv <old><new> #文件改名
$ git rm<file> #删除文件
$ git rm --cached<file> #停止跟踪文件但不删除
$ git commit -m "commit messages" #提交所有更新过的文件
$ git commit --amend #修改最后一次改动
查看提交历史
$ git log #查看提交历史
$ git log -p <file> #查看指定文件的提交历史
$ git blame <file> #以列表方式查看指定文件的提交历史
撤销
$ git reset --hard HEAD #撤销工作目录中所有未提交文件的修改内容
$ git checkout HEAD <file> #撤销指定的未提交文件的修改内容
$ git revert <commit> #撤销指定的提交
$ git log --before="1 days" #退回到之前1天的版本
分支与标签
$ git branch #显示所有本地分支
$ git checkout <branch/tag> #切换到指定分支和标签
$ git branch <new-branch> #创建新分支
$ git branch -d <branch> #删除本地分支
$ git tag #列出所有本地标签
$ git tag <tagname> #基于最新提交创建标签
$ git tag -d <tagname> #删除标签
合并与衍合
$ git merge <branch> #合并指定分支到当前分支
$ git rebase <branch> #衍合指定分支到当前分支
远程操作
$ git remote -v #查看远程版本库信息
$ git remote show <remote> #查看指定远程版本库信息
$ git remote add <remote> <url> #添加远程版本库
$ git fetch <remote> #从远程库获取代码
$ git pull <remote> <branch> #下载代码及快速合并
$ git push <remote> <branch> #上传代码及快速合并
$ git push <remote> :<branch/tag-name> #删除远程分支或标签
$ git push --tags #上传所有标签
更多内容请查看 Git 文档 。