Git Remote Branching with Learn Git Branching
Git Remote with Learn Git Branching
In this section I am going to revise my git skill with https://learngitbranching.js.org/
Push & Pull – Git Remotes!
Task 1: Clone Intro
Note
To download the remote repository from github or other alternative.
1
git clone http://url.of.repo
Question
Solution
We just need to do clone
1
git clone
Task 2: Remote Branches
Question:
Solution:
- Make a commit being on
o/main
- Checkout to
o/main
again - Make a commit
1
2
3
git commit
git checkout o/main
git commit
Task 3: Git Fetchin’
Note:
To fetch the changes from remote, we use git fetch
1
git fetch
Question:
Solution:
Simply do fetch.
1
git fetch
Task 4: Git Pullin’
Note:
Git pull is the combine of git fetch and git merge. So, we can simply do both with single command.
1
git pull
Question:
Solution:
Simply pull repo.
1
git pull
Task 5: Faking Teamwork
Note:
This level is just for simulating the updates in remote repo. But here I have solved it in more practical way.
Question:
Solution:
Clone, do couple of commit and push
1 2 3 4
git clone git commit git commit git push
Make a new commit at
c1
mergec1
atc3
1 2 3 4
git checkout c1 git commit git checkout c3 git merge c4
Task 6: Git Pushin’
Note:
Question:
Solution:
Simply make two commit and push the changes to remote
1
2
3
git commit
git commit
git push
Task 7: Diverged Work
Note:
Question:
Solution:
Clone the repo
1
git clone
Make a commit in remote i,e fake teamwork with 1 commit
1
git fakeTeamwork 1
Make a commit in locally
1
git commit
Fetch the remote changes
1
git fetch
Rebase local commit to remote latest commit
1
git rebase o/main
Push the changes
1
git push
Task 8: Remote Rejected!
Note:
The remote rejected the push of commits directly to main because of the policy on main requiring pull requests to instead be used.
You meant to follow the process creating a branch then pushing that branch and doing a pull request, but you forgot and committed directly to main. Now you are stuck and cannot push your changes.
Create another branch called feature and push that to the remote. Also reset your main back to be in sync with the remote otherwise you may have issues next time you do a pull and someone else’s commit conflicts with yours.
Question:
Solution:
Assign the current branch with another name i,e feature.
1
git branch feature
Push the commit to remote feature branch
1
git push origin feature
Checkout to
C1
and name it asmain
branch. So, main branch at local and remote both represent to same state.1 2
git checkout c1 git branch -f main
Then, check out to
main
and checkout tofeature
1 2
git checkout main git checkout feature
To Origin And Beyond – Advanced Git Remotes!
Task :1: Push Main!
Note:
When we are working on a base and someone remotely change that base. In such case we might get problem to pull or update the changes locally. For that we need to fetch and rebase properly.
Question:
Solution:
- Fetch the changes to o/main then rebase each of them properly. Point the main branch properly so remote and local both main can attain same state on push
1
2
3
4
5
6
7
8
git checkout side1
git rebase o/main
git checkout side2
git rebase side1
git checkout side3
git rebase side2
git branch -f main
git branch -f o/main
Then finally push the changes
1
git push
Task 2: Merging with remotes
Note:
We need to solve the previous task with merge than rebase.
Question:
Solution:
Merge the commits in a way that they form the commit history like in goal. Make push being checkout to main.
1
2
3
4
5
6
7
8
9
10
11
git fetch
git checkout side1
git merge o/main
git merge side2
git merge side3
git branch -f main
git checkout c2
git branch -f side1
git checkout side1
git checkout main
git push
Task 3: Remote Tracking
Note:
We will checkout a new branch named foo
and set it to track main
on the remote.
1
git checkout -b foo o/main; git pull
we used the implied merge target of o/main
to update the foo
branch. Note how main doesn’t get updated!!
Another way to set remote tracking on a branch is to simply use the git branch -u
option. Running
git branch -u o/main foo
will set the foo
branch to track o/main
. If foo
is currently checked out you can even leave it off:
git branch -u o/main
Question:
Solution:
Make a branch and set it to track
o/main
and make a commit1 2
git checkout -b side o/main git commit
Simply, pull the changes at origin, then rebase and push
1 2
git pull --rebase git push
Task 4: Git push arguments
Note:
When we have multiple branches we need to push the branches by specifying its name if we are not in that specific branches.
1
git push origin branchname
Question:
Solution:
All the need to do is make push specifying the branch name as checkout is disable,
Push
main
1
git push origin main
Push
foo
1
git push orign foo
Task 5: Git push arguments
Note:
if we wanted the source and destination to be different? What if you wanted to push commits from the foo
branch locally onto the bar
branch on remote?
In order to specify both the source and the destination of <place>
, simply join the two together with a colon:
git push origin <source>:<destination>
Question:
Solution:
- Push
main^
asfoo
andfoo
asmain
1
2
git push origin main^:foo
git push origin foo:main
Task 6: Fetch arguments
Note:
If git fetch
receives no arguments, it just downloads all the commits from the remote onto all the remote branches.
We want to fetch the remote branch or commit and want plop locally in local branch.
1
git fetch origin <source>:<destination>
Question:
Solution:
Fetch
c3
asfoo
1
git fetch origin c3:foo
Fetch
c6
asmain
1
git fetch origin c6:main
Checkout to
foo
and mergemain
1 2
git checkout foo git merge main
Task 7: Source of nothing/ deleting remote branch
Note:
We can delete remote branch by specifying nothing as source to a branch during push.
1
git push origin :foo
But, fetching “nothing” to a place locally actually makes a new branch.
1
git fetch origin :bar
Question:
Solution:
Make a bar branch
1
git fetch origin :bar
Delete foo branch
1
git push origin :foo
Task 8: Pull arguments
Note:
We can use pull to fetch and merge by assigning a branch name to a pulled commit.
1
git pull origin c3:branchname
Question:
Solution:
Being on main pull c3
as foo
branch and c2
as side
branch
1
2
git pull origin c3:foo
git pull origin c2:side