git 命令简单记录

git 管理代码是目前较为流行的方式,由linus为了管理linux源码创立;

可以在window、linux等多个平台使用,本文记录在linux下的使用;

linux下安装方法:

sudo apt-get install git

创建用户名和邮箱

git config --global user.name

git config --global user.email "email@example.com"

创建版本号

mkdir learngit
cd learngit
pwd
/Users/michael/learngit

git init
在当前目录下面创建文本文件:
touch readme.txt
vi readme.txt
写入

this is the first time to write the txt,
good result will comes.

git add readme.txt

用命令git commit告诉Git,把文件提交到仓库 -m 后面是版本说明

git commit -m "wirte the readme for the first time"

用git status查看状态

git status

git diff readme.txt

查看更改历史:

git log

回退到上一个版本

git reset --hard HEAD^

回退到某个版本:

git reset --hard 2173

2173代码版本commit 前面四个数字

Git提供了一个命令git reflog用来记录你的每一次命令

删除掉git中某个文件

git rm

再git commit

 

远端仓库

ssh-keygen -t rsa -C  "youremail@example.com"

git init
echo "# gittest" >> README.md
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/EthanYangs/gittest.git

git push -u origin master

git clone https://github.com/EthanYangs/gittest.git
创建分支
git checkout -b dev
等同于如下两句命令 -b表示创建并切换
git branch dev
git checkout dev
查看分支
git branch
切换分支
git checkout master
合并dev下面的到master上
git merge dev

删除分支
git branch -d dev
强行删除,需要使用大写的-D参数
git branch -D feature-vulcan
 

合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息。

如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。

下面我们实战一下--no-ff方式的git merge

git merge --no-ff -m "merge with no-ff" dev
临时保存分支状态
先隐藏该分支更改
git stash
切换到其他分支解决问题:
git checkout master
再返回到原来分支继续工作:
git checkout dev
git stash list

工作现场还在,Git把stash内容存在某个地方了,但是需要恢复一下,有两个办法:

一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;

另一种方式是用git stash pop,恢复的同时把stash内容也删了:

 

在多人协作过程中,出现同一个文件已经被人提前修改,与本地文件不一致,然后本地需要提交,却失败了,

就需要同步远端到本地

git rebase
打标签
git tag v1.0
git log --pretty=oneline --abbrev-commit

比方说要对add merge这次提交打标签,它对应的commit id是f52c633,敲入命令:

$ git tag v0.9 f52c633

如果标签打错了,也可以删除:

$ git tag -d v0.1

如果要推送某个标签到远程,使用命令git push origin <tagname>

$ git push origin v1.0

一次性推送全部尚未推送到远程的本地标签:

$ git push origin --tags

如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除:

$ git tag -d v0.9
Deleted tag 'v0.9' (was f52c633)

然后,从远程删除。删除命令也是push,但是格式如下:

$ git push origin :refs/tags/v0.9
 
 

 
 
 


 

 

 

 

posted on 2019-08-09 17:08  昀木  阅读(160)  评论(0编辑  收藏  举报

导航