git小白初学习

#1. 安装 http://npm.taobao.org/mirrors/git-for-windows/

#2. 设置自己的用户信息,方便各个节点知道这个客户端是谁

#全局设置

git config --global user.name 'xyg'

git config --global user.email '1584066465@qq.com'

#对单个仓库设置
cd 到对应仓库下
git config user.email '1584066465@qq.com'

#3. 建立本地git仓库

mkdir e:/gitrepo

cd e:/gitrepo

#4. 初始化文件夹,使其变为可管理的git仓库

git init

#这时候看e:/gitrepo文件夹下就回多出一个.git文件,表示已成功初始化

#5. 拷贝项目文件进入该目录

#6. 添加修改并提交

git add index.php #添加单个文件

git add Admin/ #添加一个目录

git add *.php #添加所有php文件

git commit -m '修改原因说明'  #不必每次都执行,可以add一定数量之后在执行

#7. 查看被修改过的文件或者不受git管理的文件列表

git status # +(加号)开始,绿色行,表示新增

-(减号)开始,红色行,表示删除

git对修改认为是 删除旧行,新增新行

git diff index.php          #查看修改文件 都修改了什么
git diff HEAD -- index.php  #对比仓库中和当前修改的区别, -- 分割路径修改
git diff xxxxxxx yyyyyyy    #对比两次提交的差别
git diff topic master       #对比master分支和topic分支的差别

git log #查看最近都修改了什么#  
        --pretty=oneline #让log更好看点#  
        --name-only #显示修改的文件名称#

git reflog   #查看所有操作的命令历史记录

#8. 回滚

git reset --hard HEAD^ #HEAD^ 表示回滚到上一次的版本 HEAD^^ 上上一次 HEAD~n 回滚到第n次提交的版本

git reset --hard xxxxxxx #回滚到指定commit id的版本, 可以通过git log查看, 注意这里的id一般没必要写全,前7位足够了,也可以更少

#9. 撤销修改

git checkout -- index.php #撤销工作区index.php文件的修改

git reset HEAD -- index.php #撤销暂存区index.php最新一次修改

#10. 删除文件

#先删除源文件

git rm index.php #从仓库中删除

#恢复误删文件(git之前管理过的)
git checkout -- index.php

#11. 连接远程仓库

#1). 创建SSH key

ssh-keygen -t rsa -C '1584066465@qq.com'

#一路回车就行了,创建好之后的sshkey在用户主目录里面

#例如 C:\Users\sujrex.ssh

#id_rsa位私钥不能泄露,id_rsa.pub为公钥提供给git服务器,如gitee

#2). 将id_rsa.pub的内容保存到远程库里面,后面以华为git举例

#3). 将本地仓库和远程仓库关联

git remote add boss-origin ssh://git@218.108.32.134:5188/home/gitrepo/miu.git

#公司远程库命名boss-origin

git remote add huawei-origin git@codehub.devcloud.huaweicloud.com:fucun00001/fucun.git

#华为远程库的名字就命名huawei-origin

#4). 查看远程仓库

git remote -v

#5). 拉取远程仓库的最新代码到本地

git pull huawei-origin master

#12. 克隆远程仓库

cd /homework/ #到工作目录 当在家里或者其他地方的时候,需要修改项目这时就可以用到这条命令

git clone git@codehub.devcloud.huaweicloud.com:fucun00001/fucun.git

#13. 分支概念

#1). 查看分支

git branch #查看所有分支,*指向的就是当前分支

#2). 创建删除分支
git branch fucunnewfeature    #创建fucunnewfeature分支
git switch fucunnewfeature    #切换到fucunnewfeature分支,这个命令也一样的结果 git checkout fucunnewfeature
#下面这行等同于上两行执行的结果
git switch -b fucunnewfeature #创建并切换到fucunnewfeature分支, 这个命令一样的结果 git checkout -b fucunnewfeature

git branch -d fucunnewfeature   #删除分支  -D 强行删除

#3). 合并分支
git switch master
git merge fucunnewfeature 
# 合并分支时,默认使用的是fast forward模式,如果想强制不使用该模式,可以使用
git merge --no-ff -m '合并原因' fucunnewfeature # --no-ff就是不使用默认模式,并且因为该命令会产生一次提交,所以必须有m参数
#fast forward模式在删除分支后,会丢失分支信息,使用上面的方案之后,会吧分支信息保留在git log中,即便删除了分支

git log --graph --pretty=oneline --abbrev-commit #查看分支合并图

#4). 设想场景 你正在分支上干活,现在突然间主线版本出现BUG,需要立即修复
git stash  #保存现场,待后续继续
git switch master #切回主分支
git switch -c issue-1001 # 创建bug分支,并切换到分支上,修改完成后 add, commit, switch master, merge到主分支上
git switch fucunnewfeature #重新切回到正在修改的分支上
git stash list #查看之前保存的现场
git stash pop #恢复现场并删除stash内容  也可以 
git stash apply #恢复现场,但不删除stash内容
git stash drop  #删除stash内容
git stash apply stash@{0} #指定恢复那个现场,当有多个现场的时候,其中stash@{0}可以在git stash list的时候查看到
    
#5). 设想场景,在master分支上修复了bug后,我们要想一想,fucunnewfeature分支是早期从master分支分出来的,所以,这个bug其实在当前fucunnewfeature分支上也存在。那怎么在fucunnewfeature分支上修复同样的bug?
    git cherry-pick xxxxxxx #这个xxx就是在issue-1001上commit时得到的,也可以git log查看, 这样就完成的了bug同步到分支上的功能
#6). 设想各种原因导致推送到远程服务器上的代码有问题需要回退
     git log --pretty=oneline #找到需要回滚到的节点commit id_rsa位私钥不能泄露,id_rsa
     git reset --hard commit_id #回滚到指定地点
     git push origin HEAD --force #远程提交回退
     
     #也可以通过以下方式回滚,比上面多出的是可以方便的备份修改
     git reset --hard commit_id
     git checkout -b newmaster #重新创建一个分支,这时候的分支就是上一次提交的代码
     git push origin newmaster:newmaster #推到对应的远程newmaster
     
     git branch -d master
     git push --delete origin master #这个时候相当于备份做好了,接下来就可以删除本地及远端的master分支
     
     git checkout -b master origin/newmaster
     git push master:master #从newmaster分支,重新在创建master分支,并推向远端

#14. 打包发版本

git switch master

git tag fucun-v1.0 [xxxxxxx] #默认给最新提交的commit上打tag,后面的xxxxxx是指定的commit id(通过git log查看)

git tag -a fucun-v1.0 -m "打tag说明" [xxxxxxx] #对当前tag进行注释说明

git tag #查看所有tag

git show fucun-v1.0 #查看指定tag的信息

git push huawei-origin fucun-v1.0 #推送指定tag到远程仓库
git push huawei-origin --tags     #一次性推送所有未推送到远程仓库的tag

git tag -d fucun-v1.0 #删除本地仓库指定tag
git push huawei-origin :refs/tags/fucun-v1.0 #删除远程仓库指定tag

#15. 忽略不需要提交的文件

.gitignore #创建该文件,里面编辑

#Windows

Thumbs.db

Desktop.ini

    #Linux
    *.ini

#16. 配置常用的命令的别名

#比如git log --graph --pretty=oneline --abbrev-commit这个命令太长了,记不住

git config --global alias.lg "log --graph --pretty=oneline --abbrev-commit" #之后只要执行

git lg #就可以了

git config #全局配置对应的文件在 .git/config下
#而当前仓库的配置文件在 .gitconfig下

#17. 配置beyondcompare

#在用户家目录下找到 .gitconfig文件,编辑或加入以下配置

[diff]

tool = bc4

[difftool]

prompt = true

[difftool "bc4"]

cmd = "D:/Program Files/Beyond Compare 4/BCompare.exe" "$(cygpath -w L O C A L ) " " LOCAL)" " LOCAL)""REMOTE"

[merge]

tool = bc4

[mergetool]

prompt = true

keepBackup = false #为了解决使用git mergetool合并分支时,总会产生以*.orig为扩展名的备份文件

[mergetool "bc4"]

#trustExitCode = true

cmd = "D:/Program Files/Beyond Compare 4/BCompare.exe" " L O C A L " " LOCAL" " LOCAL""REMOTE" " B A S E " " BASE" " BASE""MERGED"

#FAQ:

#1. warning: LF will be replaced by CRLF in ... #git默认开启在不同系统下不同换行结束符的转换

git config --global core.autocrlf false #关闭自动转换

#2. 第一次push时报错
The authenticity of host '....' can't be established.  输入yes

#3. 错误
error: failed to push some refs to 'git@codehub.devcloud.huaweicloud.com:fucun00001/fucun.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 --rebase huawei-origin master
#后续在git push就可以了

#4. git merge 和 git rebase区别
#假设现在有两个分支 A B
#1). 在B分支上执行 git merge A 后 A就被合到B上了
#2). 在B分支上执行 git rebase A 后,效果与merge是一样的,但是 A就没有了,两个分支就合在一起了。

#5. git pull 和 git pull --rebase
git pull = git fetch + git merge FETCH_HEAD 
git pull --rebase =  git fetch + git rebase FETCH_HEAD
#https://www.jianshu.com/p/dc367c8dca8e

#6.git push时出现
#remote: error: insufficient permission for adding an object to repository database ./objects
#这个主要原因是git服务器的git仓库权限不足导致的
cd /home/gitrepo/
chown -R git:git fucun.git
cd fucun.git
git config --bool core.bare true
相关推荐
Good_Starry2 小时前
Git介绍--github/gitee/gitlab使用
git·gitee·gitlab·github
N0zoM1z04 小时前
域内用户名枚举 实验
windows
梓䈑7 小时前
【C语言】自定义类型:结构体
c语言·开发语言·windows
云端奇趣7 小时前
探索 3 个有趣的 GitHub 学习资源库
经验分享·git·学习·github
感谢地心引力10 小时前
【MATLAB2024b】安装离线帮助文档(windows)
windows·matlab
F_D_Z11 小时前
【解决办法】git clone报错unable to access ‘xxx‘: SSL certificate problem:
网络·git·网络协议·ssl
等风来不如迎风去16 小时前
【git】main|REBASE 2/6
git
艾伦~耶格尔16 小时前
IDEA 配置 Git 详解
java·ide·git·后端·intellij-idea
云兮杜康16 小时前
IDEA中用过git之后文件名颜色全变红
java·git·intellij-idea
睡不醒的小泽16 小时前
git merge 和 git rebase
git