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 computer, in your
hyf-homework
repository, start by creating a branch for your homework, namedgit-week2
, and move to that branch - Inside the folder
hyf-homework/git/week2
create a new file namedveggies.txt
, inside the file write the name of 5 veggies, one per line. Commit your changes. - Push the branch
git-week2
to github. - While on
git-week2
, create a branch namedfeature/veggies
and 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
git-week2
and modify the name of the third veggie in the fileveggies.txt
. Commit your changes. - Before pushing the branch
feature/veggies
to github, update it with the latest changes on branchgit-week2
(hint: pull that branch from github and merge it withfeature/veggies
). - Now that
feature/veggies
has been updated, push it to github and create a pull request togit-week2
. - Wait for feedback, there will be one more task before merging :)
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 commitedgit commit -m "commit_message"
- commit (save) your changesgit branch <branch-name>
- to create a new branch named<branch-name>
git 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.
Note
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-homework
then push to a branch named my-homework
by typing git push origin my-homework
.
When pulling, if you want to pull from a branch named, for instance git-homework
, make sure that you are in a branch with the same name (git-homework
) on your computer as well, and only then do git pull origin <branch-name>
.
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 :)
- Create 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.txt
and commit your changes. Repeat this 5 times, so that at the end you have 5 country names in yourcountries.txt
file 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
master
. - Use
git cherry-pick <commit_hash>
to get the commit where you created the filemore_countries.txt
into your master branch (you need to get the commit hash by doinggit log
when 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.txt
was added (get the commit hash by doinggit log
and 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 master
. - 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 log
on 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
)