Skip to content

Instantly share code, notes, and snippets.

@davemaurer
Last active August 29, 2015 14:26
Show Gist options
  • Save davemaurer/6b8a02e769b3fe5ab5ff to your computer and use it in GitHub Desktop.
Save davemaurer/6b8a02e769b3fe5ab5ff to your computer and use it in GitHub Desktop.
Git cheat sheet for beginners
#Commands:
* git branch
- Displays branches you have in the current directory. The branch you are on will be highlighted/emphasized
* git checkout *mybranch*
- Switches you to whatever branch you put in the place of *mybranch* (*mybranch* is just a placeholder name)
* git checkout -b *newbranch*
- Creates a new branch named whatever you put in place of *newbranch*, which is just a placeholder name
* git branch -D *branchidontwantanymore*
- Deletes the branch you put in place of *branchidontwantanymore*
* git merge *branchiwanttomergefrom*
- To use git merge, switch to the branch you want to merge stuff TO, then use git merge and name the branch you want
to pull new fun additions FROM.
- example: I just pulled down from the master on Github and want to merge my LOCAL master with my branch,
awesome_branch. I would first checkout to awesome_branch, then from the command line, I would type git merge master,
which would add all of the changes I pulled down to my local master to my local awesome_branch branch.
* git pull origin master
- Merges the Github master repository with your local master, as long you as you actually are on master.
- "origin" refers to the original repo you cloned down from(if you forked and then cloned, it refers you YOUR repo),
and "master" refers to the branch of that repo. If you created the project from scratch and used git init, origin
is your original repo.
- You can just use git pull if you are always branching off of a normal repo during your project, but using origin
master won't hurt.
* git push
- This alone may not do what you want it to do...I use:
* git push origin *branchiampushingfrom*
- *branchiampushingfrom* is usually NOT master, as it's best practice to push up to a branch and then merge to master
after comparing changes on Github. If you are working on a solo project, do as you like. When in a group, YOLO is
always a nono.
#Good work flow:
##Setup:
1. Person A forks and clones or creates the project.
2. Everyone else clones down from person A's repo.
##Doing work:
1. Everyone, including person A, checks out to a new branch named after the work they are doing using
git checkout -b *mybranchname*.
2. Do some work....
3. When a branch is ready to be pushed up to master, the branch owner checks the following:
-local master is up to date with Git master, and local branch is up to date with local master
- (unless you are sure, to be safe always just switch to master, git pull origin master, switch to your branch,
and git merge master.)
-all tests are green
-bundle has been done and all gems are updated - bundle
-migrations are current(if using a DB) - rake db:migrate
4. Push your branch up to a branch on the origin repo with - git push origin *yourbranchname*.
5. Go to Github and open a pull request for the branch you just pushed up. Tell your peeps to check it out.
5. Someone else checks the files changed, and either approves or rejects.
- if rejected, fix issues and re-push the branch. Github will update it automatically.
- if approved and merged, EVERYONE should:
- pull down from Github master to their local master
- merge local master to the branch they are working on.
- bundle
- if working with a DB, rake db:migrate to make sure migrations are current(be careful if you are also working
on migrations at the same time)
- run tests to make sure nothing exploded.
NOTE: If there are multiple people who have pull requests open, they should be merged one at a time, fixing merge
conflicts as they arise.
6. Once your branch is merged, you can use git pull origin master to make sure things are in sync.
7. Delete your old branch with git branch -D *branchtobedeleted*.
8. Create a new branch with git checkout -b *newbranchname*.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment