Reverting mistakes
Reverting a single mistake...
git log git revert __COMMIT_NUM__ # copy from log; start from the top most commit and work down until all commits are reverted
Reverting local code to github's code...
git fetch origin git reset --hard origin/__BRANCH_NAME__ Examples: git reset --hard origin/master git reset --hard origin/dev git reset --hard origin/sufia-integration
Keeping everything clean and happy
Make sure master is up to date
git checkout master git pull origin master # brings master upto date with what is in github
Rebase local branch to remote branch (e.g. pull in others work)
git checkout my_branch git rebase my_branch # I use pull in Tower with rebase checked
Rebase local branch to master
git checkout my_branch git rebase master # pull master first (see 'Make sure master is up to date' above) # if you get message about being X behind and Y ahead, do the following to sync everything back up # NOTE: used when my_branch has been pushed to github and subsequent changes were made to master git push --force-with-lease origin my_branch
Squash multiple commits to one (reference)
git checkout my_branch git log # count number of commits since branch creation git rebase -i HEAD~3 # replace 3 with the number of commits # in editor, leave first line as pick; change all other lines to s git push origin my_branch --force.
Just want to change the latest commit message?
git commit --amend
Fetch vs. Pull & Merge vs. Rebase
Reference: Fetch, Pull
Reference: Merge vs. Rebase
Term | Definition | Comments |
---|---|---|
fetch | git checkout my_branch grabs all the new remote-tracking branches and tags without merging those changes into your own branches | |
merge | git checkout my_branch combines your local changes with changes made by others |
|
pull | git checkout my_branch SHORTCUT: git fetch and git merge | |
rebase | git checkout my_branch git rebase master |
|
Forking and syncing upstream
One time configuration of the upstream (the repo you forked) in your forked app
Ref: https://help.github.com/articles/configuring-a-remote-for-a-fork/
$ git remote -v origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch) origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push) $ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git $ git remote -v origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch) origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push) upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch) upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
Syncing upstream changes in your forked app
Ref: https://help.github.com/articles/syncing-a-fork/
git fetch upstream git checkout master # or branch for PR if you don't want to merge directly into master git merge upstream/master # same no matter which branch you checked out/created # now push to github the usual way
Create a new local git repository
- Navigate to the project main directory.
- Execute git init command
$ git init .
Working with Branches
Listing branches
List all branches on localhost
$ git branch * master v1.1_bugs v2.0_dev
NOTE: * means that master is the active branch in the local git repository. Any changes will go to the active branch.
Listing all branches on remote
$ git ls-remote --heads <remote-name> $ git ls-remote --heads origin d83fc4d63484c4e8ca62793d3eb14f4c3dce74c7 refs/heads/development ab079b4e6caf22b4a52475df5152f6f6026d3cb2 refs/heads/fix_15_put_sensitive_data_in_env 584917d29b8b556d5e4ca3808eed18f21b689391 refs/heads/master
Listing all tags on remote
$ git tag -l
Creating branches
Adding a branch on localhost
$ git branch __NEW_BRANCH_NAME__ $ git branch v2.0_bugs
Switching branches
Switching to a different branch on localhost
$ git checkout __LOCAL_BRANCH_TO_MAKE_ACTIVE__ $ git checkout v2.0_dev $ git branch master v1.1_bugs * v2.0_dev
NOTE: * means that v2.0_dev is now the active branch in the local git repository. Any new commits will go to the active branch.
OR Add and Switch in one command
$ git branch * master $ git checkout -b _LOCAL_BRANCH_TO_ADD_AND_MAKE_ACTIVE_ $ git checkout -b v2.0_dev $ git branch master * v2.0_dev
Switching to a remote branch
$ git checkout -b _LOCAL_BRANCH_NAME_ --track _REMOTE_REPO_NAME_/_REMOTE_BRANCH_NAME_ $ git checkout -b fix_15_put_sensitive_data_in_env --track origin/fix_15_put_sensitive_data_in_env
Switching to a remote tag
$ git checkout tags/_REMOTE_TAG_NAME_ -b _LOCAL_BRANCH_NAME_ $ git checkout tags/9.7.0 -b 9.7.0_bug_fixes
Switching to a commit
$ git checkout -b _LOCAL_BRANCH_NAME_TO_CREATE_ _COMMIT_ID_FROM_GITHUB_ $ git checkout -b esmis_pinned ea3d5ed039edd6d4a07cc41bd09eb58edd1f2b3a
Pushing to a branch
Pushing changes to branch in github
Reference: git push
$ git push __REMOTE_REPO_NAME__ __LOCAL_BRANCH_TO_PUSH__ $ git push github v2.0_dev
Pulling from a branch
Pulling a branch from github
Same steps as pulling master from github. Except the last step...
$ git pull __REMOTE_REPO_NAME__ __REMOTE_BRANCH_TO_PULL__ $ git pull github v2.0_dev
Merge changes into local branch
Merge changes in master into my branch
Make sure master is up to date.
- Use checkout to get the master from the local git repository
- Use branch to confirm in correct branch
- Use pull to merge master in remote github repository into the local working copy of master
Use push to move all local commits to the remote github repository
$ git checkout master $ get branch * master v1.1_bugs v2.0_dev $ git pull github master
Switch to repository you are merging master changes into and merge
- Use checkout to switch to branch receiving the merge
- Use branch to confirm in correct branch
Use merge to merge master into active repository (i.e., v2.0_dev)
$ git checkout v2.0_dev $ git branch master v1.1_bugs * v2.0_dev $ git merge master
NOTE: If there are no conflicts, it merges changes in master into the active branch which is v2.0_dev. Conflicts get markers representing the conflicts in the offending files, which need to be resolved by hand in an editor.
Making a Pull Request
Create the new branch in which to work...
- Clone master from github OR checkout if already cloned to local git repository.
- Pull to sync github master and local master. (Resolve any conflicts. push and pull if needed)
Create a branch in the local git repository from local master branch.
$ git checkout master $ git branch * master v1.1_bugs v2.0_dev $ git pull github master $ git branch v2.1_dev $ git checkout v2.1_dev $ git branch master v1.1_bugs v2.0_dev * v2.1_dev
Make changes to branch.
- Write tests.
- Code.
- Run tests.
- When all tests pass...
Follow steps for merging master into the branch to make sure master hasn't changed. Resolve conflicts if needed. push and pull if needed.
$ git push github v2.1_dev
In github, create pull request
- Go to main page for project.
- Use pull down for branches to select the new branch (e.g. v2.1_dev)
- Click big green button "Compare and Pull Request"
- On next page, fill in description and click big green button "Make pull request"
- Verify tests complete and pass
Useful Tools integrated with github
Travis - Auto-testing
Reference: Travis and Github - Getting Started
One Time Configuration
- Follow Step 1 & 2 in Getting Started document.
Configure Badge
Follow Step 3 and add a .travis.yml file. Example...
language: ruby bundler_args: --without debug script: "bundle exec rspec spec" rvm: - 1.9.3 - 2.0.0 - 2.1.0 - 2.1.1 - jruby-19mode matrix: allow_failures: - rvm: jruby-19mode
ERROR: Permission denied (publickey).- This should kick off a build. Had to go to my page and turn on Travis for the project that I want it to run.
- https://travis-ci.org/repositories
- click your name in top right corner
- click the name of the Repository on the left where the project lives.
- click the On/Off slider for the project
- Follow Step 4 in Getting Started documentation
- check in .travis.yml to github
- touch any other file and check it into github
Add Badge
Edit README.md and add something like...
# [![Build Status](https://travis-ci.org/_REPOSITORY_/_PROJECT_.png?branch=master)](https://travis-ci.org/_REPOSITORY_/_PROJECT_) [![Build Status](https://travis-ci.org/ld4l/open_annotation_rdf.png?branch=master)](https://travis-ci.org/ld4l/open_annotation_rdf)
Coveralls Badge - Status of Test Coverage
Reference: Coveralls -- Ruby Instructions
One Time Configuration
- Allow READONLY access from github to Coveralls (not sure where at Coveralls website I did this) -- user:email
- Can revote at... https://github.com/settings/applications
Configure Badge
- Setup Travis for the project first
Add gem
spec.add_development_dependency('coveralls')
Edit spec/spec_helper.rb and require coveralls
# MUST BE FIRST CODE AT TOP OF FILE SPEC_HELPER require 'coveralls' Coveralls.wear!
Add .coveralls.yml
service_name: travis-ci
- https://coveralls.io/repos/new
- click button to Sync Repositories !!! NOTHING HAPPENS !!!
- If you don't get a list of repos/projects, then you may need to go to the repo in github and change your visibility to public.
- Go to the main page for the repo (ex. https://github.com/ld4l)
- Click the People link on the right hand side just above the thumbnails of some of the members. (ex. https://github.com/orgs/ld4l/people)
- Find you name in the list and change the marker on the right from private to public.
- Go to the main page for the repo (ex. https://github.com/ld4l)
- If you don't get a list of repos/projects, then you may need to go to the repo in github and change your visibility to public.
Add Badge
Edit README.md and add something like...
# [![Coverage Status](https://coveralls.io/repos/_REPOSITORY_/_PROJECT_/badge.png?branch=setup_coveralls)](https://coveralls.io/r/_REPOSITORY_/_PROJECT_?branch=setup_coveralls) [![Coverage Status](https://coveralls.io/repos/ld4l/open_annotation_rdf/badge.png?branch=setup_coveralls)](https://coveralls.io/r/ld4l/open_annotation_rdf?branch=setup_coveralls)
VersionEye Badge - Status of Dependencies
Reference: Version Eye
One Time Configuration
- Allow READONLY access from github to VersionEye (not sure where at VersionEye website I did this) -- user:email
- Can revote at... https://github.com/settings/applications
Configure Badge
- Add project to VersionEye at: https://www.versioneye.com/user/projects/github_repositories !!! NO GITHUB REPOS OR PROJECTS LISTED !!!
- I think it should have listed all my github repos or projects and I could select this to turn it on for
- It had a way to add a url to a Gemfile, which I did for the open_annotation_rdf Gemfile, but that only checks out ActiveTriples. I didn't see a way to add gemspec files. This seems like the wrong way to go anyway. Seems like the github integration should allow me to simply point to the repo/project.
- ??? Do I have permissions to any repo/projects??? Is this why they don't show up when I sync with github?
Add Badge
Edit README.md and add something like…
# [![Dependency Status](https://www.versioneye.com/ruby/_REPOSITORY_-_PROJECT_/0.0.4/badge.svg)](https://www.versioneye.com/ruby/_REPOSITORY_-_PROJECT_/0.0.4) [![Dependency Status](https://www.versioneye.com/ruby/ld4l-open_annotation_rdf/0.0.4/badge.svg)](https://www.versioneye.com/ruby/ld4l-open_annotation_rdf/0.0.4)
Gem Version - Shows version of latest release
Reference:http://badge.fury.io/
One Time Configuration
- None
Configure Badge
- None
Add Badge
Edit README.md and add something like…
# [![Gem Version](https://badge.fury.io/rb/_REPOSITORY_-_PROJECT_.svg)](http://badge.fury.io/rb/_REPOSITORY_-_PROJECT_) [![Gem Version](https://badge.fury.io/rb/ld4l-open_annotation_rdf.svg)](http://badge.fury.io/rb/ld4l-open_annotation_rdf)