Git Process Chart

Typical GitHub flow

This example shows a user "Joe" forking an existing code repository for an application called Petting Zoo. He then clones (or downloads) it to his local drive, changes some of the code, and pushes this updated code back to the GitHub repo. He then creates a Pull Request to merge his forked code with the master branch.

Steps Explained:

  1. User Joe logs into GitHub and "forks" an existing repository to his own account. Git will create a link (fork) to the Parent repository but any changes made to Joe's fork are for his fork only, the parent repo remains unchanged. Joe's fork is the "origin".

  2. Joe creates a local directory which will be his local repository (master repo)

  3. > mkdir /c/projects/pettingzoo

  4. He then initializes Git and adds a new 'origin' repo

  5. > cd /c/projects/pettingzoo

  6. > git init

  7. > git remote add origin https://github.com/Joe/PettingZoo.git

  8. ## Lets check what remote repos are currently configured

  9. > git remote -v

  10. origin https://github.com/Joe/PettingZoo.git (fetch)

  11. origin https://github.com/Joe/PettingZoo.git (push)

  12. Joe then clones (downloads) his GitHub repo into his local directory

  13. > cd /c/projects/pettingzoo

    1. > git clone https://github.com/Joe/PettingZoo.git

    2. > Cloning into 'PettingZoo'

    3. Cloning creates a .git directory and a default "master" working branch.

    4. > git branch

    5. * master

  14. Joe creates a new working branch called "new_scripts". As a general rule, its a good idea to create a new branch for any kind of edits, separate from "master" branch

  15. > git checkout -b new_scripts

  16. > Switched to a new branch 'new_scripts'

  17. > git branch

  18. master

  19. * new_scripts

  20. Joe starts working on the code. He modifies "description.doc" document, deletes "cow.cpp" file and adds 2 new scripts, a Python script "pony.py" and a Ruby script "rabbit.rb"

  21. Joe adds all his current files to the working branch "new_scripts", 'adding' a file is like staging it for a commit, you are saying that this file was modified and you want to push it back to version control

  22. > git add --all

  23. or if to add a file individually,

  24. > git add pony.py

  25. to see which files are added for commitment,

  26. > git status

  27. he then commits all these files to Git, and leaves a meaningful message as part of the commit

  28. > git commit -m "adding new python scripts to Petting Zoo, deleted c++ cow script"

  29. Joe then goes back to 'master' branch and pushes all his updated code back to GitHub. Switching back to master before pushing other branches ensures a clean push.

  30. > git checkout master

  31. > git push origin new_scripts

    1. Note: Never do "git push origin master" , always do "git push origin branch_name"

    2. The reason is that if you push your local 'master' to your origin (your own fork of a Parent repo), when you create a Pull Request, if that request is approved, your local 'master' files will wipe out the Parent repo's files. If on the other hand you push your branch name, when the pull request is approved, only files in your branch will get merged with the Parent master

    3. the GitHub page should now show the new and modified files and not show the deleted .cpp file

  1. Joe's GitHub page will now be updated with his new and modified code.

  2. Once Joe's GitHub repo has been updated, he then submits a "Pull Request" (on the GitHub site). This sends a request to the original repo (from where Joe forked his repo) to pull Joe's changes into the original repo's main branch. The push/pull sequence enables multiple users to develop code on their own personal branches and then have an administrator pull their changes back into the original branch (if these changes are approved and dont cause issues).

  3. Once the pull request is approved and Joe's changes are merged into the top branch, his fork will become empty (since all his changes are now merged).

  4. Joe then switches back to 'master' branch, and deletes his "new_scripts" branch

  5. > git checkout master

  6. Switched to branch 'master'

  7. > git branch -D new_scripts

  8. Runs git pull origin master to get latest changes from origin to his local master repo

  9. > git pull origin master