understand the working index, staging, and committing
work with others in the same repo
use branches effectively
get yourself out of a git mess
merging and resolving merge conflicts
$ git status
$ git status -v
what you see in your editor and where you make your changes
$ git add .
$ git add app/controllers/application_controller.rb
$ git add -p app/controllers/application_controller.rb
a snapshot of your working tree at a particular point in development
lets you gather changes for the next commit
$ git commit
$ git commit -m 'Initial commit'
$ git commit -v
Your copy of a project, initialized as a git repository
(i.e., it has a .git directory)
PRO TIP: if you aren't into vim, set your own code editor in git config
$ git pull origin master
$ git push origin master
The shared copy of the repo that lives on a remote server
Very often this is GitHub, but it doesn't have to be.
PRO TIP: Always pull before you push!
Conventionally named
origin
but doesn't have to be.
$ git log --oneline
commit e83c5163316f89bfbde7d9ab23ca2e25604af29 Author: Linus Torvalds <torvalds@ppc970.osdl.org> Date: Thu Apr 7 15:13:13 2005 -0700 Initial revision of "git", the information manager from hell
$ git show e83c516
A branch is essentially another copy of your repo that will allow you to isolate changes and leave the original copy untouched.
You can later choose to combine these changes in whole or part with the another branch or the mainline of development, or not.
$ git branch
$ git checkout -b my_new_feature
$ git checkout master
$ git checkout -
Since you are deploying to Heroku, it's important to have a branch that you all agree will be deployable at all times.
Don't introduce broken code to the production branch!
This is where you will do your development. Break stuff here!
* This is basically the GitHub Flow.
There are other established workflows out there.
On local FEATURE branch:
Make your commit(s).
On local MASTER branch:
$ git pull origin master
Make sure local master is up to date with remote master.
$ git merge master
Keep this branch up to date with master.
Resolve merge conflicts here on the feature branch.
Create a pull request to origin or upstream master.
THEN...
👆This command merges master into the feature branch.
PULL OFTEN so you will have your colleagues' work.
PUSH OFTEN to keep your remote branch up to date.
COMMIT OFTEN to collaborate effectively.
PULL REQUEST
when your feature branch is ready to merge to master
It's your job to keep your feature branch up to date with master.
Tell your team when you are pushing up changes or have opened a pull request that requires their review.
Say why a change is needed or what it does.
Make sure your local and remote branches are in sync.
$ git checkout master
$ git merge featurebranch
$ git log --oneline
brings two lines of development together while still preserving the lineage of each commit
$ git checkout master $ git pull origin master
$ git checkout featurebranch $ git merge master
Conflicts occur when changes occurred in each branch to the same line or group of lines, and git doesn't know which to choose.
Here are lines that are either unchanged from the common ancestor,
or cleanly resolved because only one side changed.
<<<<<<< yours:sample.txt
Your changes are reflected here in this section.
=======
Their changes are here in this section, in conflict with yours.
>>>>>>> theirs:sample.txt
And here is another line that is cleanly resolved or unmodified.
The area where a pair of conflicting changes happened is marked with markers <<<<<<<, =======, and >>>>>>>.
The part before the ======= is typically your side,
and the part afterwards is typically their side.
Don't worry. Git is your friend.
and the file has gone back to its state at the previous commit.
If you haven't added (staged) files or committed yet.
$ git checkout app/views/layouts/application.html.erb
...Your changes are gone from the working tree (and your editor)
The file is removed from staging, but your working copy will be unchanged.
$ git reset app/views/layouts/application.html.erb
When you want to keep the changes in your working tree
$ git checkout app/controllers/main_controller.rb
Your changes are gone from the working tree.
Reset the working tree to its state at the last commit
Just reset the file first to unstage it.
$ git checkout app/controllers/main_controller.rb
$ git reset app/controllers/main_controller.rb
$ git log --oneline
Git lets you go back to any previous commit.
$ git revert 53d23c4
# Your default editor will open here
# you can just save it and close it as is.
$ git log --oneline
Notice that the original bad commit is still there, but now you also have another commit that undoes the changes introduced by the original one.