Some Basic Commands on Getting Updates from a Remote Repo

  • Getting changes from a remote repository
    • git fetch : retrieve new work without merging those changes into your own branches, ex: git fetch remotename
    • git merge: combines your local changes with updates in the repo, ex: git merge remotename/branchname
    • git pull: a shortcut for completing both git fetch and git merge

What you'll need


Clone an empty repository from github, create a main branch, and then push it to remote

Clone an empty repository from github

!git clone https://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@github.com/intodeeplearning/test.git
Cloning into 'test'...
warning: You appear to have cloned an empty repository.

Set identity

%%shell
cd test
git status
git config user.email "intodeeplearning@gmail.com"
git config user.name "intodeeplearning"
#git push -u origin test

#git fetch origin
#git checkout test
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Create a main branch and push it to remote

%%shell
cd test
echo '#test' >> README.md
git add README.md
git commit -m "initial commit"
git branch -M main
git push -u origin main
[master (root-commit) 1643b7b] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
Counting objects: 3, done.
Writing objects: 100% (3/3), 217 bytes | 217.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/intodeeplearning/test.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.


Create a new branch from the main branch, make changes, and then merge

Clone a branch from github repo

!git clone https://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@github.com/intodeeplearning/test.git
Cloning into 'test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
%%shell
cd test
git status
#create a new branch
git checkout -b test2
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
Switched to a new branch 'test2'

Create a new branch from main branch, and make a commit

%%shell
cd test

git config user.email "intodeeplearning@gmail.com"
git config user.name "intodeeplearning"

echo '#test2' >> README2.md
git add . README2.md
git commit -m 'add README2.md'
[test2 bf43a76] add README2.md
 1 file changed, 1 insertion(+)
 create mode 100644 README2.md

Push this newly created branch to remote

%%sh
cd test
git push -u origin test2
Branch 'test2' set up to track remote branch 'test2' from 'origin'.
remote: 
remote: Create a pull request for 'test2' on GitHub by visiting:        
remote:      https://github.com/intodeeplearning/test/pull/new/test2        
remote: 
To https://github.com/intodeeplearning/test.git
 * [new branch]      test2 -> test2

You can either merge the main branch with test3 branch on github, or locally by:

%%sh
cd test
git checkout main
git merge test2
Your branch is up to date with 'origin/main'.
Updating 1643b7b..bf43a76
Fast-forward
 README2.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 README2.md
Switched to branch 'main'

Delete a branch

%%sh
cd test
git branch -d test2
Deleted branch test2 (was bf43a76).


Clone a branch from a github repo

!git clone --branch test2 https://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@github.com/intodeeplearning/test.git
Cloning into 'test'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.


Fetch a remote branch

List all the remote tracking branches

%%shell
cd test
git branch -r
  origin/HEAD -> origin/main
  origin/main
  origin/test2

Fetch one remote branch

%%shell
cd test
git fetch origin main

#you can also fetch the all the branches by: git fetch origin
git checkout main
From https://github.com/intodeeplearning/test
 * branch            main       -> FETCH_HEAD
Branch 'main' set up to track remote branch 'main' from 'origin'.
Switched to a new branch 'main'

%%shell
cd test
git branch
* main
  test2


Push an unrelated branch to github repo

%%shell
mkdir test3 
cd test3
echo '#test3' >> README3.md
git init
git config user.email "intodeeplearning@gmail.com"
git config user.name "intodeeplearning"

git add . README3.md
git commit -m 'add README3.md'
git branch -M test3
git remote add origin https://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@github.com/intodeeplearning/test.git
git push -u origin test3
Initialized empty Git repository in /content/test3/.git/
[master (root-commit) 0ebfb4e] add README3.md
 1 file changed, 1 insertion(+)
 create mode 100644 README3.md
Counting objects: 3, done.
Writing objects: 100% (3/3), 226 bytes | 226.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'test3' on GitHub by visiting:
remote:      https://github.com/intodeeplearning/test/pull/new/test3
remote: 
To https://github.com/intodeeplearning/test.git
 * [new branch]      test3 -> test3
Branch 'test3' set up to track remote branch 'test3' from 'origin'.


Merge Two Unrelated Branches

ref

%%shell
cd test3
git status
git branch

git branch -r
On branch test3
Your branch is up to date with 'origin/test3'.

nothing to commit, working tree clean
* test3
  origin/test3

%%shell
cd test3
git fetch origin
git checkout main
git checkout -b test3-1
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
From https://github.com/intodeeplearning/test
 * [new branch]      main       -> origin/main
 * [new branch]      test2      -> origin/test2
Branch 'main' set up to track remote branch 'main' from 'origin'.
Switched to a new branch 'main'
Switched to a new branch 'test3-1'

%%shell
cd test3
git log
commit 1643b7be3c79e6002df550f0a457816c52c84c38 (HEAD -> test3-1, origin/main, main)
Author: intodeeplearning <intodeeplearning@gmail.com>
Date:   Fri Aug 12 15:42:04 2022 +0000

    initial commit

git cherry-pick test3 copies the commit from test3 to test3-1

%%shell
cd test3
git cherry-pick test3
git status
[test3-1 0c4713e] add README3.md
 Date: Fri Aug 12 15:42:08 2022 +0000
 1 file changed, 1 insertion(+)
 create mode 100644 README3.md
On branch test3-1
nothing to commit, working tree clean

%%shell
cd test3
git checkout main
git merge test3-1
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Updating 1643b7b..0c4713e
Fast-forward
 README3.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 README3.md

%%shell
cd test3
git push
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/intodeeplearning/test.git
   1643b7b..0c4713e  main -> main

%%shell
cd test3
git branch
git branch -d test3 
git branch -d test3-1
* main
  test3
  test3-1
warning: deleting branch 'test3' that has been merged to
         'refs/remotes/origin/test3', but not yet merged to HEAD.
Deleted branch test3 (was 0ebfb4e).
Deleted branch test3-1 (was 0c4713e).