1. ubuntu安装git命令行工具:
sudo apt-get install git-core
2. git常用命令
# 安装好之后输入 git 回车
git
输入命令会显示:
usage: git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
这些是各种场合常见的 Git 命令:
开始一个工作区(参见:git help tutorial)
clone 克隆一个仓库到一个新目录
init 创建一个空的 Git 仓库或重新初始化一个已存在的仓库
在当前变更上工作(参见:git help everyday)
add 添加文件内容至索引
mv 移动或重命名一个文件、目录或符号链接
reset 重置当前 HEAD 到指定状态
rm 从工作区和索引中删除文件
检查历史和状态(参见:git help revisions)
bisect 通过二分查找定位引入 bug 的提交
grep 输出和模式匹配的行
log 显示提交日志
show 显示各种类型的对象
status 显示工作区状态
扩展、标记和调校您的历史记录
branch 列出、创建或删除分支
checkout 切换分支或恢复工作区文件
commit 记录变更到仓库
diff 显示提交之间、提交和工作区之间等的差异
merge 合并两个或更多开发历史
rebase 本地提交转移至更新后的上游分支中
tag 创建、列出、删除或校验一个 GPG 签名的标签对象
协同(参见:git help workflows)
fetch 从另外一个仓库下载对象和引用
pull 获取并整合另外的仓库或一个本地分支
push 更新远程引用和相关的对象
命令 'git help -a' 和 'git help -g' 显示可用的子命令和一些概念帮助。
查看 'git help <命令>' 或 'git help <概念>' 以获取给定子命令或概念的
帮助。
3. 配置
git config --list
会显示当前的配置情况:
××××××××××
××××××××××
push.default=matching
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
省略了两行,是我自己的git和邮箱。
4.创建git库
# 在当前文件夹下初始化一个 Git 资料库
git init
则会在当前文件夹下面创建一个隐藏文件.git
初始化空的 Git 仓库于 /home/ysqyulyz/project/.git/
# 跳转到.git文件下
~/project$ cd ./.git/
# 创建3个文件
~/project/.git$ touch test01
~/project/.git$ touch test02
~/project/.git$ touch test03
# 创建一个文件夹
~/project/.git$ mkdir workfile
# 创建一个文件
~/project/.git$ touch workfile/data.txt
# 输出重定向到test01
~/project/.git$ ls > test01
这时候,test01里面的内容为:
branches
config
description
HEAD
hooks
info
objects
refs
test01
test02
test03
workfile
5.查看库状态
git status
可能会出现以下错误:
fatal: This operation must be run in a work tree
解决方法如下:
1. touch Readme
2. git init
提示也已经告诉为什么了,git的操作只能在工作树里,也就是只能在包含.git的文件里才能查看当前git的状态。
成功的话就会显示以下:
位于分支 master
初始提交
要提交的变更:
(使用 "git rm --cached <文件>..." 以取消暂存)
新文件: Readme
未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)
HEAD
config
description
hooks/
info/
6. 添加文件到git索引
一般,像上面那个一样,为跟踪的文件的改变是不会修改到git里面的,也就是和这个git并没有多大的关系,可通过以下命令加入git中:
git add .
将当前文件夹下面的文件全部添加进去,然后再查看git的status:
位于分支 master
初始提交
要提交的变更:
(使用 "git rm --cached <文件>..." 以取消暂存)
新文件: HEAD
新文件: Readme
新文件: config
新文件: description
新文件: hooks/applypatch-msg.sample
新文件: hooks/commit-msg.sample
新文件: hooks/post-update.sample
新文件: hooks/pre-applypatch.sample
新文件: hooks/pre-commit.sample
新文件: hooks/pre-push.sample
新文件: hooks/pre-rebase.sample
新文件: hooks/prepare-commit-msg.sample
新文件: hooks/update.sample
新文件: info/exclude
这样文件就会暂时缓存在本地的git里面。如果要添加单个文件,可通过以下命令:
git add 文件绝对路径
7. 正式提交到git
确定好了之后,你就可以正式提交了。这个操作会添加一个Git库中所有文件的快照。
# 正式提交变更到本地资料库
# 同时附上标注信息 “项目初始化”
git commit -m "项目初始化"
输出如下:
[master (根提交) 601db1e] initial project
14 files changed, 509 insertions(+)
create mode 100644 HEAD
create mode 100644 Readme
create mode 100644 config
create mode 100644 description
create mode 100755 hooks/applypatch-msg.sample
create mode 100755 hooks/commit-msg.sample
create mode 100755 hooks/post-update.sample
create mode 100755 hooks/pre-applypatch.sample
create mode 100755 hooks/pre-commit.sample
create mode 100755 hooks/pre-push.sample
create mode 100755 hooks/pre-rebase.sample
create mode 100755 hooks/prepare-commit-msg.sample
create mode 100755 hooks/update.sample
create mode 100644 info/exclude
8. 操作日志
我们可以通过查看日志来确定我们刚才的提交:
git log
输出如下:
Author: ×××× <××××.com>
Date: Fri Mar 23 18:30:55 2018 +0800
initial project
9. 文件的删除
touch delete_try,txt
git add . && git commit -m '测试'
git rm delete_try,txt && git commit -m '测试'
git log
输出:
rm 'delete_try,txt'
[master 15be80c] 测试
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 delete_try,txt
10. 文件的上传
先创建本地git,方法在上面,然后链接远程git仓库:
然后给仓库添加远程地址,输入命令: git remote add origin git@github.com:yourName/yourRepo.git
yourName是你的用户名,yourRepo是你的仓库名。例如我的就是GooZy/Codes.git
这个网址可以在github网页上点击项目那里的网站地址就是这个。
更新本地git和远程的git同步:
git pull --rebase origin master
之后提交到远程服务器:
git push -u origin +master
之后就等待上传,然后就会输出:
对象计数中: 19, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (17/17), 完成.
写入对象中: 100% (19/19), 8.92 KiB | 0 bytes/s, 完成.
Total 19 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/****
+ 10bf68b...c9fc541 master -> master (forced update)
分支 master 设置为跟踪来自 origin 的远程分支 master。
之后就完成了。
参考网址:
https://blog.csdn.net/dream_follower/article/details/53907217
https://blog.csdn.net/u013093948/article/details/53218306
https://blog.csdn.net/CatGlory/article/details/50444046
(还有一个很重要的忘记截了= =)