Git - 7. 협업

BANG·2025년 9월 4일

git

목록 보기
7/7
$ git init
Initialized empty Git repository in C:/Work/gittest/.git/

$ vi work.txt

$ git add work.txt
warning: in the working copy of 'work.txt', LF will be replaced by CRLF the next time Git touches it


$ git commit -m "work 1"
[main (root-commit) b36d6eb] work 1
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 1 insertion(+)
 create mode 100644 work.txt

$ git remote add origin https://github.com/~~~/git-study.git


$ git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin main

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.


$ git push --set-upstream origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 207 bytes | 207.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/~~~/git-study.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.
  • 다른 컴퓨터에서 git을 clone 함
$ git clone https://github.com/~~~/git-study.git
Cloning into 'git-study'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (3/3), done.
  • 원래 컴퓨터에서 추가 작업 진행
$ vi work.txt

$ git commit -am "work 2a"
warning: in the working copy of 'work.txt', LF will be replaced by CRLF the next time Git touches it
[main 2202cea] work 2a
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 1 insertion(+)


$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 239 bytes | 239.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/~~~/git-study.git
   b36d6eb..2202cea  main -> main
  • 다른 컴퓨터에서 pull 받는거를 잊고, 그냥 작업 진행
$ vi work.txt

$ git commit -am "work 2b"
[main 26642c7] work 2b
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 1 insertion(+)
  • 서로의 컴퓨터의 git log 확인
$ git log --all --graph --oneline
* 2202cea (HEAD -> main, origin/main) work 2a
* b36d6eb work 1


$ git log --all --graph --oneline
* 26642c7 (HEAD -> main) work 2b
* b36d6eb (origin/main, origin/HEAD) work 1

원격에 다른 사람이 작업한 내용을 받지 않아서 거절됨

  • 다른 컴퓨터에서 작업한 내용을 원격 저장소로 push하면 거절됨!!!!!!!!
$ git push
To https://github.com/~~~/git-study.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/~~~/git-study.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

원격의 내용을 거절된 상태로 pull 받기

$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 219 bytes | 12.00 KiB/s, done.
From https://github.com/~~~/git-study
   b36d6eb..2202cea  main       -> origin/main
Auto-merging work.txt
CONFLICT (content): Merge conflict in work.txt
Automatic merge failed; fix conflicts and then commit the result.


$ cat work.txt
1
<<<<<<< HEAD		# <---- 현재 작업하는 브랜치
2b
=======				# <---- 구분자
2a
>>>>>>> 2202ceaaf0b3c4f16c660338877ad8b97a23c2cd	# <---- 원격 저장소 내용
  • 충돌 부분 수정 하고 push 하기
$ git add work.txt


$ git status
On branch main
Your branch and 'origin/main' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" if you want to integrate the remote branch with yours)

All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
        modified:   work.txt


$ git commit
[main 210339e] Merge branch 'main' of https://github.com/~~~/git-study
 Committer: unknown <1111@~~~~~.com>



$ git log --all --graph --oneline
*   210339e (HEAD -> main) Merge branch 'main' of https://github.com/~~~/git-study
|\
| * 2202cea (origin/main, origin/HEAD) work 2a
* | 26642c7 work 2b
|/
* b36d6eb work 1


$ git push
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 515 bytes | 515.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/~~~/git-study.git
   2202cea..210339e  main -> main
  • 원래 컴퓨터에서 원격 저장소 내용 받기
$ git pull
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (6/6), 495 bytes | 14.00 KiB/s, done.
From https://github.com/~~~/git-study
   2202cea..210339e  main       -> origin/main
Updating 2202cea..210339e
Fast-forward
 work.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

git fetch

git fetch : 원격 저장소만 업데이트

  • git pull = git fetch + git merge FETCH_HEAD
    - 가장 최근한 fetch한 내용을 merget해줌
    흐름1: git pull -> git add -> git commit -> git push
    흐름2: git fetch -> git merge FETCH_HEAD -> git add -> git commit -> git push

remote branch 와 local branch

  • main : local branch
  • origin/main : remote branch
$ git log --all --graph --oneline
*   210339e (HEAD -> main, origin/main, origin/HEAD) Merge branch 'main' of https://github.com/~~~/git-study
|\
| * 2202cea work 2a
* | 26642c7 work 2b
|/
* b36d6eb work 1
$ git log --all --graph --oneline
*   210339e (HEAD -> main, origin/main, origin/HEAD) Merge branch 'main' of https://github.com/~~~/git-study
|\
| * 2202cea work 2a
* | 26642c7 work 2b
|/
* b36d6eb work 1


$ vi work.txt

$ git commit -am "work 3a"
[main fc45bff] work 3a
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 1 insertion(+)

$ git log --all --graph --oneline
* fc45bff (HEAD -> main) work 3a
*   210339e (origin/main, origin/HEAD) Merge branch 'main' of https://github.com/~~~/git-study
|\
| * 2202cea work 2a
* | 26642c7 work 2b
|/
* b36d6eb work 1
  • 작업 후, 현재 브랜치는 로컬의 main
    - 로컬의 main 브랜치는 현재 work 3a 커밋(버전)을 가리킴
  • 원격의 main 브랜치는 아직 merge 커밋(버전)을 가리킴
  • 로컬 main 브랜치가 원격의 main 브랜치보다 1개 버전을 앞서가고 있음

로컬 main 브랜치와 원격의 main 브랜치의 차이점 확인 가능

  • git status에서 Your branch is ahead of 'origin/main' by 1 commit
    - 너의 (현재 로컬 main)브랜치가 원격 저장소(origin/main)보다 1커밋 앞서 있다.
  • use "git push" to publish your local commits: git push를 사용해라
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
  • push해서 원격과 지역 저장소의 버전 맞추기
  • push 후엔 main, origin/main가 같은 위치에 존재
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 242 bytes | 242.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/~~~/git-study.git
   210339e..fc45bff  main -> main


$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean


$ git log --all --graph --oneline
* fc45bff (HEAD -> main, origin/main, origin/HEAD) work 3a
*   210339e Merge branch 'main' of https://github.com/~~~/git-study
|\
| * 2202cea work 2a
* | 26642c7 work 2b
|/
* b36d6eb work 1
  • 다른 컴퓨터에서 fetch하기
$ git log --all --graph --oneline
*   210339e (HEAD -> main, origin/main, origin/HEAD) Merge branch 'main' of https://github.com/~~~/git-study
|\
| * 2202cea work 2a
* | 26642c7 work 2b
|/
* b36d6eb work 1


$ git fetch
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 222 bytes | 12.00 KiB/s, done.
From https://github.com/~~~/git-study
   210339e..fc45bff  main       -> origin/main


$ git log --all --graph --oneline
* fc45bff (origin/main, origin/HEAD) work 3a
*   210339e (HEAD -> main) Merge branch 'main' of https://github.com/~~~/git-study
|\
| * 2202cea work 2a
* | 26642c7 work 2b
|/
* b36d6eb work 1


$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working tree clean
  • fetch를 했지만, 지역 저장소의 main은 여전히 merge 커밋을 가리키고 있음
  • 원격 저장소의 main 브랜치(origin/main)가 지역 저장소의 main보다 앞서 있음
  • Your branch is behind 'origin/main' by 1 commit: 너의 (현재 로컬 main)브랜치가 현재 원격 저장소(origin/main)보다 1커밋 뒤에 있다.
  • use "git pull" to update your local branch: git pull을 해서 업데이트해라
  • fetch한 후, 현재 로컬의 HEAD가 가리키는 브랜치와 같은 원격 브랜치를 merge
$ git branch
* main

$ git merge origin/main
Updating 210339e..fc45bff
Fast-forward
 work.txt | 1 +
 1 file changed, 1 insertion(+)


$ git log --all --graph --oneline
* fc45bff (HEAD -> main, origin/main, origin/HEAD) work 3a
*   210339e Merge branch 'main' of https://github.com/~~~/git-study
|\
| * 2202cea work 2a
* | 26642c7 work 2b
|/
* b36d6eb work 1
profile
Record Everything!!

0개의 댓글