8 minute read

[Git]원격 저장소를 만드는 방법 With GITHUB!

깃허브는 원격 저장소를 제공하고, 오픈 소스의 경우에는 원격 저장소를 무료로 제공한다!

01. 깃허브에 올라와 있는 오픈소스 원격 저장소를 로컬 저장소로 가져와서 소스를 수정하는 방법 git clone 오픈소스의_깃_주소 복제될_로컬_저장소_위치

GitHub - git/git: Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.

위의 git/git에 들어가자!

  • fork: 해당 프로젝트를 복제(contributor가 되더라도 수정 가능 but 원본에 영향을 주지 않음)

초록색으로 된 code 버튼을 누르고, HTTPS 탭의 “https://github.com/git/git.git“를 복사하자!

그리고 깃 배쉬에서 적당한 디렉토리로 이동을 하자!

나는 테스트용으로 remote_git_test 폴더를 만들것이다

그 후 git clone https://github.com/git/git.git gitsrc로 해당 프로젝트를 remote_git_test 내부에 gitsrc라는 폴더를 만듦과 동시에 그 폴더 안에 복제하자![복제+init]

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder
$ mkdir remote_git_test

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder
$ cd ./remote_git_test

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/remote_git_test
$ git clone https://github.com/git/git.git gitsrc
Cloning into 'gitsrc'...
remote: Enumerating objects: 312253, done.
remote: Counting objects: 100% (714/714), done.
remote: Compressing objects: 100% (317/317), done.
remote: Total 312253 (delta 460), reused 524 (delta 397), pack-reused 311539
Receiving objects: 100% (312253/312253), 169.76 MiB | 22.99 MiB/s, done.
Resolving deltas: 100% (232970/232970), done.
Updating files: 100% (3967/3967), done.

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/remote_git_test
$ ls -al
total 132
drwxr-xr-x 1 gs813 197609 0 Aug 22 14:23 ./
drwxr-xr-x 1 gs813 197609 0 Aug 22 14:22 ../
drwxr-xr-x 1 gs813 197609 0 Aug 22 14:24 gitsrc/

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/remote_git_test
$ cd ./gitsrc

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/remote_git_test/gitsrc (master)
$ git branch
* master

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/remote_git_test/gitsrc (master)
$ ls -al
total 8150
drwxr-xr-x 1 gs813 197609      0 Aug 22 14:24 ./
drwxr-xr-x 1 gs813 197609      0 Aug 22 14:23 ../
-rw-r--r-- 1 gs813 197609    327 Aug 22 14:24 .cirrus.yml
-rw-r--r-- 1 gs813 197609   5266 Aug 22 14:24 .clang-format
-rw-r--r-- 1 gs813 197609    304 Aug 22 14:24 .editorconfig
drwxr-xr-x 1 gs813 197609      0 Aug 22 14:24 .git/
(생략)

그러면 위와 같이 gitsrc라는 폴더가 만들어지고, 그 내부에 해당 프로젝트가 복제되었음을 확인해볼 수 있다

로그를 찍어볼까?

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/remote_git_test/gitsrc (master)
$ git log --branches --decorate
commit 225bc32a989d7a22fa6addafd4ce7dcd04675dbf (HEAD -> master, tag: v2.33.0, origin/master, origin/HEAD)
Author: Junio C Hamano <gitster@pobox.com>
Date:   Mon Aug 16 12:15:44 2021 -0700

    Git 2.33

    Signed-off-by: Junio C Hamano <gitster@pobox.com>

commit b06a5047eee5132a12752822114abc546462dffb
Merge: f7cd3c0832 8bcda98da5
Author: Junio C Hamano <gitster@pobox.com>
Date:   Mon Aug 16 12:14:35 2021 -0700

    Merge branch 'rs/oidtree-alignment-fix'

    Codepath to access recently added oidtree data structure had
    to make unaligned accesses to oids, which has been corrected.

    * rs/oidtree-alignment-fix:
      oidtree: avoid unaligned access to crit-bit tree

그러면 위와 같은 커밋들을 살펴볼 수 있다

🌟 git log --reverse : 로그를 역순으로 출력하여, 오름차순으로 , 즉 옛날부터 확인해볼 수 있음!

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/remote_git_test/gitsrc (master)
$ git log --branches --decorate --reverse
commit e83c5163316f89bfbde7d9ab23ca2e25604af290
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Thu Apr 7 15:13:13 2005 -0700

    Initial revision of "git", the information manager from hell

commit 8bc9a0c769ac1df7820f2dbf8f7b7d64835e3c68
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Thu Apr 7 15:16:10 2005 -0700

    Add copyright notices.

    The tool interface sucks (especially "committing" information, which is just
    me doing everything by hand from the command line), but I think this is in
    theory actually a viable way of describing the world. So copyright it.

commit e497ea2a9b6c378f01d092c210af20cbee762475
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Thu Apr 7 21:03:28 2005 -0700

    Make read-tree actually unpack the whole tree.

    I needed this to make a "sparse" archive conversion from my old

그러면 깃의 첫 번째 커밋을 확인해볼 수 있는데, 메시지가 재밌다 😸

지옥에서 온 정보 매니저인 깃의 초기 버전 이라니 😭😄

그리고 첫 번째 커밋의 소스코드를 보고 싶다면

git checkout e83c5163316f89bfbde7d9ab23ca2e25604af290 를 실행해주면

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/remote_git_test/gitsrc (master)
$ git checkout e83c5163316f89bfbde7d9ab23ca2e25604af290
Updating files: 100% (3975/3975), done.
Note: switching to 'e83c5163316f89bfbde7d9ab23ca2e25604af290'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at e83c516331 Initial revision of "git", the information manager from hell

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/remote_git_test/gitsrc **((e83c516331...))**

위와 같이 master 브랜치가 아닌 e83c516 브랜치로 이동한 것을 확인해볼 수 있다

그리고 로그와 파일 목록을 확인해보니, 로그는 우리가 확인했던 한 개가 존재하고 , 헤더파일과 c 언어로 작성된 파일이 존재하는 것을 확인해볼 수 있다

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/remote_git_test/gitsrc ((e83c516331...))
$ git log
commit e83c5163316f89bfbde7d9ab23ca2e25604af290 (HEAD)
Author: Linus Torvalds <torvalds@ppc970.osdl.org>
Date:   Thu Apr 7 15:13:13 2005 -0700

    Initial revision of "git", the information manager from hell

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/remote_git_test/gitsrc ((e83c516331...))
$ ls -al
total 145
drwxr-xr-x 1 gs813 197609    0 Aug 22 14:32 ./
drwxr-xr-x 1 gs813 197609    0 Aug 22 14:23 ../
drwxr-xr-x 1 gs813 197609    0 Aug 22 14:32 .git/
-rw-r--r-- 1 gs813 197609  997 Aug 22 14:32 Makefile
-rw-r--r-- 1 gs813 197609 8560 Aug 22 14:32 README
-rw-r--r-- 1 gs813 197609 2577 Aug 22 14:32 cache.h
-rw-r--r-- 1 gs813 197609  526 Aug 22 14:32 cat-file.c
-rw-r--r-- 1 gs813 197609 4275 Aug 22 14:32 commit-tree.c
-rw-r--r-- 1 gs813 197609 1249 Aug 22 14:32 init-db.c
-rw-r--r-- 1 gs813 197609 5940 Aug 22 14:32 read-cache.c
-rw-r--r-- 1 gs813 197609 1029 Aug 22 14:32 read-tree.c
-rw-r--r-- 1 gs813 197609 2115 Aug 22 14:32 show-diff.c
-rw-r--r-- 1 gs813 197609 5643 Aug 22 14:32 update-cache.c
-rw-r--r-- 1 gs813 197609 1507 Aug 22 14:32 write-tree.c

02. 내 계정에서 원격 저장소를 만들고, 로컬 저장소와 연결하기! git remote add origin ~git push -u origin master/main ▶️ 백업의 의미가 짙음!

먼저 테스트용으로 깃허브에 들어가서 repositories에서 new (repository)를 누르고, repository name으로 git_remote_test 를 입력 후 접근권한을 public으로 둔 후 Initialize this repository with: Add a README file(프로젝트 설명 파일 생성)을 우선은 체크하지 않도록 하겠다

그 후 create repository를 해주자

  • public : 오픈소스로 만들 때(무료)
  • private : 공개하지 않는 것! (유료인지 무료인지는 잘 모르겠다)

  • gitignore: 프로젝트에 원하지 않는 백업파일이나 로그파일, 컴파일된 파일들을 깃에서 제외 설정하는 파일

Git .gitignore 적용하기

이수진의 블로그

그렇게 원격 저장소를 만들면 아래와 같은 화면이 나타나는데

https://github.com/hy6219/TIL/blob/main/gitStudy/doItStudy/versionControl/%EC%9B%90%EA%B2%A9%20%EC%A0%80%EC%9E%A5%EC%86%8C%EC%99%80%20%EB%A1%9C%EC%BB%AC%20%EC%A0%80%EC%9E%A5%EC%86%8C%EB%A5%BC%20%EC%97%B0%EA%B2%B0%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95-%EA%B9%83%ED%97%88%EB%B8%8C.PNG?raw=true

a 부분이 바로 원격저장소 고유의 위치가 되는 것이다

그리고 밑을 보면 로컬저장소와 원격저장소를 연결하는 방법을 CLI 명령어들로 설명되어 있는데

  • 상황1 : 원격 저장소 생성 후, 복제해서 로컬 저장소를 만들어서, 로컬 저장소에서 작업(b)
  • 상황 2: 작업 중이던 로컬 저장소를 원격 저장소에 올리는 것(c)

상황 2부터 진행해보자

02-1. 작업 중이던 로컬 저장소를 원격 저장소에 연결하기

ⓐ 로컬 저장소를 만들고, 그 저장소에 간단한 작업물 만들기

hellGit이라는 레포지토리를 만들고, git init 으로 프로젝트를 생성해주자

그리고 hello.txt라는 파일을 간단히 만들고 커밋까지 진행해보자

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder
$ mkdir hellGit

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit
$ git init
Initialized empty Git repository in D:/VirtualBox_share_folder/hellGit/.git/
gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ vim hello.txt

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ cat hello.txt
hello git!

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git commit -m 'test:make local repo'
[master (root-commit) e2155a2] test:make local repo
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt

ⓑ 로컬 저장소 위치에서 git remote add origin 깃허브의_원격저장소_위치

익숙하지 않은가? 저번에 origin이 위치의 별명이고, 로컬과 원격저장소를 연결하는 첫 단계였다! 기억이 안난다면 아래로!!~[기존 작업트리에 새로운 원격 저장소(위의 주소 ~.git=origin)를 추가]

TIL/[Git]Remote Repository Overview.md at main · hy6219/TIL

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git remote add origin https://github.com/hy6219/git_remote_test.git

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git remote -v
origin  https://github.com/hy6219/git_remote_test.git (fetch)
origin  https://github.com/hy6219/git_remote_test.git (push)

그리고 git remote -v를 통해서(저번처럼) 원격 저장소가 추가된 것을 확인해볼 수 있다

📌 깃허브를 통해서 여러 개의 원격 저장소를 하나의 로컬 저장소 내에 저장할 수 있다!

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git remote add dev https://github.com/hy6219/git_remote_test.git

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git remote -v
dev     https://github.com/hy6219/git_remote_test.git (fetch)
dev     https://github.com/hy6219/git_remote_test.git (push)
origin  https://github.com/hy6219/git_remote_test.git (fetch)
origin  https://github.com/hy6219/git_remote_test.git (push)

🌟 origin이라는 원격 저장소는 로컬 저장소와 연결되어 있는 기본적인 원격 저장소로 관용적으로 많이 사용된다!

  • 필요없는 dev 저장소를 삭제하도록 하자
gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git remote remove dev

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git remote -v
origin  https://github.com/hy6219/git_remote_test.git (fetch)
origin  https://github.com/hy6219/git_remote_test.git (push)

git push -u origin master : 현재 체크아웃하고 있는 로컬 저장소의 브랜치를 origin에 해당된 원격 저장소master라는 이름의 브랜치로 동기화

  • git push : 로컬 저장소를 원격 저장소에 끼워 넣는 것(깃은 대부분 로컬 저장소를 기준으로 설명)
  • -u 옵션: 로컬 저장소와 원격 저장소를 연결시켜서 그 다음에는 git push 만으로도 자동으로 master 이름으로 업로드 되도록 지원
gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 227 bytes | 113.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/hy6219/git_remote_test.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Username과 비밀번호를 입력하라고 뜨면 입력해주면 된다

(나의 경우에는 뜨지 않았다)

그리고 나서 깃허브의 원격 저장소를 새로고침하면 아래처럼 hello.txt가 확인된다!

https://github.com/hy6219/TIL/blob/main/gitStudy/doItStudy/versionControl/%EC%9B%90%EA%B2%A9%20%EC%A0%80%EC%9E%A5%EC%86%8C%EC%99%80%20%EB%A1%9C%EC%BB%AC%20%EC%A0%80%EC%9E%A5%EC%86%8C%EB%A5%BC%20%EC%97%B0%EA%B2%B0%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95-%EA%B9%83%ED%97%88%EB%B8%8C02.PNG?raw=true

이번에는 hello.txt의 내용을 수정해서 업로드해보자

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ vim hello.txt

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ cat hello.txt
hello git!
update content!!

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git commit -m 'test:modify hello.txt'
[master 7e0301f] test:modify hello.txt
 1 file changed, 1 insertion(+)

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/hellGit (master)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 277 bytes | 138.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/hy6219/git_remote_test.git
   f2acfc3..7e0301f  master -> master

🌟 그러면 이전에 git push -u origin master 로 기본 푸쉬 브랜치를 설정해두었기 때문에 git push만 해주어도 master 브랜치로 업로드할 수 있는 것을 확인해볼 수 있다!

02-2. 원격 저장소 생성 후, 복제해서 로컬 저장소를 만들어서, 로컬 저장소에서 작업 ▶️ 협업에 보다 초점!!

이미 1번에서 배운 것이나 다름없다!!

code라는 초록색 버튼을 눌러서 HTTPS 탭의 ~.git으로 끝나는 주소를 복사해서 특정 디렉토리에 복제!

git clone .git주소 🌟**.**🌟은 현재 위치에 복제하겠다는 의미!!

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder
$ mkdir collaboration

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder
$ cd ./collaboration

gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/collaboration
**$ git clone https://github.com/hy6219/git_remote_test.git .**
Cloning into '.'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.
gs813@DESKTOP-4A8Q9J9 MINGW64 /d/VirtualBox_share_folder/collaboration (master)
$ git remote -v
origin  https://github.com/hy6219/git_remote_test.git (fetch)
origin  https://github.com/hy6219/git_remote_test.git (push)

Updated: