Git 2.0

Some more stuff about Git.

Ok, this time a little bit more about Git. I will show you some basic commands.

Create a new directory in your home called octobox.

cd ~/octobox
git init

This will create local repository in ~/octobox/.git.

Now you can add some file to that directory:

touch readme.txt 
open readme.txt

Write some text, then save.

git status

Will show you that you have untracked file, so stage it:

git add readme.txt

Or if you have more that one:

git add --all

Now you can commit it:

git commit -m "first commit"

After you make more commits, you can go through them via

git log

To see unstaged changes, type:

git diff

For staged but not committed changes, type:

git diff --staged

If you want to reset modifications of the staged file, type:

git checkout -- filename

This will get a rid of all changes since last commit.

If you stage some file by mistake or want to stop tracking changes, you can unstage it by

git reset HEAD filename

And that file will be no longer tracked for changes.

To stage the file with changes and commit it at the same time, just use:

git commit -a -m "your message"

But beware that this will not commit new untracked file you did not staged yet.

If that happens, first stage new file and then you can ammend it to the same commit:

git commit --amend -m "changes message"

Nice explanation of the concept is here:

Git essentially has 4 main statuses for the files in your local repo:

untracked: The file is new, Git knows nothing about it. If you git add , it becomes:

staged: Now Git knows the file (tracked), but also made it part of the next commit batch (called the index). If you git commit, it becomes:

unchanged: The file has not changed since its last commit. If you modify it, it becomes:

unstaged: Modified but not part of the next commit yet. You can stage it again with git add As you can see, a git add will track untracked files, and stage any file.

Also: You can untrack an uncommited file with git rm — cached filename and unstage a staged file with git reset HEAD

Credits to Emmanuel Joubaud

If you happen to forget some files in commit, you can easily get it back to staged state this way:

git reset --soft HEAD^

Undo last commit and all changes completely:

git reset --hard HEAD^

Undo last two commits:

git reset --hard HEAD^^

To add new remote repository:

git remote add origin  
   
git@example.com:example/repository.git

To push your commit to remote repository type:

git push -u origin master

If it’s for the first time, you might be asked for your username and password.

Let’s move to branching.

To create a branch, just type:

git branch name-of-branch

To switch to that branch type:

git checkout name-of-branch

To switch back to master branch — you guessed it:

git checkout master

To see the list of branch and the active one, type:

git branch

To merge name-of-branch to master, make sure you switched to master and then type:

git merge name-of-branch

Then you can safely delete that branch:

git branch -d name-of-branch

To create new branch (let’s say admin) and switch to it at the same time, type:

git checkout -b admin