在GitHub上创建一个仓库
先注册一个GitHub账号,官网:
https://github.com,或者在码云(https://gitee.com)、Coding(https://coding.net)上注册页可以新建项目(创建仓库),创建完成后会得到仓库地址,我的项目名是
test。如:https://github.com/你的用户名/test.git
将新建的仓库克隆到本地
打开终端并切换到指定目录(你想将项目放在哪个目录下)
1
$: cd Web/Git/
通过
pwd命令查看路径为:/Users/phoenix/Web/Git
注:我的是 MAC 环境克隆项目
1
$: git clone https://github.com/你的用户名/test.git
克隆完成后在我的 Git 文件夹下多了一个 test 文件夹。
提交修改到 master 分支
初健项目时就有一个master分支(一般为主分支),且只有一个master分支
在test文件夹(项目文件夹)下新建一个
test1.txt文件,里面填写内容‘第一次提交’使用
git status命令可以看到工作目录和暂存区的状态1
2
3
4
5
6
7No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test1.txt
nothing added to commit but untracked files present (use "git add" to track)要将新添加的文件(或新修改的内容)提交到远端仓库,就要用
git add命令将要提交的内容添加到暂存区(staging area)1
2git add test1.txt # git add 文件名 ,是将指定文件添加进暂存区
git add . # 是将所有改动的内容(或新增文件)添加进暂存区将
test1.txt文件添加到暂存区后在使用git status命令查看状态,得提示:1
2
3
4
5
6No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test1.txt使用
git rm命令可以将暂存区里的内容移除1
2git rm -f <file>: 强制从staging区移除文件,同时也移除出工作目录.
git rm --cachedf <file>: 从staging区移除文件,但留在工作目录中.运行命令
git rm --cached test1.txt后在运行命令git status, 得提示:1
2
3
4
5
6
7No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test1.txt
nothing added to commit but untracked files present (use "git add" to track)说明:
git rm --cached <file>从staging区移除文件,但留在工作目录中如果运行
git rm -f test1.txt, 那么项目文件夹下也没有 test1.txt 文件了,因为git rm -f <file>强制从staging区移除文件,同时也移除出工作目录.重新来一次,将文件添加到暂存区后不移除。使用
git commit提交已经被add进来的改动.1
2
3git commit -m “the commit message"
git commit -a 会先把所有已经track的文件的改动add进来,然后提交(有点像svn的一次提交,不用先暂存). 对于没有track的文件,还是需要git add一下.
git commit --amend 增补提交. 会使用与当前提交节点相同的父节点进行一次新的提交,旧的提交将会被取消.使用
git commit -m '第一次提交'提交,得提示:1
2
3[master (root-commit) 728a3eb] 第一次提交
1 file changed, 1 insertion(+)
create mode 100644 test1.txt使用
git push命令吧提交的内容推送到远端1
2
3
4
5
6git push # push所有分支
git push origin master # 将本地主分支推到远程主分支
git push -u origin master # 将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库)
git push origin <local_branch> # 创建远程分支, origin是远程仓库名
git push origin <local_branch>:<remote_branch> # 创建远程分支
git push origin :<remote_branch> #先删除本地分支(git br -d <branch>),然后再push删除远程分支使用
git push origin master命令,得提示:1
2
3
4
5
6
7
8
9
10
11
121 file changed, 1 insertion(+)
create mode 100644 test1.txt
PhoenixdeMacBook-Air:test phoenix$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 241 bytes | 120.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote: https://github.com/frPhoenix/test/pull/new/master
remote:
To https://github.com/frPhoenix/test.git
* [new branch] master -> master现在去刷新你的远端仓库就可以看到 test1.txt 文件了
创建分支切换分支
- 使用
git branch创建分支1
git branch <new_branch> # 创建新的分支
我这里创建一个test1分支,git branch test1
- 查看分支使用
git branch1
git branch
此时得到分支信息:1
2
3$ git branch
* master
test1
其中master是自动创建的主分支,test1是刚才新创建的。*号在哪个分支前面代表此时在那个分支上。
- 使用
git checkout切换分支1
git checkout <branch_name> # 切换分支
切换到test1分支使用git checkout test1,然后在使用git branch查看分支情况,得:1
2
3$ git branch
master
* test1
创建新分支并切换到新分支,使用
git checkout -b <branch_name>,如创建并切换到test2分支,git checkout -b test2
使用git branch查看分支情况,得:1
2
3
4$ git branch
master
test1
* test2查看远程分支使用,
git branch -r,得:1
2$ git branch -r
origin/master
因为没有向远端创建过别的分支,所以远端还只有master分支
操作远端分支
将分支切换到test1
- 修改文件
test1.txt的内容,添加这是添加到test1分支的内容 - 使用
git status查看得到:1
2
3
4
5
6
7
8On branch test1
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: test1.txt
no changes added to commit (use "git add" and/or "git commit -a")
其中,git checkout -- <file> 是丢弃本次修改的命令,如果执行该命令你会发现刚才添加的内容没有了
- 添加好
这是添加到test1分支的内容内容后使用git add,git commit命令提交修改,然后是一个知识点:
- 如果直接使用
git push命令(不再该命令后添加任何内容),不会上传成功,会得到提示:1
2
3
4fatal: The current branch test1 has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin test1
因为远端还没有 test1 分支,所以直接上传会出错。此时应该使用另外一个命令
- 使用
git push origin test1命令,会先在远端创建test1分支然后再上传,上传成功后的提示信息中有这样的信息:1
2To https://github.com/frPhoenix/test.git
* [new branch] test1 -> test1
第一行是上传的仓库地址,第二行是本地的哪个分支推送到了远端的哪个分支。
- 直接使用
git push是将本地的分支上传到远程的同名分支,也就是远端先创建有一个test1分支,然后使用git push将本地test1分支推送到远端test1分支
比较两个分支git diff
- 查看两个分支已经提交的内容的不同
1
git diff <branchA> ... <branchB>
我们这里使用git diff master ... test1,得到:1
2
3
4
5
6
7
8
9--- a/test1.txt
+++ b/test1.txt
@@@@@ -1,5 -1,5 -1,5 -1,5 +1,3 @@@@@
第一次提交
----这是添加到test1分支的内容
----
----使用push,test1分支的内容
++++使用push,master分支的内容
- 查看工作区的内容与分支有什么不同,使用
git diff HEAD
查看提交、撤销提交
- 查看提交情况使用
git log命令1
2
3
4git log <file> # 查看该文件每次提交记录
git log -p <file> # 查看每次详细修改内容的diff
git log -p -2 # 查看最近两次详细修改内容的diff
git log --oneline --graph:可以图形化地表示出分支合并历史.
使用git log --oneline --graph能够得到1
2
3
4* 2a21b2b (HEAD -> test1) 使用push推送
* 243db38 推动到master、test1分支
* 8141a38 推到test1分支
* 728a3eb (test2) 第一次提交
- 使用
git revert撤回提交1
2
3git revert HEAD: 撤销最近的一个提交.
git revert会创建一个反向的新提交,可以通过参数-n来告诉Git先不要提交.
git revert <$id> # 恢复某次提交的状态,恢复动作本身也创建次提交对象
其中<$id>指的是提交的版本编号,如上面得到的2a21b2b。
操作stash 缓存
1 | git stash将会把当前目录和index中的所有改动(但不包括未track的文件)压入一个栈,然后留给你一个clean的工作状态,即处于上一次最新提交处. |
合并分支内容
将master分支上的内容合并到test1分支上
- 使用
git fetch origin <branch>1
git fetch origin master:tmp
//在本地新建一个temp分支,并将远程origin仓库的master分支代码下载到本地temp分支
再使用merge,git merge tmp合并分支,这时可能会有冲突,(删除tmp分支:git branch -d temp)
撤销合并:1
git reset --hard
使用
git pull origin <branch>
从远端拉取内容并合并,合并完成之后需要一次新的提交。使用
git rebase1
2git rebase <branch>
git rebase --continue
查看分支之间的关系图
1 | git log --graph --decorate --oneline --simplify-by-decoration --all |
说明:
–decorate 标记会让git log显示每个commit的引用(如:分支、tag等)
–oneline 一行显示
–simplify-by-decoration 只显示被branch或tag引用的commit
–all 表示显示所有的branch,这里也可以选择,比如我指向显示分支ABC的关系,则将–all替换为branchA branchB branchC