Working with git branches and Github repositories
- Some Basic Commands on Getting Updates from a Remote Repo
- What you'll need
- Clone an empty repository from github, create a main branch, and then push it to remote
- Create a new branch from the main branch, make changes, and then merge
- Clone a branch from a github repo
- Fetch a remote branch
- Push an unrelated branch to github 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 bothgit fetch
andgit merge
-
- For more details: check git flow
What you'll need
- a Person Access Token)
- a link to your empty github repository
!git clone https://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@github.com/intodeeplearning/test.git
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
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
!git clone https://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@github.com/intodeeplearning/test.git
%%shell
cd test
git status
#create a new branch
git checkout -b 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'
Push this newly created branch to remote
%%sh
cd test
git push -u origin 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
Delete a branch
%%sh
cd test
git branch -d test2
!git clone --branch test2 https://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@github.com/intodeeplearning/test.git
List all the remote tracking branches
%%shell
cd test
git branch -r
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
%%shell
cd test
git branch
%%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
%%shell
cd test3
git status
git branch
git branch -r
%%shell
cd test3
git fetch origin
git checkout main
git checkout -b test3-1
%%shell
cd test3
git log
git cherry-pick test3
copies the commit from test3 to test3-1
%%shell
cd test3
git cherry-pick test3
git status
%%shell
cd test3
git checkout main
git merge test3-1
%%shell
cd test3
git push
%%shell
cd test3
git branch
git branch -d test3
git branch -d test3-1