This repo provides instructions for a simple git basics exercise.
-
Create a new repository on your github account
-
Clone this new repository by using the
git clone <repo address>
command. You can find the repo address by clicking on the greenCode
button on the main page of the repo.
-
Add any file in the cloned folder. Let's say
hello.py
and add any code to it. -
Ask git to track this file by adding it to git's index:
git add hello.py
-
Commit your changes
git commit hello.py -m "Add simple python code"
-
Make another change to
hello.py
and commit again -
Push your changes using
git push
; Voila! If you go back to your repo on github, you should see the commit history of your changes.
git branch my-new-feature
git checkout my-new-feature
- Create a new file
main.py
and add any code to it - Ask git to track this file (remember you are now tracking it on the new branch, not main):
git add main.py
- Commit your change
git commit main.py -m "Add main on branch"
- Push your change
git push
. Since this is the first time you are pushing to this branch, git will ask you to set upstream. Copy and run the command it provides you to set upstream for this branch (it will look something like thisgit push --set-upstream origin my-new-feature
. - Go to github and change the branch you are viewing from the top left corner and see how you see the commits from main (until the time you did the branch) plus the new commit you added.
- Now let's merge our feature branch changes in to the main branch. Since we want to merge things into main, we will check out main first
git checkout main
- We will ask git to merge our new branch
git merge new-feature
. - Run a
git status
and see how it tells you that your local main branch is ahead of origin/main and that you need to push your changes. This is because the git merge command created the merge commit when it merged the changes (or more precisely since this case is a fast forward, it just added the new commit to the main history). - Push to the remote repo
git push
- Go to github and see how the new change(s) you made are part of the main branch history
Let's repeat the above steps but merge things through a Pull Request rather than through the command line.
git status
to make sure we are on the main branch. If we are not, then change to main by runninggit checkout main
. Also make sure you have no uncommitted changes.git pull
just to get in the habit of making sure that your repo is always up to date when you start working.git branch featureB
git checkout featureB
- Edit
main.py
and make any changes to the code - Commit your change
git commit main.py -m "Add changes to main"
(P.S. this is a pretty bad commit message :-) ) - Push your change
git push
. Since this is the first time you are pushing to this branch, git will ask you to set upstream. Copy and run the command it provides you to set upstream for this branch (it will look something like thisgit push --set-upstream origin featureB
. - Go to github and change the branch you are viewing from the top left corner and see how you see the commits from main (until the time you did the branch) plus the new commit you added.
- Now, let's create a pull request to merge the changes we made on the branch into the main branch. Go to the Pull Requests tab on the github repo web page and select "New Pull Request". Make sure the base branch shows
main
(since the repo is the same repo, you won't see a separate repo name selection) and the compare branch showsfeatureB
. You should now see the new commit you made in the branch and you can click on "Create Pull Request" - On the created pull request, you can see that github tells you that this branch has no conflict with main and can be safely merged. Click on "Merge pull request". Once you are done merging, you can delete the branch through the github interface on the PR
- Now go to the main branch on github and you will see the that
main.py
has your new changes - On your command line, switch to main
git checkout main
and do agit log
and you will also see the expected commit history there
- Assuming we are on main and have the latest updates, create a new branch
git checkout -b featureC
#note that this is a shorthand for creating and checkout out a new branch in one step - Change any line in
main.py
and commit and push your changes (you know the drill by now) - Now, let's pretend that someone else made changes on the main branch in the meantime. To make this situation real, we will do the following:
a.
git checkout main
b. Make changes to the SAME LINE inmain.py
but create a slightly different variation of this line (e.g., if in step 2, you changedx = x + 1
tox = y + 1
, here change the same line tox = x + 2
c. Commit and push your changes - Now let's try to merge our changes from featureC into main using the same steps from exercise 2.2: a. We go on github and create a new Pull Request asking to merge featureC into main b. Now notice how github is refusing to show me the merge button and is asking me to resolve conflicts first? Well, let's resolve the conflict. I will resolve it on the command line as follows c. I want to resolve the conflicts on the BRANCH such that github will allow me to merge the pull request into main. Why? I'm the one who's asking to merge my changes on main so I'm responsible for figuring out the conflicts on my new branch and solving them before continuing the PR. So:
git checkout featureC
git merge featureC
This causes git to list the files that have conflicts. iIn this case, we will see something likeCONFLICT (content): Merge conflict in main.py
- So we will open main and find the conflict markers and decide how we want to resolve this conflict.
- Once I'm done resolving the conflict, I will ask git to add the file back to its staging/index:
git add main.py
. If I do agit status
now, git will tell me that i've resolved all conflicts but I'm still merging and need to commit to conclude the merge. So let's commit as followsgit commit -am "Merged changes from main"
- Push our changes using
git push
(note this is pushing to our branch) - Now go back to your PR on github and refresh. You should see that github now allows you to merge your changes into the main branch! Merge your changes into main.
Note: if you have multiple conflicting files, you will have to resolve all conflicts (steps 7-8).
We will mimic the situation in Exercise 2.3 but merge with a rebase. I personally like rebasing better.
- Repeat steps 1-4 above (With all substeps) but call the branch
featureD
. You should now be at the point where github is blocking you from merging the PR because there is a conflict. - We will again try to merge main into featureD. First checkout the feature branch
git checkout featureD
- Instead of using git merge, use git rebase
git rebase main
. Git again complains about the conflict and asks you to resolve it. - Go to the file it's complaining about and resolve the conflict as usual (in the end, you should have no conflict markers)
git add main.py
this is the file(s) with the conflicts that you resolvedgit rebase --continue
- save the commit message
git push
Now notice how git complains about divergent history? This is because the rebase option rewrites history and git doesn't know what to do. Accordingly, we will force push to the branchgit push -f
- If we now go back to the PR, we will see again that github is allowing us to merge
WARNING: USING FORCE PUSH IS DANGEROUS. Unless you really know what you are doing, USE IT ONLY IN THE ABOVE SITUATION ON A FEATURE BRANCH WHEN YOU REBASED FOR A PR.
Note: if you have multiple conflicting files, you will repeat steps 4-6 until done.