//

GIT STAGING & REMOTES

Let’s say you are working on your local branch, made some changes and now not sure which files you have changed, how are you going to find your changes? You can find your changes by running the git diff command as below:

git diff
diff --git a/LICENSE b/LICENSE
index 7e4922d..442669e 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2014
+Copyright (c) 2015

git diff shows unstaged differences since your last commit. The last two lines show the line removed and then the new line added in this example.

-Copyright (c) 2014
+Copyright (c) 2015 

Now let's add this file to the stage and run git diff.

git add LICENSE
git diff 

git diff doesn't output anything because there are no more differences since the last file added. But if you add --staged flag to your command:

git diff --staged
diff --git a/LICENSE b/LICENSE
index 7e4922d..442669e 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2014
+Copyright (c) 2015

It will show show the staged differences. Run git status to show the changes need to be commited.

UNSATGING FILES

What if we want to unstage some files we have staged?

git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: LICENSE
#
git reset HEAD LICENSE
Unstaged changes after reset:
M LICENSE
``

**git reset HEAD LICENSE** unstages the LICENSE file.

## DISCARDING CHANGES

If we want to discard the changes we staged: 

```sh
git checkout -- LICENSE 

This will blow away all changes since last commit.

SKIP STAGING AND COMMIT

If wanted to skip staging and only want to commit, we can run following command.

git commit -a -m "Modify readme"

But there is a caveat, this will add changes from all tracked files, but doesn't add new(untracked) files.

UNDOING A COMMIT

There may be a time that you made a commit, and immediately realised that you didn't want to commit some files. How do we undo it?

git reset --soft HEAD^
git status
Move to commit before ‘HEAD’
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage) #
# modified: README.txt

git reset --soft HEAD^, --soft will reset the committed files into staging. And HEAD^ will move the commit before 'HEAD'. Now we can make changes and commit.

If your commit went wrong, you can blow away your commit and all changes by running following command:

git reset --hard HEAD^  

You can undo last 2 commmits and all changes:

git reset --hard HEAD^^ 

ADDING TO A COMMIT

If you forgot to add a file to a commit, we can add a file later by using --amend option.

git add todo.txt
git commit --amend -m "Modify readme & add todo.txt."
[master fe98ef9] Modify readme and add todo.txt.
 2 files changed, 2 insertions(+), 1 deletions(-)
 create mode 100644 todo.txt

We can modify the commit message with the new message. This command makes whatever has been staged to the last commit.

HOW DO WE SHARE OUR CHANGES?

To share our commits with others we need to use push and pull commands.

REMOTE REPOSITORY HOSTING

There are few hosted solutions out there for access control etc. Eg:-Github, Bitbucket.

To add a remote our repository we have to specify an origin.

git remote add origin https://github.com/Sean/git-example.git   

Here origin is our name for this remote.

To get a list of all the remote repositories we have access to:

git remote -v   
origin https://github.com/Sean/git-example.git (fetch)
origin https://github.com/Sean/git-example.git (push)

To push to remote:

push -u origin master
Username for 'https://github.com': Sean
Password for 'https://Sean@github.com':

Counting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (11/11), 1.50 KiB, done.
Total 11 (delta 0), reused 0 (delta 0)
To https://github.com/Sean/git-example.git
 * [new branch]      master -> master

If you don't want to type the password every time you push to follow the link below:

https://help.github.com/articles/set-up-git

PULLING FROM REMOTE

To pull changes down from the remote:

git pull
 remote: Counting objects: 5, done.
 remote: Compressing objects: 100% (1/1), done.
 remote: Total 3 (delta 1), reused 3 (delta 1)
 Unpacking objects: 100% (3/3), done.
 From https://github.com/Sean/git-example
    fe98ef9..4e67ded  master     -> origin/master
 Updating fe98ef9..4e67ded
 Fast-forward
  todo.txt | 1 +
  1 file changed, 1 insertion(+)

HAVING MULTIPLE REMOTES

It's common to have multiple remotes contributing to your project.

To add new remotes:

$git remote add <name> <address>

To remove remotes:

$git remote rm <name>

To push to remotes:

$git push -u <name> <branch>

HEROKU REMOTE

Some of you might have a Heroku account up and running. It's useful to understand how Heroku handles git.

Creating dev-server-1426... done, stack is cedar
http://dev-server-1426.herokuapp.com/ | git@heroku.com: dev-server-1426.git
Git remote heroku added
git remote -v
heroku git@heroku.com: dev-server-1426.git (fetch) heroku git@heroku.com: dev-server-1426.git (push) origin https://github.com/Sean/git-example.git (fetch) origin https://github.com/Sean/git-example.git (push)

To push to heroku

git push heroku master