Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Git and GitHub for PHP Projects (PHPNW15)

Git and GitHub for PHP Projects (PHPNW15)

Thanks largely to Composer and its tight integration with GitHub, it has never been easier for people to share and contribute to the PHP ecosystem. These days, it more or less required that contributors have a working knowledge of Git and GitHub to be effective. Thankfully, learning how to use Git and GitHub doesn't have to be scary. Whether you're new to Git, still using Subversion, or don't have any experience with version control systems, this workshop will get you started using Git and GitHub so that you can start contributing back to your favorite PHP projects!

Beau Simensen

October 02, 2015
Tweet

More Decks by Beau Simensen

Other Decks in Programming

Transcript

  1. FIRE DRILL 15:00 Remember! If it lasts longer than 60

    seconds, it isn't a drill. :) Break at 15:30
  2. me.

  3. They didn't want to break new ground in version control

    methodology, they just wanted to fix CVS. — Subversion's History
  4. Git

  5. The first few month transitioning between subversion and git were

    easily some of the most frustrating months I've spent as a developer.
  6. 33.3% of professional software developers report Git as their primary

    source control system — Eclipse Foundation Community Report, May 2014
  7. 42.9% if you include those that reported GitHub as their

    primary source control system — Eclipse Foundation Community Report, May 2014
  8. Git

  9. echo "Hello World" > file.txt git add file.txt # file.txt

    staged with "Hello World" echo "Goodbye World" > file.txt # file.txt staged: Hello World # file.txt modified: Goodbye World git commit # commits staged changes; "Hello World" git add file.txt # file.txt staged with "Goodbye World" git commit # commits staged changes; "Goodbye World"
  10. git init # You never have to share this with

    # anyone but you can totally use # this Git repository for # amazingness.
  11. git clone /path/to/repo my-clone # Now my-clone is a clone

    of some random # local Git repository at /path/to/repo.
  12. Local Git: The Point Git does not have to be

    a network-only sort of thing. It can be used quite effectively in an isolated or local-only environment.
  13. Remote repositories are versions of your project that are hosted

    on the Internet or network somewhere. — 2.5 Git Basics - Working with Remotes
  14. Remote repositories are Git repositories external to any given Git

    repository. If you run git clone, the repository you clone is considered the origin remote, even if that repository was a local repository. — Beau Simensen, now.
  15. fork [fawrk] noun 1. Fancy marketing term for a remotely

    hosted clone of another remotely hosted repository with some invisible metadata that magically binds them together.
  16. Start with a simple set of rules and expand as

    needed and as comfort level allows.
  17. [email protected]:monii/monii-nimble-framework.git cd monii-nimble-framework git remote rename origin monii # Now

    you have cloned the original repository # and have renamed origin to monii, the # name of the repository owner.
  18. Fork the repository and add your fork as a remote

    with your name for the remote name
  19. git remote add simensen [email protected]:simensen/monii-nimble-framework.git # Now my fork is

    available as a remote in my # local clone! I can now reference either # the actual project using "monii" or # my fork using "simensen."
  20. # Fetch all remotes git fetch --all git remote update

    # I use `git remote update` because of habit # and I already use the remote command # quite a bit managing my remotes # so it flows more nicely. # I never have a reason to run `git fetch --all`.
  21. # Fetch changes from monii and then merge # changes

    from monii/master. git fetch monii && git merge monii/master git pull monii/master git remote update && git rebase monii/master # Some may argue that rebase is bad here and # under certain circumstances it may not be # ideal. However, I find it to be well # worth the risk/potential problems.
  22. # Push the changes to the local # `my-local-feature` branch

    to # the `my-feature` branch of # the `monii` remote. git push monii my-local-feature:my-feature # [local-branch-name]:[remote-branch-name] # If currently checked out branch # is `my-feature`, and you wish # to push to `my-feature` on # the remote, the branch can # be specified once. git push monii my-local-feature
  23. git push -u monii feature git push # Sets an

    upstream; some git commands, like # pull and push, will now default to this # remote if a remote is not specified. # Nice but can lead to you making assumptions. # It may be nice to just be explicit when # making every command in the beginning.
  24. git push monii :my-feature # Deletes the `my-feature` branch from

    the `monii` # remote by pushing *nothing* to the branch.
  25. git push monii my-feature # REJECTED git remote update git

    rebase monii/my-feature git push monii my-feature # The rebase will change code that has already been # published to my fork. Git will reject this as # it cannot be automatically resolved easily. # Force pushing results in completely # replacing the branch that was # already published.
  26. git push simensen my-feature # REJECTED git push -f simensen

    my-feature # If you're the only one working on a branch or your # branch is otherwise not shared you can just # force push. Think hard, though. Always # ask yourself, "is it possible that # someone is already basing code # off of this branch?" # Most often happens in teams where everyone is # working off of the same shared repository # and not contributing via personal forks.
  27. A Pull Request represents a branch on your fork that

    has commits unique to your branch when compared to another GitHub hosted repository.
  28. Continuing to work on a Pull Request (by adding, removing,

    or updating commits) is as easy as pushing those changes to your fork again.
  29. git clone [email protected]:monii/monii-nimble-framework.git cd monii-nimble-framework git remote rename origin monii

    # Clone the actual project's repository first and # then rename the `origin` remote to the name # of the GitHub user that owns the project.
  30. git remote add simensen [email protected]:simensen/monii-nimble-framework.git # Create a fork of

    the project under your GitHub account. # Add a remote with the name of your GitHub user and # give it the URL of your forked repository. # You now have two remotes, one with the name of the # project and one with your own name. Using these # names explicitly will help you always know # where you are fetching changes from and # where you are pushing changes to.
  31. git remote update git checkout -b my-feature monii/master # I

    almost always try to run `git remote update` # when creating a new branch so my branch # starts out with recentish code. # Specifying the remote/branch also ensures I # am basing my new branch off of actual new # code in case my local `master` has not # been rebased in awhile.
  32. git remote update git rebase monii/master # Rebase often, especially

    if your branch is going # to be around for awhile. This ensures you will # find conflicts earlier than you might # otherwise find them.
  33. git push -u simensen my-feature # After some commits are

    made, push the branch # to your own fork by specifying your GitHub # username as the remote you are going # to be pushing to.
  34. git push simensen # After any additional commits are made,

    push # the branch to your own fork by specifying # your GitHub username as the remote you # are going to be pushing to. # If rejected, rebase and force push! # # git remote update # git rebase simensen/my-feature # git push simensen -f #
  35. Your fork will not stay updated automatically So you can

    expect your master to stay hopelessly out of date.