Git 使用

原文:https://www.cnblogs.com/conncui/p/git.html

 

 

拷貝一份Git,向更改他的遠程倉庫,同時保存原來的log

 

git push --mirror [email protected]:【名字】/ListViewAnimation.git

從劉俊偉拷貝的ListViewAnimation,保存了他的修改,現在遠程git是自己的

 

 1:在本地創建了Eclipse/AndroidStudio/Idea ..工程 或者任意文件夾子

將該目錄創建爲git倉庫

git init

得到一個git倉庫,可以進行git add /git commit 等操作

2:在github 或者 git.oschina.net 創建了遠程倉庫

3:欲將本地倉庫託管到遠程

git remote add origin URL

 4.在本地增加了新的分支branch_2,欲將該分支同步到託管服務器

git push orign branch_2

 

 
 
$ git push
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to '/work/fun/git_experiments/bare'

 the problem is that there are no commits in "bare". This is a problem with the first commit only, if you create the repos in the order (bare,alice). Try doing git push --set-upstream origin master. This would only be required the first time. Afterwards it should work normally.

 
   
   
   
   
   
   
   

 

 

 

dss886@dss886-OptiPlex-3020:~/testgit/server$ git status 
fatal: Not a git repository (or any parent up to mount point /home)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

當前目錄不是一個git,不是一個倉庫(但是他的子目錄可以是git /倉庫)    
dss886@dss886-OptiPlex-3020:~/testgit/server$ git status
位於分支 master
無文件要提交,乾淨的工作區

當前目錄或者當前倉庫 所有內容都被提交到倉庫(實際上是一種記錄)

也就是更新到了最新

   
Git hub

dss886@dss886-OptiPlex-3020:~/testgit$ mkdir github
dss886@dss886-OptiPlex-3020:~/testgit$ cd github/

<之前配置了在樂視代碼託管服務器上的賬戶 key

改賬戶無法在github上使用  當然了22222>
dss886@dss886-OptiPlex-3020:~/testgit/github$ git clone [email protected]:cghGit/bootstrap.git
正克隆到 'bootstrap'...
The authenticity of host 'github.com (192.30.252.128)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

 

Please make sure you have the correct access rights
and the repository exists.

   

同一客戶端多個git賬號的配置

http://my.oschina.net/csensix/blog/184434

 

 

http://blog.csdn.net/five3/article/details/8904635

 

 

 

摘要 有時候我們需要在同一臺機器上使用多個git賬號,爲了避免衝突,我們需要配置~/.ssh/config文件。

git 多賬號

 

步驟一:用ssh-keygen命令生成一組新的id_rsa_new和id_rsa_new.pub。

 

1

ssh-keygen -t rsa -C "new email"

 

平時我們都是直接回車,默認生成id_rsa和id_rsa.pub。這裏特別需要注意,出現提示輸入文件名的時候要輸入與默認配置不一樣的文件名,比如:  id_rsa_new。

步驟二:配置~/.ssh/config文件,以我自己的機器爲例。

 

1

2

3

4

5

6

7

8

9

10

11

#Default Git

Host defaultgit

  HostName IP Address #域名也可

  User think

  IdentityFile ~/.ssh/id_rsa

 

#Second Git

Host secondgit

  HostName IP Address #域名也可

  User think

  IdentityFile ~/.ssh/id_rsa_second

Host就是每個SSH連接的單獨代號,IdentityFile告訴SSH連接去讀取哪個私鑰。

步驟三:執行ssh-agent讓ssh識別新的私鑰。

 

1

ssh-add ~/.ssh/id_rsa_new

 

該命令如果報錯:Could not open a connection to your authentication agent.無法連接到ssh agent,可執行ssh-agent bash命令後再執行ssh-add命令。

以後,在clone或者add remote的時候,需要把config文件中的host代替git@remoteaddress中的remoteaddress。

同時,你可以通過在特定的repo下執行下面的命令,生成區別於全局設置的user.name和user.email。

 

1

2

3

4

5

git config user.name "newname"

git config user.email "newemail"

 

#git config --global --unset user.name 取消全局設置

#git config --global --unset user.email 取消全局設置

 

例子:

 

1

2

3

4

5

6

#在同一機器不同目錄下克隆遠程同一個repo

cd /home/user1

git clone git@defaultgit:xxx.git

 

cd /home/user2

git clone git@secondgit:xxx.git

上面的兩條clone命令,雖然關聯到同一個repo,卻是通過不同ssh連接,當然也是不同的git賬號。

 

 

=============================================================================================

 

Linux下使用git命令及github項目

在linux下搭建git環境
1、創建Github賬號,https://github.com
2、Linux創建SSH密鑰: 
  1. ssh-keygen -t rsa -C "new email" 回車使用當前目錄,也可指定
3、將公鑰加入到Github賬戶信息Account Settings->SSH Key

4,修改文件 .ssh/config

  添加

    
Host mygit1
  User conncuiGIT    //github 用戶名
  Hostname github.com
  IdentityFile 第一步生成的密鑰文件地址

 5:ssh-add  私鑰目錄

5、測試驗證是否成功。
  1. ssh -T [email protected]  
  2. Hi someone! You've successfully authenticated, but GitHub does not provide shell access.  

同步github到本地
1、複製項目到本地: 
  1. git clone git://github.com:xxxx/test.git ##以gitreadonly方式克隆到本地,只可以讀  
  2. git clone [email protected]:xxx/test.git  ##以SSH方式克隆到本地,可以讀寫  
  3. git clone https://github.com/xxx/test.git ##以https方式克隆到本地,可以讀寫  
  4. git fetch [email protected]:xxx/xxx.git  ##獲取到本地但不合並  
  5. git pull [email protected]:xxx/xxx.git ##獲取併合並內容到本地  

本地提交項目到github
1、本地配置 
  1. git config --global user.name 'onovps'  
  2. git config --global user.email '[email protected]' #全局聯繫方式,可選  
2、新建Git項目並提交到Github。
 

 

  1. mkdir testdir & cd testdir  
  2. touch README.md  
  3. git init #初始化一個本地庫  
  4. git add README.md #添加文件到本地倉庫  
  5. git rm README.md #本地倒庫內刪除  
  6. git commit -m "first commit" #提交到本地庫並備註,此時變更仍在本地。  
  7. git commit -a  ##自動更新變化的文件,a可以理解爲auto  
  8. git remote add xxx [email protected]:xxx/xxx.git  #增加一個遠程服務器的別名。  
  9. git remote rm xxx   ##刪除遠程版本庫的別名  
  10. git push -u remotename master #將本地文件提交到Github的remoname版本庫中。此時才更新了本地變更到github服務上。  

分支版本操作
1、創建和合並分支 
  1. git branch #顯示當前分支是master  
  2. git branch new-feature  #創建分支  
  3. git checkout new-feature  #切換到新分支  
  4. vi page_cache.inc.php  
  5. git add page_cache.inc.php  
  6. git commit -a -m "added initial version of page cache"  
  7. git push origin new-feature  ##把分支提交到遠程服務器,只是把分支結構和內容提交到遠程,並沒有發生和主幹的合併行爲。  
2、如果new-feature分支成熟了,覺得有必要合併進master 
  1. git checkout master  #切換到新主幹  
  2. git merge new-feature  ##把分支合併到主幹  
  3. git branch #顯示當前分支是master  
  4. git push  #此時主幹中也合併了new-feature的代碼  

git命令使用思維圖:【非常有料】

 

   
smartgit 圖形化界面工具 

下載地址http://www.syntevo.com/

下載後 在解壓目錄下/bin 中的smartgithg.sh  設置smartgit 的運行環境(要求  1.7)


if [ "$SMARTGITHG_JAVA_HOME" = "" ] ; then
SMARTGITHG_JAVA_HOME=$SMARTGIT_JAVA_HOME
fi
if [ "$SMARTGITHG_JAVA_HOME" = "" ] && [ -f "/usr/lib/jvm/java-7-openjdk-i386/jre/bin/java" ] ; then
SMARTGITHG_JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386/jre"
fi
if [ "$SMARTGITHG_JAVA_HOME" = "" ] ; then
SMARTGITHG_JAVA_HOME=$JAVA_HOME
fi

if [ "$SMARTGITHG_MAX_HEAP_SIZE" = "" ] ; then
SMARTGITHG_MAX_HEAP_SIZE=$SMARTGIT_MAX_HEAP_SIZE
fi
if [ "$SMARTGITHG_MAX_HEAP_SIZE" = "" ] ; then
SMARTGITHG_MAX_HEAP_SIZE=256m
fi
SMARTGITHG_JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64

   
 

 

 

本地代碼庫回滾

git reset --hard commit-id :回滾到commit-id,講commit-id之後提交的commit都去除

 

還有的時候,你希望查看某個指定文件的提交歷史。譬如你希望查看'notes.c'文件每一次的提交歷史

git log --pretty=oneline -- file

Git日誌

 用git管理Andoird  Studio,IntellJieda時,ide產生大量的臨時文件(gradle,build...)這些文件沒有必要納入git管理,所有用文件   .gitignore  來標記不納入git的文件,文件夾

build : .gitignore 所在父目錄下的所有build 將被忽略

/build:.gitignore 所在同等目錄下的build

.class :所有後綴爲class 的文件

 

 

問題:

利用.gitignore過濾文件,如編譯過程中的中間文件,等等,這些文件不需要被追蹤管理。

現象:文件,以過濾該文件,但是通過git status查看仍顯示file1文件的狀態。

原因

在git庫中已存在了這個文件,之前push提交過該文件。

.gitignore文件只對還沒有加入版本管理的文件起作用,如果之前已經用git把這些文件納入了版本庫,就不起作用了

解決

需要在git庫中刪除該文件,並更新。

然後再次git status查看狀態,file1文件不再顯示狀態。

複製代碼

ci@clinux:~/c/CuiDemo/studio/StudioProject$ git rm -r build
error: 如下文件有本地修改:
    StudioProject/build/intermediates/dex-cache/cache.xml
(使用 --cached 保留本地文件,或用 -f 強制刪除)
ci@clinux:~/c/CuiDemo/studio/StudioProject$ 

複製代碼

請使用:

ci@clinux:~/c/CuiDemo/studio/StudioProject$ git rm -r --cached build
rm 'StudioProject/build/intermediates/dex-cache/cache.xml'
rm 'StudioProject/build/intermediates/model_data.bin'
ci@clinux:~/c/CuiDemo/studio/StudioProject$ 

或者

ci@clinux:~/c/CuiDemo/studio/StudioProject$ git rm -r -f  build

 

 

   
   
   
   
   
   
   

複製代碼

廖雪峯的官方網站
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013752340242354807e192f02a44359908df8a5643103a000

使用Git管理本地項目
http://www.linuxidc.com/Linux/2014-03/98367.htm

 
和菜鳥一起學linux之本地git中心倉庫建立
http://blog.csdn.net/eastmoon502136/article/details/7735957
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章