git常用命令

//配置用户名和邮箱,可以任意设置,不用完全和github的账号相同
git config --global user.name 'name'
git config --global user.email 'youemai@xx.com'

//查看配置
git config --list
git config --global --list
git config --local --list

//使用git创建项目
git init project_name

//添加管理文件
git add  file_name dictionary

//commit 
git commit -m'msg'

//查看git状态,看哪些文件没有git管理,哪些没有加入暂存区
git status

//已经被git管理的文件,一起保存到栈存区 -u代表update
git add -u 

//清除栈存区的内容
git reset --hard

//重命名文件名
git mv old_name new_name

//git log
git log --oneline 
git log -n3 --oneline

//查看本地有多少分支
git branch -av

//产生新分支
git checkout -b branchname xxxxxxx(基于commit号或分支)

//命令显示版本库对象的内容、类型及大小信息 -t是类型 -大小 -p查看内容
git cat-file -t hash

//两次commit的差异
git diff commit_hash   commit_hash
git diff HEAD HEAD~1
git diff 工作区和暂存区的区别 可以写多个文件,或不写默认所有

//删除分支  先用-d然后在用-D
git branch -D branch_name

//修改上次提交的msg
git commit --amend

//修改前几次的msg ,选择r
git rebase -i pre_commit_hash

//合并几次的commit,选择
git rebase -i pre_commit_hash 

//清空暂存区
git reset --hard HEAD   或者+ -- file_name   file_name选择暂存区文件和原文件保持一致

//工作区的文件恢复和暂存区一样
git checkout -- file_name

//消除最近几次的commit
git reset  --hard commit_hash  该commit以后的提交都会删除

//比较两个分支的不同
git diff branch01 branch02  -- file_name 可以选择具体的文件
git diff branch01_commit_hash branch02_commit_hash --file_name

//删除文件
git rm file_name

//临时加塞紧急任务可以把当前的任务放到对列中
git stash 放到对列中
git stash list 
git pop 恢复

//gitignore
test/  test目录是要版本控制,test目录下的文件不用版本控制 

//添加远程
git remote add short_name url

git pull = fetch +merge

//基于远程某个分支制作本地分支
git branch -av
git checkout -b  new_branch_name origin/feature/add_git_commands

git push -f 多人协作的是严格禁止的

.git 文件夹中 HEAD文件记录的是当前的分支或commit(分离头指针状态)
.git文件夹中的objects 文件夹名称和内部文件的hash组成文件
git可以只在本地运行,区别于集中式的版本管理工具
每次commit都是一次当前项目的快照,每次commit都有一个tree,blob是具体的文件

分离头指针:git checkout commit hash后,修改文件进行commit后由于当前是没有分支的,会出现警告,所以在操作的时候要把把commit和分支绑定在一起
工作区-》暂存区-》commit
HEAD指的是头部指针指向的那个commit_hash
两个人修改同一个分支,修改不同文件文件需要先进行merge然后
两个人修改同一个分支,修改同一个文件不同区域也是先pull然后在push
两个人修改同一个分支,修改同一个文件的同一个地方,后添加的需要处理冲突,然后commit
写代码前先执行git pull
两人人同时对一个文件修改文件名需要解决冲突,使用git rm file_name删除
fast forward: 当前分支分出一条新分支后,在新分支进行修改,然后跳转到前分支进行git merge 2nd_branch,会发生fast forward,前一个分支的头部commit会指向后一个分支的头部
git rebase会使原来有的commit合并看不到,在多人协同开发中不使用

工作之后的发现

一般项目会有多个分支,develop(开发)、sandbox(测试)、preview(预发布)、production(生产环境)
现在本地开发,然后合并和测试线,然后预发布,预发布和生产的数据库用同同一个的,最后才部署到生产环境
develop开发后提交,如果提交不成功需要先pull,提交完毕后,本地切换到sandbox分支,pull获取最新的代码,然后git merger develop 分支,合并到sanbox后push到远程,这样远程的sandbox分支就有了最新的develop分支的代码了,一般sandbox也会配置一个域名,可以写一个发布机程序一键部署到测试线项目
切换分支的时候如果有新写的代码没有commit会不让切换,可以使用git stash保存下来,当切换回来的时候再执行git pop即可

原文地址:https://www.cnblogs.com/lis2/p/14575727.html