Pulling commits from remote branch

You can use “git pull” to pull commits from an online repository.

There are different scenarios for git pull.

  • When the online repository is a few commits ahead of your local branch
  • When your local branch has diverged from the remote branch, and there are local changes

Let us see these scenarios hands-on.

git pull to update the local branch from remote repository

Consider this case where the online branch origin/main has more commits than the local branch main.

~/repoone$ git branch
* main

~/repoone$ 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

~/repoone$ git log --oneline
42296d0 (HEAD -> main) Second commit in main
453424e This is first commit
2673111 Initial commit

Always, do a “git fetch” to get the latest information of the online repository.

~/repoone$ git fetch origin

Use “git pull” to get the missing commits onto the local branch. You can see one extra commit in the local branch.

~/repoone$ git pull
Warning: Permanently added the ECDSA host key for IP address '140.82.114.3' to the list of known hosts.
Updating 42296d0..908d6ac
Fast-forward
 filefive | 0
 filesix  | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 filefive
 create mode 100644 filesix

~/repoone$ git log --oneline
908d6ac (HEAD -> main, origin/main, origin/HEAD) Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit

git pull to update the local branch with changes

Consider this scenario where you have one extra commit in the remote repository compared to the local branch. And there are some changes in your local branch, that are not present in remote.

~/repoone$ git log --oneline
2f7cdab (HEAD -> main) Extra commit in local
908d6ac (origin/main, origin/HEAD) Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit

~/repoone$ 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

Always, do a “git fetch” to get the latest information of the online repository.

~/repoone$ git fetch origin

“git pull” in this case would open a window for merge. Once you accept the merge and save, it will complete a merge request.

~/repoone$ git pull
Warning: Permanently added the ECDSA host key for IP address '140.82.114.4' to the list of known hosts.
CONFLICT (add/add): Merge conflict in filesix
Auto-merging filesix
Merge made by the 'recursive' strategy.
 filefive | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 filefive

~/repoone$ git log --oneline
30f871d (HEAD -> main) Merge branch 'main' of github.com:codeversionmaster/repoone into main
58fd87b Local commit in main
908d6ac (origin/main, origin/HEAD) Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit