目录
1. 本地Git下载
需要借助 Git Bash 工具
地址 : msysgit官网
百度网盘 密码:aeie
2. 输入 Github 账户名字 和邮箱地址
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
3. 创建版本库
选择打开本机的一个文件夹,创建空文件
$ mkdir learngit
$ cd learngit
$ pwd
你选择的文件夹路径/learngit
通过git init命令把这个目录变成Git可以管理的仓库
$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/
4. 把文件放到Git仓库
第一步:创建文件(比如:readme.txt)
第二步:添加到仓库
$ git add readme.txt
第三步:提交文件到仓库
$ git commit -m "wrote a readme file"
[master 32c9a61] wrote a readme file
1 file changed, 2 insertions(+), 2 deletions(-)
-m 后面的内容,是本次提交的说明(可以同时提交多个文件)
5. 同步本地仓库到远程仓库Github
git push origin master
6. 查看更改
忘记之前做的改动时可用
先查看被改动的文件
$ 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: readme.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
TESTFILE/
project/
no changes added to commit (use "git add" and/or "git commit -a")
再查看被改动的内容
$ git diff readme.txt
warning: LF will be replaced by CRLF in readme.txt.
The file will have its original line endings in your working directory.
diff --git a/readme.txt b/readme.txt
index 7c9ec1d..c9b621f 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a distributed version contorl system
-Git is free software distributed
+Git is a distributed version contorl system.
+Git is free software.
之后就可以添加,提交了
7. 版本回退
查看历史记录
$ git log
commit 32c9a61278c58a2235266f97b6aff75a28fb7520 (HEAD -> master)
Author: Coding-life <15195865723@163.com>
Date: Sat Sep 22 09:48:29 2018 +0800
wrote a readme file
commit b7ab6b4bfa8a9660728696fcbc74ca9176b0ae82
Author: Coding-life <15195865723@163.com>
Date: Sat Sep 22 09:33:58 2018 +0800
append
commit ca2efbc3e2efb74a7e68372d30beec8d9e8dda3f
Author: Coding-life <15195865723@163.com>
Date: Tue Sep 18 16:33:47 2018 +0800
wrote a readme file
版本回退(HEAD表示当前版本,HEAD^——上一个,HEAD^^——上上个)
$ git reset --hard HEAD^
HEAD is now at b7ab6b4 append
恢复到之前的版本:需要输入 commit 中的 id 号(只要前面几位 id 号即可)
$ git reset --hard 32c9a
HEAD is now at 32c9a61 wrote a readme file
记录命令
$ git reflog
32c9a61 (HEAD -> master) HEAD@{0}: reset: moving to 32c9a
b7ab6b4 HEAD@{1}: reset: moving to HEAD^
32c9a61 (HEAD -> master) HEAD@{2}: reset: moving to HEAD^
55c1099 HEAD@{3}: commit: append GPL
32c9a61 (HEAD -> master) HEAD@{4}: commit: wrote a readme file
b7ab6b4 HEAD@{5}: commit: append
ca2efbc HEAD@{6}: reset: moving to HEAD
ca2efbc HEAD@{7}: commit (initial): wrote a readme file
8. 克隆项目到本地仓库
$ git clone 地址
地址获取方法
9. 更新代码
第一步:查看当前的git仓库状态,可以使用git status
git status
第二步:更新全部
git add *
第三步:接着输入git commit -m "更新说明"
git commit -m "更新说明"
第四步:先git pull,拉取当前分支最新代码
git pull
第五步:push到远程master分支上
git push origin master
不出意外,打开GitHub已经同步了
问题:
git add file时
warning: LF will be replaced by CRLF in read.txt.
The file will have its original line endings in your working directory.
设置转换工作区文件换行符就可以了
git checkout
push时报错不能覆盖远程仓库代码
事实上远程仓库是新建的,里边或许有个README.md,就再没有其他,故直接强制覆盖;-f
:强制覆盖。
git push -uf origin master