github는 git에 대한 토론과 코드 저장소의 역할을 한다. git이 인터넷이 필요하지 않은 로컬 버전 관리 시스템이라면, github는 인터넷을 통한 공유 버전 관리 시스템이라고 생각하면 된다.
즉, 내 local git을 internet github에 올려서 협업을 위해 공유하거나, 내 코드를 백업하는 것이다.
git clone은 우리의 local에 없는 코드를 github에서 clone하여 가져오는 것이다. 재밌는 것은 git clone을 통해서 이전부터의 commit history와 git system들을 모두 가져올 수 있다는 것이다.
다음의 형식으로 clone이 가능하다.
git clone https://github.com/blah
모든 기록, 모든 커밋, 모든 파일들을 가져올 수 있다.
다음의 사이트에 접속하도록 하자.
https://github.com/gabrielecirulli/2048
github를 통해서 commit과 file, code들을 볼 수 있다.
여기서 clone을 하고 싶다면 가운데에 있는 Code를 누르고, url을 복사한 뒤에 git clone을 통해서 복사하면 된다.
git clone https://github.com/gabrielecirulli/2048.git
Cloning into '2048'...
remote: Enumerating objects: 1318, done.
remote: Counting objects: 100% (2/2), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 1318 (delta 0), reused 0 (delta 0), pack-reused 1316 (from 2)
Receiving objects: 100% (1318/1318), 625.64 KiB | 18.96 MiB/s, done.
Resolving deltas: 100% (765/765), done.
ls를 입력하면 2048이라는 디렉터리가 있을 것이다. 여기에 들어가면 github에서 봤던 file들이 있다.
cd 2048/
ls
CONTRIBUTING.md favicon.ico index.html js LICENSE.txt meta Rakefile README.md style
git log를 확인하면 commit log들을 볼 수 있다.
git log
commit 478b6ec346e3787f589e4af751378d06ded4cbbc (HEAD -> master, origin/master, origin/HEAD)
Author: Gabriele Cirulli <886011+gabrielecirulli@users.noreply.github.com>
Date: Thu Oct 24 15:06:19 2024 +0200
Update README.md
commit fc1ef4fe5a5fcccea7590f3e4c187c75980b353f
Author: Gabriele Cirulli <gabrielecirulli@users.noreply.github.com>
Date: Sat Oct 27 18:25:12 2018 +0200
Remove Adsense code
commit ffa8559d622e6c1b216e43de969f037204cdb102
Author: Gabriele Cirulli <gabrielecirulli@users.noreply.github.com>
Date: Sat Oct 27 13:21:45 2018 +0200
Add AdSense code
code를 수정해서 사용할 수도 있다.
github에서 code 저장소를 repository라고 한다. repository를 github에 만드는 방법은 다음과 같다.
demo라는 repository로 만들기로 하자. repository를 만들고 나면 다음의 글들이 나온다.
…or create a new repository on the command line
echo "# demo" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/colt/demo.git
git push -u origin main
github에 repository를 만들기는 했지만, 우리 local로 가져오진 않았다. 이 명령어들이 무슨 의미인지 알아보도록 하자.
우리의 local 저장소에 해당 github repository가 연결되어 있지 않다. 이를 가능하게 해주는 것이 git remote이다. remote가 바로 repository url인 것이다. 따라서, git remote는 local git에게 repository url을 알려주는 것이다.
현재의 local 저장소에 연결된 github repository를 확인하는 방법은 git remote -v이다.
git remote -v
아무 결과도 안나온다면 우리의 local 저장소에 아무런 repository가 연결되지 않았다는 것을 의미한다.
그럼 2048 project로 가서 git remote -v를 해보도록 하자.
git remote -v
origin https://github.com/gabrielecirulli/2048.git (fetch)
origin https://github.com/gabrielecirulli/2048.git (push)
remote의 이름과 url 값을 보여준다. 즉, remote의 이름이 origin이고, repo의 url이 https://github.com/gabrielecirulli/2048.git라는 것이다. 이 remote 주소를 통해서 우리의 local에서 원격지인 remote repository에 코드를 올리거나, 코드를 다운받거나, 리뷰를 올리거나 등을 할 수 있는 것이다.
local에 remote를 추가하는 방법은 다음과 같다.
git remote add <name> <origin>
만약 https://github.com/gabrielecirulli/2048.git url을 origin이라는 remote 이름으로 local 저장소에 연결하고 싶다면 다음과 같다.
git remote add origin https://github.com/gabrielecirulli/2048.git
origin이란 그냥 url 이름일 뿐이고 특별하진 않지만, 관례상 master branch 이름처럼 쓰는 것이다.
이제 다시 우리가 만든 github repository로 돌아가면 다음의 code가 있을 것이다.
…or push an existing repository from the command line
git remote add origin https://github.com/colt/demo.git
git branch -M main
git push -u origin main
git remote add origin https://github.com/colt/demo.git가 바로 origin으로 우리의 github repository인 https://github.com/colt/demo.git를 연결하겠다는 것이다. 해당 명령어를 입력하고 git remote -v를 실행하면 다음과 같다.
git remote -v
origin https://github.com/colt/demo.git(fetch)
origin https://github.com/colt/demo.git(push)
우리의 local 저장소에 remote 원격 저장소를 연결한 것이다.
git remote의 다른 명령어들은 다음과 같다.
git remote rename <old> <new>
git remote remove <name>
원래 가지고 있던 remote가 있었다면 git remote remove로 삭제하고, 다시 새로운 remote 주소를 넣어줄 수도 있는 것이다.
나중에 보겠지만, 보통 오픈소스 프로젝트를 하게되면 remote가 여러 개 가지고 있게 된다.
local 저장소에 remote repository를 연결하였으니, 우리의 local 저장소를 remote repository로 push하도록 하자.
git pish <remote> <branch>
remote가 바로 git remote로 설정했던 remote 이름이고 branch가 git의 branch를 말하는 것이다. 즉, remote라는 repository의 branch로 local 저장소 code를 넣으라는 것이다.
git push origin master
요즘은 master에서 main이지만 가장 흔한 git push 명령어이다. origin에 master branch로 local code를 올리라는 것이다.
git push를 하기 전에 먼저 git add와 git commit을 해야한다. 즉, git add로 working directory에 있는 file들을 staging area로 옮기고, git commit으로 local repository로 옮기도록 한다. git push는 local repository에 있는 file들을 remote repository로 옮기는 것이기 때문에 반드시 해주어야 한다.
git add .
git commit -m "first commit"
git branch -M master
git branch -M master의 경우는 현재 checkout된 branch를 master로 바꾸라는 것이다. 참고로 local git에만 영향이 있고 실제 remote repository에는 영향을 주지 않는다.
이제 git push를 해보도록 하자.
git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 2.75 KiB | 2.75 MiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/colt/demo.git
* [new branch] master -> master
우리의 github repository page에 가면 파일들이 올라온 것을 볼 수 있다.
다른 branch를 만들어 넣어보도록 하자.
git switch -c empty
echo "test" > ./test.txt
git add ./test.txt
git commit -m "add test.txt"
empty branch에 code를 넣어보기 전에 현재의 상황을 보도록 하자.
master --> (first commit)
|
emtpy ----(add test.txt)
master --> (first commit)
다음과 같은 상황인 것이다. github repo에도 empty branch의 commit을 추가해주도록 하자.
git push origin empty
empty branch가 추가된 것을 볼 수 있다.
흔하지 않지만, 재미난 것도 가능하다. 지금은 local branch와 remote branch의 이름을 동일하게 했는데, 다음과 같이 달리할 수도 있다.
git push origin cats:dogs
local branch에서는 cats였지만, 원격 branch에는 dogs로 올라간다.
그런데 새 저장소를 만들면 보이는 -u 옵션은 무엇일까??
git push -u origin main
-u는 upstream을 의미하는데, 원격 branch 중에 어떤 것을 default upstream branch로 가져갈 것이냐를 설정하는 것이다. 이렇게 되면 origin에서 main이 default가 되어, git push만 실행해도 git push origin main으로 실행된다.
현재는 upstream이 설정되어 있지 않아서 다음의 결과가 나올 것이다.
git push
fatal: The current branch empty has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin empty
upstream을 설정해보도록 하자.
git switch master
echo "test" >> ./test.txt
git add ./test.txt
git commit -m "add test.txt"
git push -u origin master
이제 upstream을 설정했으니, git push만 입력하여 remote repository에 code를 올려보도록 하자.
echo "end" >> ./test.txt
git add ./test.txt
git commit -m "append line in test.txt"
git push
git push만 해도 upstream으로 origin의 master를 참조하므로 바로 들어간다. 따라서, 처음 push할 때만 upstream 설정을 하고 그 후로는 안쓴다.