Assignment
Part 1
In this exercise you will get some practice creating branches, merging, pushing/pulling to Github, and creating a pull request. Here are the instructions:
In your assignment repo, start by creating a branch for your assignment, named
collaboration-via-github-week1/yourname, and move to that branchInside the
collaboration-via-githubfolder, create a new file namedveggies.txt, inside the file write the name of 5 veggies, one per line. Commit your changes.Push the branch
collaboration-via-github-week1/yournameto github.While on
collaboration-via-github-week1/yourname, create a branch namedfeature/veggiesand move to that branch.On the file
veggies.txt, modify the name of the third veggie, and add two more veggies (one per line) at the end. Commit your changes.On Github, go to branch
collaboration-via-github-week1/yournameand modify the name of the third veggie in the fileveggies.txt. Commit your changes.Before pushing the branch
feature/veggiesto github, update it with the latest changes on branchcollaboration-via-github-week1/yourname(hint: pull that branch from github and merge it withfeature/veggies).Now that
feature/veggieshas been updated, push it to github and create a pull request tocollaboration-via-github-week1/yourname.Wait for feedback, there will be one more task before merging.
[!TIP] Always pull and merge the latest changes from main (and your branch, if others are contributing to it) before starting work on it again. This helps avoid merge conflicts.
Commands that you will need for this part:
git add <file_name>- tell git to start tracking a file and to update what will be committedgit commit -m "commit_message"- commit (save) your changesgit checkout -b <branch-name>- to create a new branch named<branch-name>and move to itgit checkout <branch-name>- to move to a branch named<branch-name>git merge <branch-name>- to merge the branch named<branch-name>with the branch where you are.git push origin <branch-name>- push (upload) your changes in your current branch to your github repository into the branch named<branch-name>.git pull origin <branch-name>- pull (download) your changes from your github repository in the branch named<branch-name>, into your current local branch.
[!TIP] For the sake of consistency (and to avoid mistakes), make sure that when you push you do it to a branch with the same name as the branch where you are, e.g. if you are on a branch named
my-assignmentthen push to a branch namedmy-assignmentby typinggit push origin my-assignment.
When pulling, if you want to pull from a branch named, for instance git-assignment, make sure that you are in a branch with the same name (git-assignment) on your computer as well, and only then do git pull origin <branch-name>.
Part 2
This part is only for you to do by yourself in your computer so you can see how the commands work and get comfortable with them :)
It is important that after each step you run git log --oneline and git status to see what has changed. Also check the contents of your folder :)
git reset and git revert
git reset and git revertCreate a new folder somewhere in your computer, named
reset_playground;Start a git repository inside
reset_playground(make sure you are not inside a git repo before you create it!!!)Create a file named
countries.txt, commit your changes.Add a country name to
countries.txtand commit your changes. Repeat this 5 times, so that at the end you have 5 country names in yourcountries.txtfile and a commit for each.Create a new branch named
feature/more_countries, move to it.Create a new file, named
more_countries.txt, add the name of 2 countries to that file and commit your changes.Go back to
main.Use
git cherry-pick <commit_hash>to get the commit where you created the filemore_countries.txtinto your main branch (you need to get the commit hash by doinggit logwhen you are in the branchfeature/more_countries).In the file
countries.txt, add one more country, and commit your changes.Again, in the file
countries.txt, add one more country, and commit your changes.Remove the latest commit you did using
git reset --soft <commit_hash>Either commit or stash the changes from the last step.
Use
git revert <commit_hash>to revert the commit that you cherry picked from the branchfeature/more_countries, the one where the filemore_countries.txtwas added (get the commit hash by doinggit logand looking for the commit you want).Use
git reset --mixed <commit_hash>to get rid of the last 2 commits.Either commit or stash the changes from the last step.
Use
git checkout <commit_hash>to check what your repo looked like after your first commit.Now go back to your latest commit using
git checkout main.Finally, use
git revert <commit_hash>to revert the commit where you added country number 3. Most likely you will get a conflict and you must solve it.
Commands that you'll need for this part:
git log- see your commit history with all the detailsgit log --oneline- see your commit history in a single line.git cherry-pick <commit_hash>- will copy the commit with hash<commit_hash> from another branch to your current branch. To find the hash for the commit you want usegit logon the branch where the commit exists.git checkout <commit_hash>- you use this to go back to a specific commit and take a look at your code back then, but do NOT ever change your code at this point, since you are in a detached HEAD state when you go back to a specific commit. Remember, you wouldn't walk around if your head wasn't glued to your neck :). To go back to your latest commit you just dogit checkout <branch-name>, where<branch-name>is the name of the branch where you are.git revert <commit_hash>- will revert whatever was done in a specific commit with hash<commit_hash>. Be careful with possible conflicts. If you get conflicts and you want to abort the revert, you can do it withgit revert --abort.git reset --soft <commit_hash>- will remove all commits until the commit with hash<commit_hash>, but the file changes will stay in the staging/index area.git reset --mixed <commit_hash>- will remove all commits until the commit with hash<commit_hash>, but the file changes will stay in your filesystem.
Other useful git commands:
git status- remember, it is your best friend, it tells you what is the state of your repository and sometimes what you should do.git branch- this is your second best friend, it tells you in which branch you are (you can also see where you are when you dogit status)
Last updated