git设置本地用户名和密码_Git学习(3)

a9eab719439e72ddb3603a8a97b785dd.png
git_learn

5.版本回溯

1.文件恢复git checkout

切换分支或恢复文件

$ git checkout -- filename     # 丢球工作区中的某个文件修改
$ git checkout -- .            # 丢弃工作区中的所有修改 
$ touch 2.txt
$ git status
On branch master
Untracked files:
(use "git add ..." to include in what will be committed)
     2.txt

nothing added to commit but untracked files present (use "git add" to track)

$ git add 2.txt
$ git status
On branch master
Changes to be committed:
(use "git restore --staged ..." to unstage)
     new file:   2.txt

$ git commit -m 'add 2.txt'
[master 353e702] add 2.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 2.txt

$ git status
On branch master
nothing to commit, working tree clean

$ rm 2.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
     deleted:    2.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git checkout 2.txt
Updated 1 path from the index

$ git status
On branch master
nothing to commit, working tree clean

$ ls
2.txt  
2.git reset

每次的更改都会在git中留下记录值HEAD也就是commit id.根据这个HEAD 可以跳回到原来的状态.

$ git reset  

可以通过git log来查看更改的HEAD

# 新建一个测试文件
$ touch 3.txt

# 编辑测试文件,并添加数字 1,2,3,4
$ vim 3.txt

$ git status
On branch master
Untracked files:
(use "git add ..." to include in what will be committed)
     3.txt



$ git add 3.txt


$ git commit -m "add 3.txt"
[master e38dbcb] add 3.txt
1 file changed, 4 insertions(+)
create mode 100644 3.txt


$ git status
On branch master
nothing to commit, working tree clean

# 在测试文件中新添加数字 5
$ vim 3.txt

$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
     modified:   3.txt

no changes added to commit (use "git add" and/or "git commit -a")


$ git add 3.txt



$ git commit -m 'add 5 in 3.txt'
[master 840106f] add 5 in 3.txt
1 file changed, 1 insertion(+)

$ git status
On branch master
nothing to commit, working tree clean

# 查看commit id
$ git log
commit 840106f762192b023f26abfd1e6c2a55b96379f5 (HEAD -> master)
 add 5 in 3.txt


$ ls
2.txt  3.txt  test.txt  test1


$ cat 3.txt
1
2
3
4
5


# 回溯文件的 commit id
$ git reset --hard e38dbcb124  # id号不用写全
HEAD is now at e38dbcb add 3.txt


$ ls
2.txt  3.txt  test.txt  test1
c

$ cat 3.txt
1
2
3
4
3.查看历史命令git reflog
$ git reflog   # 查看指令,但是不会永久保存,git会自己定时清理
4.查看某次提交的内容git show
$ git show "commit id"
5.查看分支版本号
$ git rev-parse 分支名

6.分支

1.分支的创建/切换
# 创建分支
$ git branch 分支名

# 查看分支
$ git branch 

# 切换分支
$ git checkout 分支名

# 创建分支并切换到这个分支
$ git checkout -b 分支名

示例:

# 创建分支1
$ git branch branch_1

# 查看所有的分支
$ git branch
branch_1
* master

# 创建分支2
$ git branch branch_2

# 切换分支2
$ git checkout branch_2
Switched to branch 'branch_2'

# 创建文件
$ touch 2.txt

$ vim 2.txt

# 分支2中查看到2个文件
$ ls
1.txt  2.txt

$ git add .
$ git commit -m "add 2.txt in branch_2"

# 切换到主分支
$ git checkout master
Switched to branch 'master'

# 查看到只有1个文件
$ ls
1.txt

# 查看所有的分区和所处的分区(* 为所在分区)
$ git branch
  branch_1
  branch_2
* master

GUI界面中查看更容易:

5e712582a63868f0a4043d2affa2ef15.png
10216
2.分支合并

当我们修复完bug或者是开发新的特性后,需要合并回原理的主分支上.

$ git merge    # Join two or more development histories together 
$ git rebase   # Reapply commits on top of another base tip
3.分支删除

当分支完成自己的使命后,分支就不需要了,可以删除他

$ git branch -d 分支名

7.远程仓库

1.创建SSH KEY

为了演示方便使用gitee

如果本地用户主目录中没有.ssh 目录,则需要手动创建ssh_key

$ ssh-keygen -t rsa -C "youremail@example.com" # 使用默认值即可
$ cd .ssh/

$ ls
id_rsa    # 私钥,不能泄露
id_rsa.pub  # 公钥,需要上传给服务器
2.上传公钥

登录gitee,并上传ssh公钥

a7f719ee0a372fad1c71e1219629ff75.png
10217
3.新建一个远程仓库

如图:

a475c0926f7c2bea3bbaeffff7068ef3.png
10218
4.推送本地库到远程库
# 关联本地仓库和远程仓库
git remote add origin 远程仓库地址
# 使用git 地址
$ git remote add origin git@gitee.com:ningwenyan/test.git

$ git remote -v
origin  git@gitee.com:ningwenyan/test.git (fetch)
origin  git@gitee.com:ningwenyan/test.git (push)

之后,可以正常的git pushgit pull进行推送

$ git push -u origin master  
#  -u参数 作为第一次提交使用, 
# 作用是把本地master分支和远程master分支关联起来(设置默认远程主机), 
# 后续提交不需要这个参数!

其他命令

git remote set-url origin 远程仓库地址
# 也可以先删除origin后再添加
git remote rm origin    # 删除仓库关联
git remote add origin 远程仓库地址 # 添加仓库关联

windows系统添加用户名密码错误修改方法:可以在控制面板中找到凭据管理器 删除错误的用户名密码,然后重新登录.

本电脑不知是不是因为输错密码的缘故,弹出错误提示如下

To gitee.com:ningwenyan/test1.git
! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@gitee.com:ningwenyan/test1.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决办法:使用git pull 合并分支

git pull -u -f  origin master  # 第一次加-f 后续就不用添加了
5.从远程仓库克隆

远端新建仓库

4d9c6b61affeea0e4c6506cecae4c679.png
10219

执行如下命令

$ git clone git@gitee.com:ningwenyan/test2.git
Cloning into 'test2'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (4/4), done.

$ ls
test2/

$ cd test2/

$ ls
README.en.md  README.md

8.标签

打标签的作用,就是给项目的开发节点,加上语义化的名字,也即功能版本的别名. 打上标签名的同时,写上附带信息,可以方便项目日后维护过程中的回溯和复查.

另外,也可以通过标签记录,大致了解当前项目的向下兼容性,API的修改和迭代情况.

1.创建标签
$ git tag -a "tagname" -m "comment" "commit_id"
# -a 标签名
# -m 备注信息
# commit_id 提交id

示例:

$ git tag -a 'v0.1.0' -m "初始" 

类似v0.1.0遵从一个简单的规范

  • 主版本号: 当你做了不兼容的API修改
  • 次版本号: 向下兼容的功能性增加
  • 修订号: 向下兼容的问题修正
2.查看所有标签
$ git tag
3.查看具体标签信息
$ git show tagname
4.删除本地标签
$ git tag -d tagname
9d6be6ab744b391d26de6aa24c4eacf8.png
wechat
- END -
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值