Git常用场景下的操作

一、本地代码首次提交到远程仓库

方案一:

1. 在远程仓库上创建一个仓库

2. 克隆远程仓库

git clone 远程仓库地址

3. 将代码拷贝到本地仓库目录下

4. 提交到暂存区

git add .

5. 提交到本地仓库

git commit -m "注释"

6. git push

推送到远程仓库

 

方案二:

1.git init 

在gitRepos目录下 初始化一个git仓库

2.git add 

复制一个文件到gitStore目录下,然后执行git add .

将“修改”从当前工作区存放到暂存区

3.git commit -m "first commit"

将暂存区中存放的文件提交到git仓库

4.在远端新建一个git代码库

5.git remote add origin 远端仓库git地址

将本地代码库的当前分支与远程的代码库相关联

6.git push -u origin master

将本地代码库的当前分支强制推送到远程的代码库(这样会使远程修改丢失,尤其是在多人协作开发时是不可取的)

 

第6步中,如果使用 git push,会出现错误提示,

如下图所示:

Git常用场景下的操作_第1张图片

使用 git push --set-upstream origin master,会出现如下错误

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'http://111.225.223.40:88/fxb/fxb_dashboard.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
如下图所示:

Git常用场景下的操作_第2张图片

按照提示执行 git pull命令,出现如下错误:

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/ master

如下图所示:

Git常用场景下的操作_第3张图片

根据提示执行 git push --set-upstream origin master,出现如下错误

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '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常用场景下的操作_第4张图片

执行 git pull origin master,出现如下错误:

fatal: refusing to merge unrelated histories

执行 git pull origin master --allow-unrelated-histories

成功pull,但是出现冲突:

解决冲突并add、commit后,再次执行 git pull命令,出现提示信息:

There is no tracking information for the current branch.

Git常用场景下的操作_第5张图片

执行命令 git branch --set-upstream-to=origin/master master

再次执行 git push,成功推送到远程仓库。

 

二、打标签

方案一:

git tag 标签名称

git tag 标签名称 commitid:为某一个commit定义标签

git tag -a 标签名称 -m "注释" commitid:为某一个commit定义标签时添加注释

方案二:利用GPG实现标签加密

1. 使用GPG生成自己的密钥

gpg --gen-key

完成后建议记录 密钥号(gpg key)、用户ID(uid)、密码(生成密钥时输入的密码)

2. 生成撤消的整数(可以不生成)

gpg --gen-revoke 密钥号

例如:gpg --gen-revoke 57E54640

3.使用gpg生成标签

(1) 设置git全局使用的gpg程序路径

git config --global gpg.program "C:\Program Files (x86)\GNU\GnuPG\gpg2.exe"

(2) 设置git全局要签名的key(也可以不设置)

如果设置此项,那么使用git tag命令时就不需要添加 -u 选项了

git config --global user.signingkey 密钥号

例如:git config --global user.signingkey 57E54640

(3) 使用GPG生成tag

git tag -u "密钥号" -s 标签名 -m "注释" commitId

例如:git tag -u "57E54640" -s v0.9 -m "生成gpg tag" 23aed86

注意:标签名中不能有中文

(4) 验证tag(可以不验证)

git tag -v 标签名称

(5) 向远程库推送tag

向远程库推送单个tag:

git push origin 标签名称

向远程库推送所有tag:

git push origin --tags

4. 删除标签

(1) 删除本地标签:

git tag -d 标签名称

创建的标签都只存储在本地,不会自动推送到远程。所以,打错的标签可以在本地安全删除。

(2) 删除远程标签:

如果标签已经推送到远程,先从本地删除,然后远程删除:

git push origin :refs/tags/标签名称

 

三、强制回退到某个历史版本再推送到远程仓库

1. 使用 git log 命令查看历史版本

git log

或者

git log --pretty=oneline

2. 回退到指定的历史版本

回退到指定的提交ID:

git reset --hard 提交ID

或者

回退到上一个版本:

git reset --hard HEAD~1

或者

回退到上一个版本:

git reset --hard HEAD^

3. 推送到远程

git push -f -u origin master

 

四、删除本地仓库和远程仓库中的文件

1. 删除本地仓库中的文件

git rm 文件名

直接删除文件之后,也需要使用 git rm 删除版本库中的文件。

2. 提交到本地仓库

git commit -m "注释"

3. git push

推送到远程仓库

 

不断更新中……

你可能感兴趣的:(Git)