Skip to content

Latest commit

 

History

History
868 lines (354 loc) · 16.3 KB

6. Git push, fetch and pull.md

File metadata and controls

868 lines (354 loc) · 16.3 KB

Overview of the push fetch and pull Git command

image-20230801123716699

  • Does remote repository update itself automatically after local repository change? No you need to push changes.
  • What happens when remote repository updates? Does local repository update itself automatically? No, you need to pull changes.

About fetch

image-20240610132526428

  • git fetch command doesn’t update your local working directory.

cf> pull

image-20240610132932893


What is origin

image-20240610133217739

  • default name of remote repository is origin.

image-20240610133602344

  • git remote: you are able to list all remote servers for your local git repositories.

By default after cloning, Git will not create corresponding local branches for all remote branches except default remote branch

  • Question 🙋‍♂️ : How you can get a list of all remote branches(including local)?? coming soon...
    • List all branches available locally and on remote server: git branch -a

List remote and local branches

image-20240610211804490

# all local and remote
git branch -a 
# * main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/feature-1
#   remotes/origin/main

# only remote branch
git branch -r 

# only local branch
git branch  

What is tracking branch

image-20230801125647755

  • Tracking branch is your local branch that is connected to specific remote branch.
    • master, release, BR-2
  • By default, when you clone any remote repository, git create only one tracking branch with the same name as default branch in the remote repository.
git branch -vv 
# local branch에 대한 추가정보를 알 수 있다.
# 예를 들면, 현재 브랜치와 연결된 원격 추적 브랜치(tracking branch)

image-20240610215315132

remote repository

image-20240610215510046


Checkout remote branch

image-20240610215627806

git branch -a

image-20240610215855173

Let’s create another local tracking branch for remote feature(feature-1)

git checkout feature-1

image-20240611012911384

git branch

image-20240611013040998

현 상태

image-20240611013124285

git branch -d feature-1
# error: Cannot delete branch 'feature-1' checked out at '/Users/isntsoo/Desktop/github-repo-for-fun'
git checkout main

git branch
#   feature-1
# * main

git branch -d feature-1

image-20240611013302338

  • local에서 삭제되었다. 그래도 remote에는 남아있다. git remote -a로 확인

image-20230801131817460

  • 다음 단계 진행을 위해 local에 다시 remote의 feature-1을 트랙킹하는 feature-1 branch를 만들자 git checkout feature-1, git branch -vv

image-20240611013446222

image-20230801132043048


Git remote show origin

git branch -vv

image-20240611013711253

  • 다음 command는 local, remote 에 관한 Entire information을 얻을 수 있다.
# git remote show <nameofServer>
git remote show origin

image-20240611013904015


Git fetch in action

일단 web 상에서 새로운 branch named temp를 생성한 후,

image-20240611015834647

In local, git branch -r - (로컬저장소에 있는 원격 브랜치들을 보여주므로, 원격저장소와 아직 동기화 되지 않았으므로 temp branch가 나타나지 않음)

image-20240611014441007

git fetch - (temp branch를 remote에서 local로 가져왔다. 원격 저장소의 최신 정보를 로컬 저장소로 가져옴.) (fetch command: You will be able to fetch remote changes and download them into your local git repository.)

image-20240611014600564

다시 한 번 git branch -r

image-20240611014708749

# local에 temp branch에 대한 Tracking branch를 만들자

git checkout temp

image-20240611014940699

git brangh -vv

image-20240611014922099git remote show origin

image-20240611015052468

# Web(원격)에서 remote server의 temp branch를 제거한 다음,
git checkout main

git branch -d temp

image-20240611015141080

git remote prune origin

image-20240611015217008

git branch -vv

image-20240611015237287

git branch -vv

image-20240611015332683

git branch -d temp

image-20240611015439901

git branch -vv

image-20240611015509577

git remote show origin

image-20240611015603995


Git pull is 2-step precess

  • Git Pull consists of Git Fetch and Git Merge. It is two-steps process.
  • Git pull updates only single local currently checked out branch

image-20230801143625386


How to perform git pull

image-20230801143802582

현 상황

image-20230801144229116

  • Your local branch is receiving branch to pull.

What is FETCH_HEAD

git fetch -v
git pull -v

git checkout main

git pull -v

cd .git
cat FETCH_HEAD

git branch -a

image-20240611123451568

git branch -vv

image-20240611123517585

git remote show origin

image-20240611123427381

git fetch -v, and git pull -v

image-20240611123929247

  • 위 그림으로 git pull에서는 git fetch가 선행됨을 알 수 있다.

image-20240612132452521

cat FETCH_HEAD

image-20240612132407751

In remote server

image-20240612132934098

  • After fetching it will update .git/FETCH_HEAD list and first branch in this list will be currently checked out branch
  • Finally Git executes "git merge FETCH_HEAD" command that finds first branch in .git/FETC H_HEAD list without "not-for-merge" tag and merge it into local tracking currently checked out branch

image-20240612133236327

image-20230801152808342


Git pull with fast forward merge

remote repository에서 새로운 commit을 한다 in feature-1 branch

image-20240612134124239

In feature-1 branch(receiving branch)

image-20240612133951421

check staging area

image-20240612134407468

merge 전

image-20240612134555455

merge 후

image-20240612134625113


Fetch remote changes manually

Modified in local, feature-1 branch, and commit

image-20240612140749739

image-20240612141059732

In remote repository, feature-1 branch, create another file. and commit

image-20240612141124601

image-20240612141147046

git fetch -v

image-20240612141235901

git ls-files -s

image-20240612141315751

image-20240612141458101

image-20240612141734901

image-20240612141853178


Merge FETCH_HEAD manually

image-20240612142058256

In remote server

image-20240612142316282

But In local,

image-20240612142417197

  • 아직 merge를 하지 않았기 때문에 log에 d0732가 log에 표시되지 않음.

git merge FETCH_HEAD... 3 ways merge 이루어짐.

image-20240612142659878

ls -la feature

image-20240612142733141

git ls-files -s

image-20240612142755356

git pull: 당해 커맨드 입력 없이, fetch와 merge로 pull은 이미 완료가 됐다

image-20240612142905892

git log, new merge commit을 확인해볼 수 있다.) (hash of merge 확인해보기)

image-20240612142844477

new merge commit 확인

image-20240612143308687

현 상황: 다음 그림과 같다

image-20240612143245351


Resolving conflicts during Git pull

현 상황

image-20240612143245351

In remote, edit README.md. and commit(0e5c8).

image-20240612150631894

image-20240612151137562

In local, edit README.md. and commit(d2cc3f0)(To make conflict)

image-20240612150722397

image-20240612151104058

git pull -v : CONFLICT 발생(In feature-1 branch)

image-20240612151242726

In vscode

image-20240612165128769

In staging area

image-20240612165238598

Conflict 해결하자 on README.md file

image-20240612165440153

commit

image-20240612165600685

git log

image-20240612165734177

아직 push를 하지 않았으므로, remote repository는 다음과 같다.

image-20240612165906379


Pushing to remote repository

git push 전에 pull을 먼저 해주자.

image-20240612184335278

  • 현재 상황: There are changes in the local repository that are absent in the remote repository.

image-20240612184657610

  • In order to perform git push operation, you need to have write access to remote repository.

git push -v

image-20240612185127407

git log

image-20240612185702748

git pull에 관한 유익한 글

https://ryanking13.github.io/2021/10/17/why-git-pull-is-broken.html


Commit under another author

In local, modify README.md. and commit

image-20240612222724928

git log

image-20240612222925636


Remote and local branches are in sync

주기적으로 git fetch를 해서 remote repo와 싱크를 맞춰주자

GUI를 쓰면 간단하다

git pull -v

git push -v

하였을 때 up to date 이면 된다.

image-20240612223627831


Create remote branch based on local

image-20240612223911761

git checkout -b feature-2

image-20240612224138653

git branch -vv

image-20240612224219189

In local, feature-2 branch, modify another-file.txt. and commit.

image-20240612224427990

git log

image-20240612224505277

git push -v : error 발생

image-20240612224629761

image-20240612224830817

git push --set-upstream origin feature-2

image-20240612224746596

git branch -vv

image-20240612224812118

In vs code, another modification. and commit.

image-20240612224925462

git push -v

image-20240612225028990

image-20240612225008659

git log

image-20240612225104072

image-20240612225210564


Update tracking statuses of the branches

image-20240612225607227

In Remote, create new branch temp

image-20240612225937324

git fetch

image-20240612230040730

git branch -a

image-20240612230100449

git checkout temp

image-20240612230201107

git branch -a

image-20240612230242528

git branch -vv

image-20240612230310392

In remote, delete temp branch

image-20240612230351318

git fetch

image-20240612230452176

git branch -vv:

image-20240612230522637

  • remote temp branch가 삭제됐음에도 여기에는 나타난다. 즉 local temp branch는 여전히 remote temp branch를 tracking하고 있다.

git remote update origin --prune

image-20240612230746042

git branch -vv

image-20240612230909609

git checkout main

git branch -D temp

image-20240612231032470

git branch -vv

image-20240612231102499


Remove remote branch using local

image-20240612231931302

git checkout -b temp, git branch -a

image-20240612232051771

image-20240612232014985

git push -u origin temp

image-20240612232138376

git branch -vv

image-20240612232200127

git push origin -d temp

image-20240612232407561

local 환경에서 remote repo의 temp branch를 지워보자

git branch -a

image-20240612232337978

git checkout main, git branch -D temp

image-20240612232524662


Git show-ref

git show-ref : 특정 branch의 local과 remote의 reference를 빠르게 비교하고 싶다면.

git show-ref

image-20240612232640053

다른 방법(수고스러움)

image-20240612233146173

branch 별로도 확인할 수 있음. hash도 비교 가능.

image-20240612233259526

git checkout featrue-2. and modify README.md file.

image-20240612233424582

  • 둘의 hash가 달라졌다.

git log

image-20240612233511950

git push -v

image-20240612233601471

git show-ref feature-2

image-20240612233629063

  • 다시 같아졌다.