DevOps/GIT

github#9 git remote repository

aliceintr 2020. 11. 21. 06:22
반응형

다른사람과의 협업이나 나의 코드 백업을 위해서는 원격저장소가 remote repository 가 필요하다.

 

전체적인 흐름을 보면 아래와 같다.

ref : https://www.git-tower.com/learn/git/ebook/en/command-line/remote-repositories/introduction/

 

1. local 과 remote repository 개념 이해하기

이번 실습은 remote repository 이 로컬머신에 있는 경우의 실습이다.

이제 나의 machine 에 local repository 를 만들어 보자.

지금 작업하고 있는 working directory 아래에 local repository를 위한 새로운 directory를 만든다.

 

% mkdir local 
% cd ..
% cd gitproject/local

#first.txt라는 파일을 만들고 add하고 commit 함
% vi first.txt
% git add first.txt
% git commit -m "first commit"
[master 6abd1bf] first commit
 create mode 100644 local/first.txt

이제는 remote repository 를 만들어 보자

% cd ..
#local direcotry 가 아닌 데에 remote 라는 이름의 디렉토리 생성
# --bare 옵션은 이 디렉토리는 다른사람과의 협업을 위한 것이기 때문에 파일이 절대 변경되지 않는다. 수정불가
% git init --bare remote
Initialized empty Git repository in /Users/alice/Documents/gitproject/remote/
% cd remote
ls -al
total 24
drwxr-xr-x   9 alice  staff  288 20 Nov 14:18 .
drwxr-xr-x  15 alice  staff  480 20 Nov 14:18 ..
-rw-r--r--   1 alice  staff   23 20 Nov 14:18 HEAD
-rw-r--r--   1 alice  staff  111 20 Nov 14:18 config
-rw-r--r--   1 alice  staff   73 20 Nov 14:18 description
drwxr-xr-x  14 alice  staff  448 20 Nov 14:18 hooks
drwxr-xr-x   3 alice  staff   96 20 Nov 14:18 info
drwxr-xr-x   4 alice  staff  128 20 Nov 14:18 objects
drwxr-xr-x   4 alice  staff  128 20 Nov 14:18 refs

#local 디렉토리의 커밋 내용을 remote에도 똑같이 적용하자 
#생성한 remote 디렉토리를 origin이라는 alias 지정해준다. 
#remote 디렉토리의 위치를 저장해서 사용한다고 이해하면 쉽다. 

% git remote add origin /Users/alice/Documents/gitproject/remote
% git remote -v
origin	/Users/alice/Documents/gitproject/remote (fetch)
origin	/Users/alice/Documents/gitproject/remote (push)

# local 에서 remote 로 push 하기전에 글로벌 셋팅 하나 해주고 
% git config --global push.default simple

#push 를 하면
% git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

% git branch
* master

#에러가 나기 때문에 아래의 명령어로 세팅을 다시해준다. 
% git push --set-upstream origin master
Enumerating objects: 80, done.
Counting objects: 100% (80/80), done.
Delta compression using up to 8 threads
Compressing objects: 100% (64/64), done.
Writing objects: 100% (80/80), 17.65 KiB | 2.94 MiB/s, done.
Total 80 (delta 23), reused 0 (delta 0), pack-reused 0
To /Users/alice/Documents/gitproject/remote
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

#remote 디렉토리에 가서 log를 확인하면 아까 local 에서 commit한 기록을 여기서도 볼 수 있다. 
% cd ../remote
% git log
commit 6abd1bf3282400e74687ce7ed2e0b1d8c79bf90c (HEAD -> master)
Author: aliceson89 <alice.son1109@gmail.com>
Date:   Fri Nov 20 14:18:15 2020 -0500

    first commit

 


2. github을 remote repository 로 이용하기

기존에 github.com 에 있는 코드를 나의 로컬 머신에 clone 해보도록 하자.

git clone [github.com address] [저장하고자하는 directory이름(새디렉토리도 가능)]

 

clone 하고자 하는 github 페이지로 들어가서  http 부분을 copy 하고 terminal 에 명령어를 실행한다.

% git clone https://github.com/aliceson89/JavaLearn.git javacode
Cloning into 'javacode'...
remote: Enumerating objects: 54, done.
remote: Counting objects: 100% (54/54), done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 54 (delta 12), reused 46 (delta 11), pack-reused 0
Receiving objects: 100% (54/54), 20.24 KiB | 4.05 MiB/s, done.
Resolving deltas: 100% (12/12), done.

% cd javacode
% ls -ltr
total 48
-rw-r--r--  1 alice  staff    39 20 Nov 15:22 README.md
-rw-r--r--  1 alice  staff  4652 20 Nov 15:22 S03_DataType.java
-rw-r--r--  1 alice  staff  1470 20 Nov 15:22 S03_exercise.java
-rw-r--r--  1 alice  staff  3991 20 Nov 15:22 S04_Condition.java
-rw-r--r--  1 alice  staff  2136 20 Nov 15:22 S04_Operator.java

javacode 안에 github.com 에 있던 코드가 clone 된 것을 확인할 수 있다.

이제 githhub.com 에 있는 원격저장소를 remote 명령어로 나의 로컬 저장소와 연결시킨다.

% git remote add java https://github.com/aliceson89/JavaLearn.git
% git remote -v 
java	https://github.com/aliceson89/JavaLearn.git (fetch)
java	https://github.com/aliceson89/JavaLearn.git (push)

간단하게 git remote 명령어 description

 

NAME
       git-remote - Manage set of tracked repositories

SYNOPSIS
       git remote [-v | --verbose]
       git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <url>
       git remote rename <old> <new>
       git remote remove <name>
       git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
       git remote set-branches [--add] <name> <branch>...
       git remote get-url [--push] [--all] <name>
       git remote set-url [--push] <name> <newurl> [<oldurl>]
       git remote set-url --add [--push] <name> <newurl>
       git remote set-url --delete [--push] <name> <url>
       git remote [-v | --verbose] show [-n] <name>...
       git remote prune [-n | --dry-run] <name>...
       git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]

DESCRIPTION
       Manage the set of repositories ("remotes") whose branches you track.

OPTIONS
       -v, --verbose
           Be a little more verbose and show remote url after name. NOTE: This
           must be placed between remote and subcommand.

COMMANDS
       With no arguments, shows a list of existing remotes. Several
       subcommands are available to perform operations on the remotes.

       add
           Add a remote named <name> for the repository at <url>. The command
           git fetch <name> can then be used to create and update
           remote-tracking branches <name>/<branch>.

           With -f option, git fetch <name> is run immediately after the
           remote information is set up.

           With --tags option, git fetch <name> imports every tag from the
           remote repository.

           With --no-tags option, git fetch <name> does not import tags from
           the remote repository.

           By default, only tags on fetched branches are imported (see git-
           fetch(1)).
With -t <branch> option, instead of the default glob refspec for
           the remote to track all branches under the refs/remotes/<name>/
           namespace, a refspec to track only <branch> is created. You can
           give more than one -t <branch> to track multiple branches without
           grabbing all branches.

           With -m <master> option, a symbolic-ref refs/remotes/<name>/HEAD is
           set up to point at remote's <master> branch. See also the set-head
           command.

           When a fetch mirror is created with --mirror=fetch, the refs will
           not be stored in the refs/remotes/ namespace, but rather everything
           in refs/ on the remote will be directly mirrored into refs/ in the
           local repository. This option only makes sense in bare
           repositories, because a fetch would overwrite any local commits.

           When a push mirror is created with --mirror=push, then git push
           will always behave as if --mirror was passed.

       rename
           Rename the remote named <old> to <new>. All remote-tracking
           branches and configuration settings for the remote are updated.

           In case <old> and <new> are the same, and <old> is a file under
           $GIT_DIR/remotes or $GIT_DIR/branches, the remote is converted to
           the configuration file format.

       remove, rm
           Remove the remote named <name>. All remote-tracking branches and
           configuration settings for the remote are removed.

       set-head
           Sets or deletes the default branch (i.e. the target of the
           symbolic-ref refs/remotes/<name>/HEAD) for the named remote. Having
           a default branch for a remote is not required, but allows the name
           of the remote to be specified in lieu of a specific branch. For
           example, if the default branch for origin is set to master, then
           origin may be specified wherever you would normally specify
           origin/master.

           With -d or --delete, the symbolic ref refs/remotes/<name>/HEAD is
           deleted.

           With -a or --auto, the remote is queried to determine its HEAD,
           then the symbolic-ref refs/remotes/<name>/HEAD is set to the same
           branch. e.g., if the remote HEAD is pointed at next, git remote
           set-head origin -a will set the symbolic-ref
           refs/remotes/origin/HEAD to refs/remotes/origin/next. This will
           only work if refs/remotes/origin/next already exists; if not it
           must be fetched first.

           Use <branch> to set the symbolic-ref refs/remotes/<name>/HEAD
           explicitly. e.g., git remote set-head origin master will set the
           symbolic-ref refs/remotes/origin/HEAD to
           refs/remotes/origin/master. This will only work if
           refs/remotes/origin/master already exists; if not it must be
           fetched first.

       set-branches
           Changes the list of branches tracked by the named remote. This can
           be used to track a subset of the available remote branches after
           the initial setup for a remote.

           The named branches will be interpreted as if specified with the -t
           option on the git remote add command line.

           With --add, instead of replacing the list of currently tracked
           branches, adds to that list.

       get-url
           Retrieves the URLs for a remote. Configurations for insteadOf and
           pushInsteadOf are expanded here. By default, only the first URL is
           listed.

           With --push, push URLs are queried rather than fetch URLs.

           With --all, all URLs for the remote will be listed.
set-url
           Changes URLs for the remote. Sets first URL for remote <name> that
           matches regex <oldurl> (first URL if no <oldurl> is given) to
           <newurl>. If <oldurl> doesn't match any URL, an error occurs and
           nothing is changed.

           With --push, push URLs are manipulated instead of fetch URLs.

           With --add, instead of changing existing URLs, new URL is added.

           With --delete, instead of changing existing URLs, all URLs matching
           regex <url> are deleted for remote <name>. Trying to delete all
           non-push URLs is an error.

           Note that the push URL and the fetch URL, even though they can be
           set differently, must still refer to the same place. What you
           pushed to the push URL should be what you would see if you
           immediately fetched from the fetch URL. If you are trying to fetch
           from one place (e.g. your upstream) and push to another (e.g. your
           publishing repository), use two separate remotes.

       show
           Gives some information about the remote <name>.

           With -n option, the remote heads are not queried first with git
           ls-remote <name>; cached information is used instead.

       prune
           Deletes stale references associated with <name>. By default, stale
           remote-tracking branches under <name> are deleted, but depending on
           global configuration and the configuration of the remote we might
           even prune local tags that haven't been pushed there. Equivalent to
           git fetch --prune <name>, except that no new references will be
           fetched.

           See the PRUNING section of git-fetch(1) for what it'll prune
           depending on various configuration.

           With --dry-run option, report what branches would be pruned, but do
           not actually prune them.

       update
           Fetch updates for remotes or remote groups in the repository as
           defined by remotes.<group>. If neither group nor remote is
           specified on the command line, the configuration parameter
           remotes.default will be used; if remotes.default is not defined,
           all remotes which do not have the configuration parameter
           remote.<name>.skipDefaultUpdate set to true will be updated. (See
           git-config(1)).

           With --prune option, run pruning against all the remotes that are
           updated.

 

**NOTE : working directory에서 a.txt 라는 파일을 삭제 후(git rm)에 같은 내용의 파일로 다른이름인 b.txt 파일을 git add하면 자동으로 같은 파일로 인식해서

renamed:    S04_Condition.java -> S05_Condition.java

이런식으로 git status 가 표현된다.

 

이제 파일을 몇 개 추가하고 원격저장소로 git push를 해보자

% git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    S04_Condition.java
	deleted:    S04_Operator.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	S05_Condition.java
	S05_Operator.java
	S06_Forloop.java
	S06_modulooperator.java
	S06_nestingLoop.java
	S06_switch.java
	S06_while.java

% git add S06_*.java
% git add S05*
% git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	renamed:    S04_Condition.java -> S05_Condition.java
	renamed:    S04_Operator.java -> S05_Operator.java
	new file:   S06_Forloop.java
	new file:   S06_modulooperator.java
	new file:   S06_nestingLoop.java
	new file:   S06_switch.java
	new file:   S06_while.java
    

% git push -u origin master
Username for 'https://github.com': 
Password for 'https://aliceson89@github.com': 
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 8 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 4.17 KiB | 4.17 MiB/s, done.
Total 9 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/aliceson89/JavaLearn.git
   70a308f..7b90227  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

이제 github.com 의 나의 github repo에 파일이 잘 들어갔는지 확인한다.

 

파일이 아주 잘 업로드 된 것을 확인 할 수 있다.


3. github remote repository 동기화 방법

이번에는 같은 github.com 의 코드를 내 로컬에 2군데의 directory에 clone 해보자

% git clone https://github.com/aliceson89/JavaLearn.git git_home
Cloning into 'git_home'...
remote: Enumerating objects: 63, done.
remote: Counting objects: 100% (63/63), done.
remote: Compressing objects: 100% (43/43), done.
remote: Total 63 (delta 15), reused 53 (delta 11), pack-reused 0
Receiving objects: 100% (63/63), 23.25 KiB | 3.32 MiB/s, done.
Resolving deltas: 100% (15/15), done.


% git clone https://github.com/aliceson89/JavaLearn.git git_office
Cloning into 'git_office'...
remote: Enumerating objects: 63, done.
remote: Counting objects: 100% (63/63), done.
remote: Compressing objects: 100% (43/43), done.
remote: Total 63 (delta 15), reused 53 (delta 11), pack-reused 0
Receiving objects: 100% (63/63), 23.25 KiB | 2.32 MiB/s, done.
Resolving deltas: 100% (15/15), done.

작업 컴퓨터가 한대는 집에 한대는 회사에 있다고 가정하고 따로 로컬 폴더를 만든거라고 가정해 보자.

오늘 집에서 작업을 다 마치고 난뒤 1개의 새로운 파일을 github repository (github.com)에 업로드 했다.

% ls -ltr
total 96
-rw-r--r--  1 alice  staff    39 20 Nov 16:09 README.md
-rw-r--r--  1 alice  staff  4652 20 Nov 16:09 S03_DataType.java
-rw-r--r--  1 alice  staff  1470 20 Nov 16:09 S03_exercise.java
-rw-r--r--  1 alice  staff  3991 20 Nov 16:09 S05_Condition.java
-rw-r--r--  1 alice  staff  2137 20 Nov 16:09 S05_Operator.java
-rw-r--r--  1 alice  staff  1040 20 Nov 16:09 S06_Forloop.java
-rw-r--r--  1 alice  staff  2534 20 Nov 16:09 S06_modulooperator.java
-rw-r--r--  1 alice  staff   259 20 Nov 16:09 S06_nestingLoop.java
-rw-r--r--  1 alice  staff  2696 20 Nov 16:09 S06_switch.java
-rw-r--r--  1 alice  staff  2133 20 Nov 16:09 S06_while.java
-rw-r--r--  1 alice  staff   745 20 Nov 16:13 Tutorial02.java
% git add Tutorial02.java 
% git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   Tutorial02.java

% git commit -m "home:Tutorial02.java"   
[master cd85b60] home:Tutorial02.java
 1 file changed, 39 insertions(+)
 create mode 100644 Tutorial02.java

% git push       
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 645 bytes | 645.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/aliceson89/JavaLearn.git
   7b90227..cd85b60  master -> master

그 후 내일아침이 되어 회사에서 일을 다시 시작할 때는 어떻게 시작해야 하는가?

이럴 때 사용하는 것이 git pull 명령어 이다.

% cd git_office
% git pull     
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:

  git config pull.rebase false  # merge (the default strategy)
  git config pull.rebase true   # rebase
  git config pull.ff only       # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.

remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), 625 bytes | 125.00 KiB/s, done.
From https://github.com/aliceson89/JavaLearn
   7b90227..cd85b60  master     -> origin/master
Updating 7b90227..cd85b60
Fast-forward
 Tutorial02.java | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 Tutorial02.java

% ls -ltr
total 96
-rw-r--r--  1 alice  staff    39 20 Nov 16:10 README.md
-rw-r--r--  1 alice  staff  4652 20 Nov 16:10 S03_DataType.java
-rw-r--r--  1 alice  staff  1470 20 Nov 16:10 S03_exercise.java
-rw-r--r--  1 alice  staff  3991 20 Nov 16:10 S05_Condition.java
-rw-r--r--  1 alice  staff  2137 20 Nov 16:10 S05_Operator.java
-rw-r--r--  1 alice  staff  1040 20 Nov 16:10 S06_Forloop.java
-rw-r--r--  1 alice  staff  2534 20 Nov 16:10 S06_modulooperator.java
-rw-r--r--  1 alice  staff   259 20 Nov 16:10 S06_nestingLoop.java
-rw-r--r--  1 alice  staff  2696 20 Nov 16:10 S06_switch.java
-rw-r--r--  1 alice  staff  2133 20 Nov 16:10 S06_while.java
-rw-r--r--  1 alice  staff   745 20 Nov 16:18 Tutorial02.java

마지막에 Tutorial02.java (git_home 에서 새로 add한 파일) 파일이 git_office 에도 생긴 것을 볼 수 있다.

 

내용이 도움이 되셨다면 블로그 구독하기 부탁드리겠습니다.

* 이 글의 모든 저작권은 aliceintr에 있으며 무단 배포 및 사용은 자제해 주시기 바랍니다. *

 

 

반응형

'DevOps > GIT' 카테고리의 다른 글

github#11 git tag 와 branch 차이  (0) 2020.11.25
github#10 git push and pull  (0) 2020.11.24
github#8 git stash  (0) 2020.11.20
github#7 3 ways merge  (0) 2020.11.20
github#6 git merge conflict  (0) 2020.11.20