java基礎---->git的使用(一)


  這里面記錄一下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 code
 110 files changed, 5905 insertions(+)
 create mode 100644 .idea/misc.xml
 create mode 100644 .idea/modules.xml
 create mode 100644 .idea/workspace.xml
 create mode 100644 LearnPython1.iml
 create mode 100644 datagram/connMysql.py
 create mode 100644 datagram/funUtils.py
 create mode 100644 datagram/huhx.py
 create mode 100644 file/bookdata.csv
 create mode 100644 file/huhx.png
 create mode 100644 file/huhx1.txt
 create mode 100644 file/huhx2.txt
 create mode 100644 file/log.txt
 create mode 100644 file/log1.txt
 create mode 100644 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 compression using up to 4 threads.
Compressing objects: 100% (130/130), done.
Writing objects: 100% (137/137), 127.37 KiB | 1.46 MiB/s, done.
Total 137 (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 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 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

關於分支創建、查看可以參考博客:http://blog.csdn.net/arkblue/article/details/9568249

 

三、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'
[master 49e3162f] test commit
 1 file changed, 3 insertions(+), 1 deletion(-)

現在我們開始commit的撤銷操作,這個操作分為三種類型。git reset –-mixedgit reset –-softgit 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: huhongxiang <huhongxiang@csii.com.cn>
Date:   Wed Dec 27 18:23:02 2017 +0800

    test commit

commit ce477925b378e8d3ec451caafb0b72c811dc697b (origin/master)
Author: huhongxiang <huhongxiang@csii.com.cn>
Date:   Wed Dec 27 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 branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working 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: huhongxiang <huhongxiang@csii.com.cn>
Date:   Wed Dec 27 17:40:11 2017 +0800

    卡券贈送

如果我們需要撤銷git push到遠程分支的操作,后續補充。

 

友情鏈接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM