Git操作笔记
基础应用
简单使用
创建仓库
- # 1. 在git/gitee上创建远程仓库
- # 2. 在本地创建仓库并连接远程仓库
- mkdir snippets
- cd snippets
- git init
- touch README.md
- git add README.md
- git commit -m "first commit"
- git remote add origin https://gitee.com/Young_jam/snippets.git
- git push -u origin "master"
- # 2.5 或者连接本地已有仓库
- cd existing_git_repo
- git remote add origin https://gitee.com/Young_jam/snippets.git
- git push -u origin "master"
复制代码 常用命令
- git init 初始化新仓库
- git add [file/.] 添加[新文件/所有文件]到暂存
- git commit -m "xxx" 提交修改到本地版本库并附加提交信息
- git commit -F file 使用文件内容作为提交信息
- git commit --reuse-message 使用之前某次(由 commit-id 确定)提交的 message 作为本次提交的 message
- git commit --reedit-message= 作用同上,但是调用编辑器允许修改提交信息
- git commit --amend 修改上次的提交信息
- git push 同步到远程仓库,默认推送本地分支到有跟踪关系(通常是同名)的远程分支
- git push [-f] origin a:b 将本地 a 分支的内容推送到远程 b 分支,如果 b 分支有不同的提交历史,需要使用 -f 选项强制覆盖
- git pull 同步远程仓库到本地
- git clone httpS://git.xxxx 复制远程仓库到本地
- git status 查看仓库信息
- git log [-N] 查看仓库提交日志
- git log [-p] filename 查看单个文件的修改记录,-p 选项查看具体的修改内容
配置git
- # 设置全局用户
- git config --global user.name "xxxx"
- git config --global user.email "xxxx@xxx"
- # 设置git编辑器为vim
- git config --global core.editor vim
- # 设置git支持x显示中文路径
- git config --global core.quotepath false
复制代码
- 配置git push时免密码
- 本地主机创建ssh秘钥
- ssh-keygen -t rsa -C "xxxx@qq.com"
复制代码 - 打开gitee的个人设置,在安全设置--ssh 公钥中添加id_rsa.pub中的内容
- 如果配置远程仓库时使用的https地址,则需要修改为ssh
- git remote -v # 查看远程仓库地址,如果是https开头,则需要改为ssh
- git remote set-url origin git@gitee.com:yangjam_tm/coding.git
复制代码
常用操作
- git强制远程覆盖本地
- # 1. 拉取远程更新
- git fetch --all
- # 2. 撤销本地修改
- git reset --hard origin/master
- # 3. 同步远程仓库
- git pull origin master
复制代码 - 删除未跟踪的文件
- # 查看未跟踪文件
- git clean -n
- # 删除文件
- git clean -f
复制代码 - 取消跟踪文件
- git rm --cached readme1.txt # 删除readme1.txt的跟踪,并保留在本地。
- git rm --f readme1.txt # 删除readme1.txt的跟踪,并且删除本地文件
复制代码 - 本地 git 回退到指定版本
- # git log 查看特定版本的commit sha码
- git reset --hard [961ca2d2f45a28b962e293a10b11a8f7ab4e4777]
复制代码 - 撤销提交
- git reset --soft HEAD~1
- # 撤销最后一次commit,但是不撤销add操作
- git reset --mixed HEAD~1
- # 撤销最后一次commit及add操作,与git reset HEAD~1作用相同
复制代码 - 修改已经 push 到远程的 commit 信息
- # 本地同步到远程已经提交的版本
- git commit --amend -m "" # git commit --amend -F log.txt
- # 此时本地相当于回退到上一个版本然后重新进行了一次提交,如果直接push会存在冲突
- # 使用force将强制覆盖远程的那一次提交,需要确定那一次提交没有任何依赖
- git push --force
复制代码 实用场景
- 我在本地主分支上修改了代码,但是不想提交到主分支,而是提交到一个新的远程分支
- # 1. 先创建一个新的本地分支
- git checkout -b new_branch
- # 2. 查看一下当前本地分支
- git branch
- > master
- > * new_branch # 表明新分支已经创建,且切换到了该分支上
- # 3. 在该分支上提交代码
- # 4. 将该分支推到远程
- git push origin new_branch:remote_new_branch
复制代码 - 我要拉取远程的子分支到本地,然后将修改合并到主分支,然后删除子分支
- # 0. 查看所有分支
- git branch -a
- # 1. 拉取远程子分支到本地
- git fetch origin remote_new_branch:temp # 拉取remote_new_branch到本地并命名为temp
- # 2. 比较分支差异
- git diff temp
- # 3. 合并分支
- git merge temp # 如果存在冲突,需要处理冲突
- # 4. 删除本地分支和远程子分支
- git branch -D temp
- git push origin --delete remote_new_branch
复制代码 - 将本地分支的内容推送到远程的两个不同分支
- # 1. 查看本地所处分支和所有远程分支
- git branch -a
- # 2. 修改当前分支内容
- # 3. 提交当前分支到远程对应分支
- git psush
- # 4. 切换到另一分支
- git checkout b
- # 5. 合并a分支的内容到b分支
- git merge a
- # 6. 将a分支提交到远程对应分支
- git push
复制代码 - 将分支的某次修改同步到主干/另一分支
- git log # 查看修改的commit-id
- git checkout xxxx # 切换到需要同步修改的分支
- git cherry-pick commit-id # 将修改合并到该分支
- # 如果存在冲突,需要解决冲突再commit
- git push
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |