-
Notifications
You must be signed in to change notification settings - Fork 2
Git Cheat Sheet
It is very important that you make your commits as you work. Commit every atomic piece of work separately and DO NOT just make one huge commit at the end.
See what has changed, using the following command. It will divide the files into three different categories.
The first category: is the “staged” files, which are the files that you have added them but you did not commit them yet.'
The second category: is the “not staged” files, which are the files that you have modified them and where in your repo before.
The third category: is the “untracked” files, which are the new files that you have created them and you will find the ignored files here too.
Note that you do not have to find the categories when you run the command. `git status`
### Add files and changes to the staging area
**PLEASE, do not add anything except the files that you modified them or created them** `git add some/thing/here.js`
### Delete a file? Note, this means delete it and when committing, commit the fact that it has been deleted.
So if you are going to delete a file this will delete it from the main repo when you merge, so PLEASE do not use it unless it was a file you have created and the only one using it
git rm that/other/thing/there.js
`git status`
If you wish to remove something from the staging area for example if you added a file and you wish to not commit it now
git reset some/thing/here.js
git status
git commit -m "Your commit message here"
## Creating a new branch Let’s say you have created a new issue and you have to create a branch for that issue: So you have to be in develop before you create your branch and develop should be up to date.
`git checkout develop`
Please pull develop before creating your branch to get the most recent version.
`git pull origin develop`
Then you create your new branch.
`git branch “BranchName”`
Then switch to your new branch by writing the command found below.
`git checkout “BranchName”`
Then you can start working normally in your branch.
Whenever you are going to start modifying in the project, PLEASE check that you are in your BRANCH and start working by pulling develop from your BRANCH to be updated.
So if you tried to merge a branch and git told you hurtful things like:
CONFLICT (add/add): Merge conflict in Idearator/test/functional/home_controller_test.rb
CONFLICT (modify/delete): Idearator/db/schema.rb deleted in HEAD and modified in ``C1_mennaamr_#91_Facebook_login. Version C1_mennaamr_#91_Facebook_login of Idearator/db/schema.rb left in tree.
Removing Idearator/db/migrate/20130326183608_add_devise_to_users.rb ......
These are the dreaded CONFLICTS.
Conflict resolution is simple as long as you are careful. You have to take care of each message before git will allow you to commit.
There are two major kinds of conflicts, those that introduce conflicting modifications and those that delete files altogether (DO NOT IGNORE THESE!)
ESLAM
Examples for the first kind:
CONFLICT (add/add): Merge conflict in Idearator/test/functional/home_controller_test.rb
CONFLICT (content): Merge conflict in Idearator/app/views/layouts/application.html.erb
To resolve this kind of conflict you must open the file in question, then find the parts that are marked to be in conflict like so:
require 'test_helper'
class HomeControllerTest < ActionController::TestCase
<<<<<<< HEAD
# test "the truth" do
# assert true
# end
=======
test "should get index" do
get :index
assert_response :success
end
>>>>>>> C1_mennaamr_#91_Facebook_login
end
The part between <<<<<<< HEAD and ======= is the change that you introduced, while the part between ======= and >>>>>>> C1_mennaamr_#91_Facebook_login is the change that the branch you are merging introduced. You should now see which version you want to keep (possibly a mix of the two!) and delete the merge markers (the >>>>>>> ..... and ======= etc) Save the file. git add the file. Move to the next conflict.
Examples for the second kind:
CONFLICT (modify/delete): Idearator/db/schema.rb deleted in HEAD and modified in
C1_mennaamr_#91_Facebook_login. Version C1_mennaamr_#91_Facebook_login of Idearator/db/schema.rb left in tree.
Removing Idearator/db/migrate/20130326183608_add_devise_to_users.rb
As the message says, the HEAD ref(erence) (that is, your branch) has deleted this file, while the branch you are merging has modified or kept it. You need to make a choice.
If you want to keep the file:
git checkout HEAD Idearator/db/schema.rb
git add Idearator/db/schema.rb
If you want to delete the file, like should be the case for this specific example (schema.rb), it depends on what the exact message is. If it said Removing Idearator/db/schema.rb then you don't need to do anything. But it actually did not remove it in our example (see the messages above! It says left in tree at the end), so you need to tell git to remove it:
git rm Idearator/db/schema.rb
Do NOT skip these conflicts as these WILL lead to mysterious deleted files later on.
Move to the next conflict.