Git基本使用

Git的其他知识就不说了直接上操作吧

Git官网

https://git-scm.com/

Git基础操作

初始化

先在自己的工作区创建一个文件夹:

DELL@DESKTOP-TAGH8QP MINGW64 /d
$ mkdir testGitDiv

DELL@DESKTOP-TAGH8QP MINGW64 /d
$ cd testGitDiv/

DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv
$ pwd
/d/testGitDiv

走到该目录下用git init命令进行初始化并用ls -la查看,多了.git的隐藏文件:

DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv
$ git init
Initialized empty Git repository in D:/testGitDiv/.git/

DELL@DESKTOP-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

注意:项目级别优先于系统用户级别,二者都有时采用项目级别的签名
如果只有系统用户级别的签名,就以系统用户级别的签名为准,二者都没有不允许
这儿我们用局部签名就好了

DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git config user.name eugene

DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git config user.email eugene@email.com

查看工作区、暂存区状态

用git status查看状态,显示啥都没有,现在创建一个test.txt的文件,再查看状态提示用git add添加到暂存区

DELL@DESKTOP-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)

DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ vim test.txt

DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ ls
test.txt

DELL@DESKTOP-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 进行提交后,再看状态提示没有需要提交的东西。

DELL@DESKTOP-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

DELL@DESKTOP-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


DELL@DESKTOP-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

DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git status
On branch master
nothing to commit, working tree clean

查看历史纪录

1.git log
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git log
commit 65d08acfcca8402f0c7cb0d3cb8474e5d0f0434a (HEAD -> master)
Author: eugene <eugene@email.com>
Date:   Tue Nov 19 14:59:12 2019 +0800

    my three commit

commit de93b02ec4464e009dfd240ea263ffd9d2862929
Author: eugene <eugene@email.com>
Date:   Tue Nov 19 14:58:01 2019 +0800

    my two commit

commit 4b51a42cf1a04e4744e8f8c8ae78616a08013531
Author: eugene <eugene@email.com>
Date:   Tue Nov 19 14:56:11 2019 +0800

    my test First commit
2.git log --pretty=oneline
DELL@DESKTOP-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
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git reflog
65d08ac (HEAD -> master) HEAD@{0}: commit: my three commit
de93b02 HEAD@{1}: commit: my two commit
4b51a42 HEAD@{2}: commit (initial): my test First commit
前进和后退(git reset --hard [索引值] )

继续修改重复提交几次以后查看记录

DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git reflog
e8e8a7d (HEAD -> master) HEAD@{0}: commit: my four commit
65d08ac HEAD@{1}: commit: my three commit
de93b02 HEAD@{2}: commit: my two commit
4b51a42 HEAD@{3}: commit (initial): my test First commit
#回到第二次提交
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git reset --hard de93b02
HEAD is now at de93b02 my two commit
#再查看记录
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git reflog
de93b02 (HEAD -> master) HEAD@{0}: reset: moving to de93b02
e8e8a7d HEAD@{1}: commit: my four commit
65d08ac HEAD@{2}: commit: my three commit
de93b02 (HEAD -> master) HEAD@{3}: commit: my two commit
4b51a42 HEAD@{4}: commit (initial): my test First commit

分支

创建分支(git branch [分支名])
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git branch test_fix
查看分支(git branch -v)
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git branch -v
* master   de93b02 my two commit
  test_fix de93b02 my two commit
切换分支(git checkout [分支名])
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git checkout test_fix
Switched to branch 'test_fix'
#查看分支
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ git branch -v
  master   de93b02 my two commit
* test_fix de93b02 my two commit

#用ll查看
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ ll
total 1
-rw-r--r-- 1 DELL 197121 54 11月 19 15:05 test.txt

#这是复制过来的test.txt
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ cat test.txt
test.txt
test gitdev
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#在分支上修改内容并提交
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ vim test.txt
DELL@DESKTOP-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分支
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ git checkout master
Switched to branch 'master'

#查看test.txt还是原来的版本
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ cat test.txt
test.txt
test gitdev
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#合并分支
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git merge test_fix
Updating de93b02..924b8d6
Fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)

#查看text.txt已经把分支上的合并过来了
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ cat test.txt
test.txt
test gitdev
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
这是分支的内容

解决冲突

有分支可能会产生冲突下面就来解决冲突

#现在两个分支都是同一个本版的
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git branch -v
* master   924b8d6 分支内容修改
  test_fix 924b8d6 分支内容修改
  
#修改master上的test.txt 并提交
DELL@DESKTOP-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 
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git checkout test_fix
Switched to branch 'test_fix'

#修改test.txt并提交
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ vim test.txt
DELL@DESKTOP-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(+)

#查看分支两个分支的版本不一样了。
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ git branch -v
  master   7b070e4 commit by master
* test_fix b29c4fc commit by test_fix

#切换到master
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (test_fix)
$ git checkout master
Switched to branch 'master'

#把test_fix分支的内容合并到master上
DELL@DESKTOP-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.

#查看状态提示冲突
DELL@DESKTOP-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

然后添加到暂存区并提交 注意提交时不要带文件名

#保存退出
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master|MERGING)
$ vim test.txt

#添加到暂存区
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master|MERGING)
$ git add test.txt

#提交修改 注意!!!不要带文件名!!!!!!!!!
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master|MERGING)
$ git commit -m "master merge test_fix"
[master c9bc145] master merge test_fix

#解决冲突后的test.txt文件
DELL@DESKTOP-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 [别名] [分支名称] 把分支推送到远程库中

#添加远程库
DELL@DESKTOP-TAGH8QP MINGW64 /d/testGitDiv (master)
$ git remote add testgit https://github.com/Eugene-Jou/testGit.git

#查看所有远程库
DELL@DESKTOP-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
DELL@DESKTOP-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的基本操作就到这儿。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值