Git基本使用
Git的其他知识就不说了直接上操作吧
Git官网
https://git-scm.com/
Git基础操作
初始化
先在自己的工作区创建一个文件夹:
aaa@qq.com-TAGH8QP MINGW64 /d
$ mkdir testGitDiv
aaa@qq.com-TAGH8QP MINGW64 /d
$ cd testGitDiv/
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv
$ pwd
/d/testGitDiv
走到该目录下用git init命令进行初始化并用ls -la查看,多了.git的隐藏文件:
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv
$ git init
Initialized empty Git repository in D:/testGitDiv/.git/
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ ls -la
total 12
drwxr-xr-x 1 DELL 197121 0 11月 19 14:52 ./
drwxr-xr-x 1 DELL 197121 0 11月 19 14:52 ../
drwxr-xr-x 1 DELL 197121 0 11月 19 14:52 .git/
设置签名
设置签名用(项目级别/仓库级别:仅在当前本地库范围内有效)
git config user.name xxxx
git config user.email xxxx
系统用户级别:登录当前操作系统的用户范围
git config --global user.name xxxx
git config --global user.eamil xxxx
注意:项目级别优先于系统用户级别,二者都有时采用项目级别的签名
如果只有系统用户级别的签名,就以系统用户级别的签名为准,二者都没有不允许
这儿我们用局部签名就好了
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git config user.name eugene
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git config user.email aaa@qq.com.com
查看工作区、暂存区状态
用git status查看状态,显示啥都没有,现在创建一个test.txt的文件,再查看状态提示用git add添加到暂存区
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ vim test.txt
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ ls
test.txt
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
然后我们用git add XXX添加文件到暂存区,再查看状态时test.txt变成绿色,我们用git commit 进行提交后,再看状态提示没有需要提交的东西。
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test.txt
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git commit -m "my test First commit" test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory
[master (root-commit) 4b51a42] my test First commit
1 file changed, 3 insertions(+)
create mode 100644 test.txt
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git status
On branch master
nothing to commit, working tree clean
查看历史纪录
1.git log
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git log
commit 65d08acfcca8402f0c7cb0d3cb8474e5d0f0434a (HEAD -> master)
Author: eugene <aaa@qq.com.com>
Date: Tue Nov 19 14:59:12 2019 +0800
my three commit
commit de93b02ec4464e009dfd240ea263ffd9d2862929
Author: eugene <aaa@qq.com.com>
Date: Tue Nov 19 14:58:01 2019 +0800
my two commit
commit 4b51a42cf1a04e4744e8f8c8ae78616a08013531
Author: eugene <aaa@qq.com.com>
Date: Tue Nov 19 14:56:11 2019 +0800
my test First commit
2.git log --pretty=oneline
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git log --pretty=oneline
65d08acfcca8402f0c7cb0d3cb8474e5d0f0434a (HEAD -> master) my three commit
de93b02ec4464e009dfd240ea263ffd9d2862929 my two commit
4b51a42cf1a04e4744e8f8c8ae78616a08013531 my test First commit
3.git reflog
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git reflog
65d08ac (HEAD -> master) aaa@qq.com{0}: commit: my three commit
de93b02 aaa@qq.com{1}: commit: my two commit
4b51a42 aaa@qq.com{2}: commit (initial): my test First commit
前进和后退(git reset --hard [索引值] )
继续修改重复提交几次以后查看记录
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git reflog
e8e8a7d (HEAD -> master) aaa@qq.com{0}: commit: my four commit
65d08ac aaa@qq.com{1}: commit: my three commit
de93b02 aaa@qq.com{2}: commit: my two commit
4b51a42 aaa@qq.com{3}: commit (initial): my test First commit
#回到第二次提交
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git reset --hard de93b02
HEAD is now at de93b02 my two commit
#再查看记录
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git reflog
de93b02 (HEAD -> master) aaa@qq.com{0}: reset: moving to de93b02
e8e8a7d aaa@qq.com{1}: commit: my four commit
65d08ac aaa@qq.com{2}: commit: my three commit
de93b02 (HEAD -> master) aaa@qq.com{3}: commit: my two commit
4b51a42 aaa@qq.com{4}: commit (initial): my test First commit
分支
创建分支(git branch [分支名])
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git branch test_fix
查看分支(git branch -v)
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git branch -v
* master de93b02 my two commit
test_fix de93b02 my two commit
切换分支(git checkout [分支名])
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git checkout test_fix
Switched to branch 'test_fix'
#查看分支
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ git branch -v
master de93b02 my two commit
* test_fix de93b02 my two commit
#用ll查看
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ ll
total 1
-rw-r--r-- 1 DELL 197121 54 11月 19 15:05 test.txt
#这是复制过来的test.txt
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ cat test.txt
test.txt
test gitdev
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#在分支上修改内容并提交
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ vim test.txt
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ git commit -m "分支内容修改" test.txt
[test_fix 924b8d6] 分支内容修改
1 file changed, 1 insertion(+)
合并分支(git merge [ 有新内容分支名 ])
第一步:切换到接受修改的分支(被合并,增加新内容)上 gitcheckout [被合并分支名]
第二步:执行 merge 命令 git merge [有新内容分支名]
#git checkout 到 master分支
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ git checkout master
Switched to branch 'master'
#查看test.txt还是原来的版本
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ cat test.txt
test.txt
test gitdev
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#合并分支
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git merge test_fix
Updating de93b02..924b8d6
Fast-forward
test.txt | 1 +
1 file changed, 1 insertion(+)
#查看text.txt已经把分支上的合并过来了
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ cat test.txt
test.txt
test gitdev
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
这是分支的内容
解决冲突
有分支可能会产生冲突下面就来解决冲突
#现在两个分支都是同一个本版的
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git branch -v
* master 924b8d6 分支内容修改
test_fix 924b8d6 分支内容修改
#修改master上的test.txt 并提交
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git commit -m "commit by master" test.txt
[master 7b070e4] commit by master
1 file changed, 2 insertions(+)
#切换到test_fix
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git checkout test_fix
Switched to branch 'test_fix'
#修改test.txt并提交
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ vim test.txt
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ git commit -m "commit by test_fix" test.txt
[test_fix b29c4fc] commit by test_fix
1 file changed, 2 insertions(+)
#查看分支两个分支的版本不一样了。
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ git branch -v
master 7b070e4 commit by master
* test_fix b29c4fc commit by test_fix
#切换到master
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ git checkout master
Switched to branch 'master'
#把test_fix分支的内容合并到master上
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git merge test_fix
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
#查看状态提示冲突
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master|MERGING)
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
打开test.txt
test.txt
test gitdev
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
这是分支的内容
<<<<<<< HEAD
edit by master
=======
edit by test_fix
>>>>>>> test_fix
修改成这样
test.txt
test gitdev
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
这是分支的内容
edit by master
edit by test_fix
new content
然后添加到暂存区并提交 注意提交时不要带文件名
#保存退出
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master|MERGING)
$ vim test.txt
#添加到暂存区
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master|MERGING)
$ git add test.txt
#提交修改 注意!!!不要带文件名!!!!!!!!!
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master|MERGING)
$ git commit -m "master merge test_fix"
[master c9bc145] master merge test_fix
#解决冲突后的test.txt文件
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ cat test.txt
test.txt
test gitdev
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
这是分支的内容
edit by master
edit by test_fix
new content
小结:
第一步:编辑文件,删除特殊符号
第二步:把文件修改到满意的程度,保存退出
第三步:git add [文件名]
第四步:git commit -m “日志信息”
注意:此时 commit 一定不能带具体文件名
Github
主页
https://github.com/
创建资源库
添加远程库并创建别名
git remote add [别名] [远程地址] 添加远程库
git remote -v 查看当前所有远程地址和别名
git push [别名] [分支名称] 把分支推送到远程库中
#添加远程库
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git remote add testgit https://github.com/Eugene-Jou/testGit.git
#查看所有远程库
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git remote -v
testgit https://github.com/Eugene-Jou/testGit.git (fetch)
testgit https://github.com/Eugene-Jou/testGit.git (push)
#把master分支推送到github
aaa@qq.com-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git push testgit master
Enumerating objects: 18, done.
Counting objects: 100% (18/18), done.
Delta compression using up to 8 threads
Compressing objects: 100% (11/11), done.
Writing objects: 100% (18/18), 1.38 KiB | 1.38 MiB/s, done.
Total 18 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To https://github.com/Eugene-Jou/testGit.git
* [new branch] master -> master
成功把test.txt放到了github上
需要时再从github上把代码在拉下来
git clone https://github.com/Eugene-Jou/testGit.git
Git的基本操作就到这儿。
上一篇: git基本使用 详解
推荐阅读
-
CorelDRAW使用钢笔工具绘制装饰画背景
-
Apple Watch 使用技巧和隐藏功能大全 应用之间快速切换教程
-
Android 数据存储之 FileInputStream 工具类及FileInputStream类的使用
-
Android使用Circular Reveal动画让页面跳转更炫酷
-
华硕笔记本截屏快捷键失灵怎么使用键盘截屏?
-
实例讲解使用HTML5 Canvas绘制阴影效果的方法
-
微博营销的基本法则你都了解吗?
-
一点浏览器怎么设置右键快速关闭网页 一点浏览器右键快速关闭网页功能使用方法
-
Android 自动判断是电话,网址,EMAIL方法之Linkify的使用
-
Android变形(Transform)之Camera使用介绍