Git学习笔记&使用记录

Git学习笔记

基础概念

git分为三个区
workspace 工作区
index stash暂存区
local repo本地仓库
remote repo 远程仓库

Install

http://www.git-scm.com

windows下载便携版本的git设置一下环境变量即可
cmd是一个文件夹
在这里插入图片描述

初始化workspace

切换到想要创建为git仓库的文件夹
注意:文件名最好都是英文的,方便后期操作
git init命令把目录变成git可以管理的仓库
empty git repository 空的git仓库
空仓库中有一个隐藏的.git/文件夹

Administrator@DESKTOP-G9 MINGW64 /e/Documents/Typora
$ git init
Initialized empty Git repository in E:/Documents/Typora/.git/

Administrator@DESKTOP-G9 MINGW64 /e/Documents/VSCodePersonal (master)
$ ls -a .git/
./  ../  HEAD  config  description  hooks/  info/  objects/  refs/

Administrator@DESKTOP-O9 MINGW64 /e/Documents/Typora (master)
$ ls -a .git/
./  ../  HEAD  config  description  hooks/  info/  objects/  refs/

Git 全局设置Global Configuration:

配置账户

config的三个作用域
缺省等同于local

git config --global   #global对当前用户所有仓库有效
git config --local     #local只对某个仓库有效
git config --system    #system对系统所有登录的用户有效
# 缺省等同于local

优先级

local > global > system

配置全局用户名密码

git config --global user.name "your_name"
git config --global user.email "your_email@domain.com"
# 清除 --unset
git config --global --unset user.name
git config --global --unset user.email
# 查看配置
git config --global --list
git config --local --list
git config --system --list

建项目仓库Create project repo

两种场景:

  1. 新建的项目直接用git管理
  2. 把已有的项目代码纳入git管理
mkdir test
cd 某个文件夹
git init   your_project #会在当前路径下创建和项目名称同名的文件夹 初始化
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/test/test.git   关联remote repo 
git pull 拉取远程仓库到本地
git fetch 获取远程仓库中所有的分支到本地
git push -u origin master
cd existing_git_repo
git init 
git remote add origin https://gitee.com/test/test.git
#推送本地到远程master
git push -u origin master

Git常见操作

在这里插入图片描述

工作区中,对于git已经跟踪的文件,使用-u参数一次性提交到暂存区

git add -u

加了-a,在 commit 的时候,能帮你省一步 git add ,但也只是对修改和删除文件有效, 新文件还是要 git add,不然就是 untracked 状态

git commit -a

工作区文件提交到暂存区 指令 git add 对应文件是当前目录下的.git/index文件
暂存区文件提交到本地仓库 指令 git commit
本地仓库提交到远程仓库 指令 git push

本地配置用户名

git config --global user.name ""
git config --global user.email ""
查看全局配置
git config --global --list 
初始化git仓库
git init
将local workspace放到暂存区也就是index
git add .
暂存区放local repo
git commit -m "the first push"
查看当前项目的状态
git status
查看local repo的文件
This lists all of the files in the repository, including those that are only staged but not yet committed. 
这将列出存储库中的所有文件,包括仅暂存但尚未提交的文件
git ls-files

查看日志
git log
按照作者名查找改动
git log --author=''
添加github
git remote add origin https://github.com/username/repo.git
推送本地repo至github
git push -u origin master
删除文件
git rm filename

文件重命名

git mv origin_name new_name
移动文件夹
git mv filename path
git status
git commit -m "解释"
移动并重命名
git mv filename path/newfilename
git commit -m "移动重命名"

文件有变化,如何查看文件前后变化
git log --pretty=oneline path/filename
找到文件id 取出前几位,或者整个id
git show id

git log -p path/filename
撤销文件追踪
git reset HEAD path/filename
git checkout --path/filename
操作失误,如何还原
回到上一次的提交状态 最后一次提交的状态
git checkout --path/filename
只回退某个文件的指定版本
git log
git checkout 文件id --filename
修改内容之后进行push
git push origin master 推送本地分支到远程仓库
git rm -r --cached 文件/文件夹名字 取消文件被版本控制
git reflog 获取执行过的命令
git log --graph 查看分支合并图
git merge --no-ff -m '合并描述' 分支名 不使用Fast forward方式合并,采用这种合并方式可以看到合并记录
git check-ignore -v 文件名 查看忽略规则
git add -f 文件名 强制将文件提交

不提交到暂存区,直接进行commit

git commit -am'direct commit'

分支操作

查看分支

查看本地有多少分支

git branch -v
git branch -av
创建新分支
git branch branch_name 
排序按照首字母进行排序
前面有*代表当前在哪个分支
创建完成会把master中的内容默认拷贝到分支中
git branch -b 分支名 origin/分支名 创建远程分支到本地
git branch -v 查看所有分支的最后一次操作
git branch -vv 查看当前分支
远程分支

列出本地和远程的所有分支

$ git branch -a
* dev
  master
  remotes/origin/develop
  remotes/origin/feature
  remotes/origin/master
  remotes/origin/release

关联远程分支 将本地的dev分支和远程的develop分支建立关联

$ git branch --set-upstream-to=remotes/origin/develop
Branch 'dev' set up to track remote branch 'develop' from 'origin'.

列出远程关联的分支

git removt -v
删除分支
git branch -d branch_name  删除本地分支
git branch origin: branch_name  删除远程仓库分支
git branch -D branch_name  强制删除分支

checkout创建分支并切换到新建的分支上

git checkout -b new_branch_name
git checkout branch_name 切换分支
合并分支
1.切换分支
git --checkout master
git branch
git branch --merged 查看别的分支和当前分支合并过的分支
git branch --no-merged 查看未与当前分支合并的分支
git merge branch_name 合并分支到当前分支上    branch_name:要合并的分支的名字
清空缓存区
rm .git/index
暂存区的本质是.git目录下的index文件

标签操作

每个版本的独立标签,所有版本的标签管理

git tag  列出所有标签列表
git tag 标签名 添加标签(默认对当前版本)
git tag v1.0
git tag 标签名commit_id 对某一提交记录打标签
git tag -a 标签名 -m '描述' 创建新标签并增加备注
git show 标签名 查看标签信息

默认添加最新一次的commit
git tag name fileID
git tag -d 标签名   删除本地tag
git push origin tag_name 推送标签到远程仓库 举例:git push origin v1.0
git push origin:refs/tags/标签名 从远程仓库remote repo 中删除标签

暂存操作

git stash 暂存当前修改
git stash apply 恢复最近一次的暂存
git stash pop 恢复暂存并删除暂存记录
git stash list 查看暂存列表
git stash drop stash_name 删除某次某次暂存(例:stash@{0})
git stash clear 清空暂存

日志操作

查看提交日志

git log

查看commit列表,相对git log更加简洁

git log --oneline

只显示最近的几次commit

git log -n4
git log -n4 --oneline

查看本地所有分支的log

git log --all
# 图形化显示所有分支
git log --all --graph

回退操作

清空暂存区的所有内容

git reset --hard
#再次查看git状态 显示空
git status
git reset --hard HEAD^ 回退到上一个版本 一个脱字符代表一个版本
#回退到指定版本 git log查看想要恢复的版本的前几位或者id全部
git reset --hard id (commit_id) 
git checkout --file 撤销修改的文件(如果文件加入到了暂存区,则回退到暂存区的,如果文件加入到了版本库,则还原至加入版本库之后的状态)
git reset HEAD file 撤回暂存区的文件修改到工作区
只回退某个文件的指定版本
git log
git checkout 文件id --filename

取消某次commit

git revert commit_name

推送

强制推送到远程仓库

git push -f origin master

图形化操作

在本地某个仓库目录中输入

gitk

布局介绍
左上角:分支树
patch:变更集

.git文件夹

config存放配置信息

HEAD 指针 文件内容
记录此时的git工作分支
HEAD落脚于commit

$ cat .git/HEAD 
ref: refs/heads/master

git的分支指针指向的内容

$ cat .git/refs/heads/master 
8647483a3afc1707392a642ef70f550403032a3f


#查看文件类型
$ git cat-file -t 8647483a3afc1
commit

Administrator@DEW64 /e/VSCodePersonal (master)
$ git branch -av
* master 8647483 test

查看文件指向

git cat-file -t

git中的三个主要对象:
commit
tree
blob

ref中存放引用的内容,各个分支或tag的信息

git对象彼此关系:
一个commit对应一棵树
tree代表取出commit 文件快照

分离头指针

  1. 在某一个commit下修改内容进行commit会出现的内容
git checkout 某次commit的id
  1. 修改部分内容,进行提交
  2. 此时又需要切换到主分支,git会提示刚刚提交的内容没有关联分支,是否需要创建分支进行关联,此时不创建分支进行关联,则刚刚的修改git会丢弃
#创建分支
git branch branch_name commit_id

远程仓库gitee

git常用的传输协议

常用协议语法格式说明
本地协议(1)/path/to/repo.git哑协议
本地协议(2)file://path/to/repo.git智能协议
http/https协议http://git-server.com:port/path/to/repo.git https://git-server.com:port/path/to/repo.git平时接触到的都是智能协议
ssh协议user@git-server.com:path/to/repo.git工作中最常用的智能协议

哑协议与智能协议:

直观区别:哑协议传输进度不可见;智能协议传输可见
传输速度:智能协议比哑传输协议传输速度快。

查看远程仓库的详细信息

$ git remote -v 
gitee   git@gitee.com:xxx/xxxxra.git (fetch)
gitee   git@gitee.com:xxx/txxxra.git (push)

删除远程关联的仓库

$ git remote rm 远程仓库名称
$ git remote rm origin

关联远程仓库

$ git remote add gitee git@gitee.com:git个人域名/reponame.git

场景处理

有冲突时如何正确合并分支

在不同分支处修改了同一处的代码,此时git就不知道你需要保存那一部分的代码
忽略其他分支的代码,保留原分支的代码
git merge --abort 
git commit提交报错,
按下i键编辑,输入内容,:wq保存退出
git commit -m "resolve 冲突之后的code"

git多人分支协作常见操作
不同人想要查看版本路线
查看简写版本的log
git log --oneline
查看当前版本路线
git log --oneline --graph
删除
拉取所有的远程仓库
git fetch
删除分支
git push origin --delete branch_name
不同人修改不同文件
git add <filename>
git commit -m "second"
git push
push如果出现文件就是分支没有合并
git merge path/branch_name
vim编辑,保存退出
git push
git clone url 文件夹名称
git branch -av
本地分支和远程分支做关联
git checkout -b local_branch_name remotes/origin/test
拉取,提交,推送 fetch add commit push

忽略已加入到版本库中的文件

git update-index --assume-unchanged file 忽略单个文件
git rm -r --cached 文件/文件夹名字 (.  忽略全部文件)

取消忽略文件
git update-index --no-assume-unchanged file

浏览器插件chrome plugin

github  octotree
enhanced github show the every file size
gitzip for github 

实例

两台设备配置同步pycharm

新设备
切换本地项目的存放目录
先使用clone把remote repo中的项目克隆下来

git clone https://gitee.com/xxx/xxx.git

pycharm中open这个项目文件夹
配置git全局配置账户和密码
关联远程仓库
修改测试内容
上传
另一台设备拉取测试

//查询当前远程的版本
$ git remote -v
//获取最新代码到本地(本地当前分支为[branch],获取的远端的分支为[origin/branch])
$ git fetch origin master  [示例1:获取远端的origin/master分支]
$ git fetch origin dev [示例2:获取远端的origin/dev分支]
//查看版本差异
$ git log -p master..origin/master [示例1:查看本地master与远端origin/master的版本差异]
$ git log -p dev..origin/dev   [示例2:查看本地dev与远端origin/dev的版本差异]
//合并最新代码到本地分支
$ git merge origin/master  [示例1:合并远端分支origin/master到当前分支]
$ git merge origin/dev [示例2:合并远端分支origin/dev到当前分支]

本地合并分支

本地创建一个develop分支,和远程的develop分支关联;
在本地dev分支编辑文件,push到remote dev分支

$ git pull origin develop 
From https://gitee.com/kxxo/xx
 * branch            develop    -> FETCH_HEAD
fatal: refusing to merge unrelated histories
# 拒绝合并两个不相关的历史

强制合并两个不相关的分支

$ git pull origin develop --allow-unrelated-histories
From https://gitee.com/xxxx/xxx
 * branch            develop    -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 .gitee/ISSUE_TEMPLATE.zh-CN.md |  13 +++
 .gitignore                     |  18 ++++
 LICENSE                        | 201 +++++++++++++++++++++++++++++++++++++++++
 README.en.md                   |  36 ++++++++
 README.md                      |  37 ++++++++
 5 files changed, 305 insertions(+)
 create mode 100644 .gitee/ISSUE_TEMPLATE.zh-CN.md
 create mode 100644 .gitignore
 create mode 100644 LICENSE
 create mode 100644 README.en.md
 create mode 100644 README.md

将本地的dev分支合并到master并推送

1. 切换到master
git checkout master
2. 合并分支 merge后跟要合并到当前分支的分支名
git merge dev 
3. 推送
git push 

本地dev分支记录

创建本地dev分支,远程dev分支 并关联

Administrator@DESKTOP-9 MINGW64 /e/VSCode (dev)
$ git pull origin dev --allow-unrelated-histories 
remote: Enumerating objects: 50, done.
remote: Counting objects: 100% (50/50), done.
remote: Compressing objects: 100% (49/49), done.
remote: Total 50 (delta 18), reused 11 (delta 1), pack-reused 0
Unpacking objects: 100% (50/50), 14.35 KiB | 16.00 KiB/s, done.
From https://gitee.com//vscode
 * branch            dev        -> FETCH_HEAD
 * [new branch]      dev        -> origin/dev
CONFLICT (add/add): Merge conflict in README.md
Auto-merging README.md
Automatic merge failed; fix conflicts and then commit the result.
Administrator@DESINGW64 /e/VSCode (dev|MERGING)
$ git branch -a 
* dev
  master
  remotes/origin/dev
  remotes/origin/master
Administrator@DESK MINGW64 /e/VSCode (dev|MERGING)
$ git commit -m "merge"
[dev 7bbe799] merge

Administrator@DESKTOP MINGW64 /e/VSCode (dev)
$ git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 580 bytes | 580.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https:///.git
   67f9ab8..7bbe799  dev -> dev

dev分支推送失败

$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use
本地分支和远端分支不相同
    git push origin HEAD:develop

To push to the branch of the same name on the remote, use
本地分支名和远端分支名相同
    git push origin HEAD

To choose either option permanently, see push.default in 'git help config'.
# 上游分支和当前分支不匹配,推送分支到远端

笔者本地分支名称和远程分支不同所以使用此命令推送
$ git push origin HEAD:develop
Merge made by the 'recursive' strategy.     warning: LF will be replaced by CRLF in 资源对象yaml/test/case2.yaml.   $ git push 
fatal: The current branch dev has no upstream branch. 
To push the current branch and set the remote as upstream, use 
 
git push --set-upstream origin dev niaho  hint: Updates were rejected because the remote contains work that you do got 
hint: not have locally.   This is usually caused by another repository pushing 
hint: to the same ref. You may want to first integrate the remote changes 
hint: (e.g., 'git pull ...') before pushing again. 
hint: See the 'Note about fast-forwards' in 'git push --help' for details.


通过“递归”策略进行合并。警告:LF将被资源对象yaml/test/case2.yaml中的CRLF所取代。git推动美元
fatal:当前分支dev没有上游分支。
要推动当前分支并将远端设置为上游,请使用

git push——set-upstream origin dev niaho提示:更新被拒绝,因为远程包含了你得到的工作
提示:本地没有。这通常是由另一个存储库推送引起的
提示:指向相同的引用。您可能想首先集成远程更改
提示:(例如,'git pull…')然后再推。
提示:参见'git push——help'中的'Note about fast-forward '了解详情。

vscode推送记录

$ git push 
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

解决:使用git push -u

$ git push -u origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 283 bytes | 283.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com
   968b997..ae63761  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
$ git push --set-upstream origin master 
To https://gitee.comxxxx/vscode.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.comxxxvscode.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git push --set-upstream origin master 
To https://gitee.comxxxx/vscode.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.comxxxvscode.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

使用过程中遇到过的问题

git push 报错

Administrator@DESKTOP-9 MINGW64 /e/Documents/Typora (master)
$ git push origin master 
To https://gitee.com/username/typora.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/username/typora.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

在这里插入图片描述
原因:…
解决:

# 合并不相关的分支并提交
git pull origin master --allow-unrelated-histories
git push origin master

git push出错

cause@DESKTOP-V MINGW64 /d/project/wiki (master)
$ git push -u origin master
error: src refspec master does not match any
error: failed to push some refs to 'git@gitee.com:username/wiki.git'

git add .sh文件提示

linux下的换行符为LF Windows下的换行符为CRLF

$ git add install_httpd.sh 
warning: LF will be replaced by CRLF in install_httpd.sh.
The file will have its original line endings in your working directory

git push 报错

Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details

$ git branch --set-upstream-to=origin/master
Branch 'master' set up to track remote branch 'master' from 'origin'.

$ git branch --set-upstream-to=origin/master
Branch 'master' set up to track remote branch 'master' from 'origin'.

$ git push origin master 
To https://gitee.com/cause_jun/scripts.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/cause_jun/scripts.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 push -u origin master
To https://gitee.com/cause_jun/scripts.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/cause_jun/scripts.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.

原因: remote repo 和local repo不一样,hint中提示用git pull

$ git pull --rebase origin master 
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.

git --rebase做了什么:

  1. 把commit到local repo的内容,取出来放在暂存区(stash)这时候work space是干净的
  2. 把remote repo中的内容pull到local repo,这时候由于work space是干净的,所以不会有冲突
  3. 从暂存区(stash)把你之前提交的内容取出来,同拉下来的内容进行合并

rebase在pull内容before要make sure the local repo is clean, 如果你本地修改的内容没完全commit或者stash,就会rebase失败

$ git stash 
Saved working directory and index state WIP on master: bedb6d7 add install_httpd

$ git pull --rebase origin master 
From https://gitee.com/cause_jun/scripts
 * branch            master     -> FETCH_HEAD
Successfully rebased and updated refs/heads/master.

接着再push就ok了

git checkout分支报错

$ git checkout master 
error: Your local changes to the following files would be overwritten by checkout:
        push.sh
Please commit your changes or stash them before you switch branches.
Aborting

意思是dev分支中有修改了的内容未提交,提示你可以先commit或者放在暂存区。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值