git local remote repository

To learn or test the effect of some git commands, you can use a service provider like github, or you can build a server repository yourself. The latter is more convenient, faster, and better understand what git is.

 

1. Create a new git remote local warehouse folder

 

# switch directory
$ cd /Users/lnj/Desktop/git walkthrough/company/weibo                                                 
 % git init --bare                                 
Reinitialized existing Git repository in /home/matt/git/testgit.git/
 % ls                                               
branches/  config  description  HEAD  hooks/  info/  objects/  refs/

It can be seen from the ls command that the directory initialized by git init --bare is equivalent to the content of a .git directory. In this way, it is better to understand the essence of git: the .git of the local warehouse and the server warehouse. They are the same thing.

 

# Create a blank codebase

$ git init --bare

Note: This repository is only used to manage code and does not participate in development

 

2. The project manager initializes the project

 

2.1 First clone an empty warehouse to the local

git clone remote local warehouse folder directory

 

# switch directory

$ cd /Users/lnj/Desktop/git walkthrough/manager

# "clone" the codebase to local

$ git clone /Users/lnj/Desktop/git walkthrough/company/weibo/

 

# Personal information configuration (because to demonstrate multi-person collaboration on one machine, daily development can be ignored)

$ git config user.name manager

$ git config user.email [email protected]

 

 

2.2 Ignore files and folders that do not need to be added to the version controller

 

echo -e "# Compiled class file

*.class

 

# Log file

*.log

 

# BlueJ files

*.ctxt

 

# Mobile Tools for Java (J2ME)

.mtj.tmp /

 

# Package Files #

*.jar

*.war

*.ear

*.zip

* .tar.gz

*.rar

 

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml

hs_err_pid*" > .gitignore

 

 

Note: To configure ignore files only need to search on github. gitignore can copy the code written by others

          https://github.com/github/gitignore/blob/master/Java.gitignore

          configuration. gitignore must be in the same level as the .git hidden folder

 

 

2.3 After generating the .gitignore file, you also need to add the .gitignore file to version control

 

# Add .gitignore to the repository

$ git add .gitignore

git commit .gitignore -m "configure ignore files"

 

2.4 New project

 

 commit commit the code to the local repository

 push commits the code to the remote repository

 

A branch is created by default in git, which is called origin/master

 

% mkdir -p ~/tmp/testgit
% cd ~/tmp/testgit
% git init                                               
Initialized empty Git repository in /home/matt/tmp/testgit/.git/       
% touch README               
 % git add .                                        
 % git commit -m "initial commit"      
[master (root-commit) 02ecfad] initial commit                                       
 0 files changed                                                                   
 create mode 100644 README                                                           
 % git remote add origin localhost:/home/matt/git/testgit.git                         
 % git push -u origin master                  
Counting objects: 3, done.                                                            
Writing objects: 100% (3/3), 205 bytes, done.                                         
Total 3 (delta 0), reused 0 (delta 0)                                                 
To localhost:/home/matt/git/testgit.git                                               
 * [new branch]      master -> master                                                 
Branch master set up to track remote branch master from origin. 

 

 

* 如果想把本地的某个分支test提交到远程仓库,并作为远程仓库的master分支,或者作为另外一个名叫test的分支,如下:
$ git push origin test:master         // 提交本地test分支作为远程的master分支
$ git push origin test:test              // 提交本地test分支作为远程的test分支

 

查看当前的远程库

 

要查看当前配置有哪些远程仓库,可以用 git remote 命令,它会列出每个远程库的简短名字.在克隆完某个项目后,至少可以看到一个名为 origin 的远程库,Git 默认使用这个名字来标识你所克隆的原始仓库:

(1)git remote 不带参数,列出已经存在的远程分支

  $ git remote

 

  origin

 

(2)git remote -v | --verbose 列出详细信息,在每一个名字后面列出其远程url

此时, -v 选项(译注:此为 –verbose 的简写,取首字母),显示对应的克隆地址:

  $ git remote -v

  origin git://github.com/schacon/ticgit.git如果有多个远程仓库,此命令将全部列出.比如在我的 Grit 项目中,可以看到:

  $ cd grit

  $ git remote -v

  bakkdoor git://github.com/bakkdoor/grit.git

  cho45 git://github.com/cho45/grit.git

  defunkt git://github.com/defunkt/grit.git

  koke git://github.com/koke/grit.git

 

  origin 这样一来,我就可以非常轻松地从这些用户的仓库中,拉取他们的提交到本地.请注意,上面列出的地址只有 origin 用的是 SSH URL 链接,所以也只有这个仓库我能推送数据上去

 

添加远程仓库

  要添加一个新的远程仓库,可以指定一个简单的名字,以便将来引用,运行 git remote add [shortname] [url]:

  $ git remote

  origin

  $ git remote add pb git://github.com/paulboone/ticgit.git

  $ git remote -v

  origin git://github.com/schacon/ticgit.git

  pb git://github.com/paulboone/ticgit.git现在可以用字串 pb 指代对应的仓库地址了.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326174712&siteId=291194637