git -- 项目开发最常用操作记录

2023-05-12,,

官方Git - Book

https://git-scm.com/book/zh/v2

------------------------------git配置以及公钥生成------------------------------------
1、安装git
命令行设置
$ git config --global user.name "Your Name" 配置用户名
$ git config --global user.email "email@example.com" 配置邮件
查看本地git 配置的命令:
git config --list
git config --global -l 查看全局配置(当前用户目录中的 .gitconfig文件一般在c:\users\Administrators中)
git config +环境变量名如user.name

2、创建本地git库
选定一个目录,git init (生成.git)

2.生成并部署SSH key(下次使用再总结)

--------------------------------项目中常用的git操作---------------------------------------------
1.克隆项目
git clone url(一般用ssh格式)

2.提交到本地库
git add file 添加到stage
git add . 将本地修改的所有文件添加到stage
git commit -m "......."

3.下载解决冲突(必要时手动解决冲突)
git pull origin branchName

4.提交代码
git push origin branchName提交服务器

-----------------------------其他指令-----------------------------------

git commit --amend 使用 --amend 可以修补提交消息(可在修改后先 git add 再 git commit --amend 修补刚才的提交)

git push 将本地git库推送到远程git库
推送时有2种可能:
1、远程git库自从上次pull之后没有发生过变化快速向前自动完成
2、远程git库已经被其他人push了新内容
git push origin blue -u 将本地分支推送到远程git库(origin)-u 将本地分支和远程git库中的blue分支关联起来
git push <远程git库名称> --tags 将所有标签推送到远程git库

git remote 查看与本地git库关联所有远程git库(一个本地git库可以向多个远程git库推送)
git remote -v 查看远程库的url及权限(verbose详细信息)
git remote show <远程库名字> 可以查看远程的基本信息(如:主分支名字,全部分支的列表,本地库和远程库之间的差异)

git log 查看历史提交,方便回退
git status 查看工作区的当前状态
git reflog 查看命令历史
git rm file 从版本库中删除一个文件(若要保存到git库需要commit,删除后checkout无法找回,只能用回退命令)

git checkout -- file 从git库中获取一个文件版本替换工作区文件版本。

----------------------------------------------------------------------------------------

git分支操作:生成,删除,切换

创建本地分支: git branch newBranchName
切换到新创建的本地分支: git checkout newBranchName
将新分支发布在github上: git push origin newBranchName
在本地删除一个分支: git branch -d newBranch
在github远程端删除一个分支: git push origin :newBranch (分支名前的冒号代表删除)
提交分支数据到远程服务器:git push origin <local_branch_name>:<remote_branch_name>(注意区分)
从服务器上拉取特定分支:git checkout --track origin/2.0.1.20120806(--track参数会让git自动切换到对应分支)

从已有分支创建新分支(如branch1):git checkout -b branch1;

--------------------------------------廖雪峰git教程笔记--------------------------------------------------
工作区(Working directory)、版本库(Repository),远程库(origin)
版本库包括暂存区(stage)、和当前分支如master
以下简称working,stage,branch
回顾下操作命令:
git add #将working的修改更新到stage
git commit #将stage提交到brbranch
git diff #是working和stage的比较
git diff ——cached #是stage与branch的比较

关联远程库:
$ git remote add origin git@github.com:yanbiao38156/learngit.git
第一次推送master分支内容:
git push -u origin master(以后不需要-u参数)

----------------------------------远程分支拉取到本地-------------------------------------------

1、新建文件夹myProject(假设远程分支叫dev)

2、进入myProject,执行git init(初始化git信息)

3、执行git remote add origin http://xxxxx.git(建立关联)

4、执行git fetch origin dev(拉取代码)

5、执行git checkout -b dev origin/dev(新建本地分支对应远程分支,并切换到这个分支)

6、执行git pull origin dev

git -- 项目开发最常用操作记录的相关教程结束。

《git -- 项目开发最常用操作记录.doc》

下载本文的Word格式文档,以方便收藏与打印。