赞
踩
这里面记录一下git的使用,只是平时工作中遇到的一些问题的解决方案,不会涉及到git的一些基础概念及说明。人的天性便是这般凉薄,只要拿更好的来换,一定舍得。
Git的一些使用
一、在码云建立好仓库之后,想把本地已经写好的代码推送上去。
首先git init我们的项目:
huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1
$ git init
Initialized empty Git repository in G:/Java/Go/program/2017-05-18/LearnPython1/.git/
添加项目到本地的仓库:
huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1 (master)
$ git add .
提交代码到本地仓库:
huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1 (master)
$ git commit-m 'commit python code'[master (root-commit) 4408093] commit python code110 files changed, 5905 insertions(+)
create mode100644 .idea/misc.xml
create mode100644 .idea/modules.xml
create mode100644 .idea/workspace.xml
create mode100644LearnPython1.iml
create mode100644 datagram/connMysql.py
create mode100644 datagram/funUtils.py
create mode100644 datagram/huhx.py
create mode100644 file/bookdata.csv
create mode100644 file/huhx.png
create mode100644 file/huhx1.txt
create mode100644 file/huhx2.txt
create mode100644 file/log.txt
create mode100644 file/log1.txt
create mode100644 file/seach_button.png
......
与服务器的分支建立联系:
huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1 (master)
$ git remote add origin https://gitee.com/huhx/pythonLearn.git
推送代码到远程仓库:
huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1 (master)
$ git push-f origin master
Counting objects:137, done.
Delta compressionusing up to 4threads.
Compressing objects:100% (130/130), done.
Writing objects:100% (137/137), 127.37 KiB | 1.46 MiB/s, done.
Total137 (delta 1), reused 0 (delta 0)
To https://gitee.com/huhx/pythonLearn.git
+ 45c1fff...4408093 master -> master (forced update)
注意上述的推送需要加-f参数,如果没有添加的话。会有如下的错误提示
huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1 (master)
$ git push origin master
To https://gitee.com/huhx/pythonLearn.git
! [rejected] master ->master (fetch first)
error: failed to push some refs to'https://gitee.com/huhx/pythonLearn.git'hint: Updates were rejected because the remote contains work that youdohint: not have locally. Thisisusually caused by another repository pushing
hint: to the sameref. 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 checkout huhx-dev
添加到git管理:
git add .
提交到huhx-dev本地仓库:
git commit -m 'code'切换到master分支:
git checkout master
从远程仓库更新代码:
git pull
合并huhx-dev分支:
git merge huhx-dev
提交到本地的mater仓库:
git commit-m 'master commit'将本地的master代码提交到远程:
git push origin master
三、git撤销的操作
我们需要撤销上次提交的commit,注意这里还没有执行push的操作。举个例子来加以说明这个撤销的过程。比如我们我们修改提交了文件redis.txt文件。
$ git add code/imp-root/imp-db/impserver_db/redis.txt
此时如果我们要撤销git add的操作,可以输入以下的命令:
$ git rm --cached code/imp-root/imp-db/impserver_db/redis.txt
rm'code/imp-root/imp-db/impserver_db/redis.txt'
我们接着去提交redis.txt,先git add再git commit操作。命令如下:
$ git add code/imp-root/imp-db/impserver_db/redis.txt
$ git commit-m 'test commit'[master49e3162f] test commit1 file changed, 3 insertions(+), 1 deletion(-)
现在我们开始commit的撤销操作,这个操作分为三种类型。git reset –-mixed、git reset –-soft和git reset –-hard。
git reset –-mixed:此为默认方式,不带任何参数的git reset,它回退到某个版本,保留修改后的文件源码,回退commit和index信息。也就是如果再提交,需要经过git add和git commit操作。
git reset –-soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。保留修改后的文件源码,如果还要提交,直接git commit即可。
git reset –-hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容。
我们对git reset –mixed这种方式做一个测试,也就是默认的git reset commitid。首先我们需要通过git log得到最后的commit id。如下
$ git log
commit 49e3162fb3cf244f809d9b38bc1ed9c5e651ea49 (HEAD->master)
Author: huhongxiangDate: Wed Dec27 18:23:02 2017 +0800test commit
commit ce477925b378e8d3ec451caafb0b72c811dc697b (origin/master)
Author: huhongxiangDate: Wed Dec27 17:40:11 2017 +0800卡券赠送
我们要回退到上一个版本,也就是commitId=ce477925b378e8d3ec451caafb0b72c811dc697b。所以执行撤销的命令如下:
$ git reset ce477925b378e8d3ec451caafb0b72c811dc697b
Unstaged changes after reset:
M code/imp-root/imp-db/impserver_db/redis.txt
再通过git status,可以看到redis.txt也回退了index的信息。
$ git status
On branch master
Your branchis up to date with 'origin/master'.
Changes not stagedforcommit:
(use"git add ..."to update what will be committed)
(use"git checkout -- ..." to discard changes inworking directory)
modified: code/imp-root/imp-db/impserver_db/redis.txt
此时执行git log,可以看到如下的输出。可以知道已经看不到redis.txt的commit提交记录了。
$ git log
commit ce477925b378e8d3ec451caafb0b72c811dc697b (HEAD-> master, origin/master)
Author: huhongxiangDate: Wed Dec27 17:40:11 2017 +0800卡券赠送
如果我们需要撤销git push到远程分支的操作,后续补充。
友情链接
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。