Git - Part 9: Remote repository on GitHub

1. When logged in to GitHub, click green button "New repository"

2. Choose repository name, enter description and click "Create repository"

3. Now you have two options: create a new repository on the command line or push an existing repository on the command line

New repository on the command line

Use this option, if you doesn't have an existing local repository initialized.

echo "# Some text" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:username/repository.git
git push -u origin master

Push an existing repository from the command line

Use this option, if you already have an existing local repository initialized and you want to push it to GitHub.

git remote add origin git@github.com:username/repository.git
git push -u origin master

Cloning remote repositories

Clone remote repository to a specific folder

git clone git@github.com:username/repository.git someFolderPath

Clone remote repository to a folder with the same name as repository

git clone git@github.com:username/repository.git someDirectoryPath

Remote branching

Checkout (or create) some local branch

git checkout feature-branch

Push branch to a remote repository

git push origin feature-branch

Delete remote branch

git push origin :feature-branch

OR

git push origin --delete feature-branch

Git - Part 8: Connect to GitHub via SSH protocol

1. On your Windows machine I suggest installing Cmder console emulator, visit http://cmder.net/ and then download and install the Full package (with git-for-windows)

2. Open Cmder or other console software on your Windows machine, navigate to root directory, create .ssh directory and enter it

cd ~
mkdir .ssh
cd .ssh

3. Create SSH key pair id_rsa & id_rsa.pub (-t is type, -b is bytes, -f is filename)

ssh-keygen -t rsa -b 4096 -f id_rsa -C "email@domain.com"

When prompted for password, just hit Enter key - do not enter the password.

4. Display the public key on the screen, select the printed text and copy it to clipboard

cat id_rsa.pub

5. Open GitHub website (www.github.com) and login with your account

6. Navigate to Settings / SSH and GPG keys

7. Click "New SSH key" button on the top right corner

8. Enter title (can be anything you want), paste previously copied public key to the "Key" text area and click "Add SSH key"

9. Go back to Cmder (or any other console emulator) and enter

ssh -T git@github.com

10. Enter 'yes' when you get prompted with: Are you sure you want to continue connecting (yes/no)?

That's it. Now you do not have to enter your password every time when you push your code to GitHub.

Git - Part 7: Stash away changes

Stash away changes

git stash

Check stash list

git stash list

Apply the most recent stash and leave it on the list

git stash apply

Apply specific stash and leave it on the list

git stash apply stash@{0}

Apply the most recent stash and clear it from the list

git stash pop

Apply specific stash and clear it from the list

git stash pop stash@{0}

Delete the most recent stash from the list

git stash drop

Delete specific stash from the list

git stash drop stash@{0}

Apply stash to a new branch called feature

git stash branch feature

Exercise

  1. Create index.html file with content <h1>Hello World!</h1> and blank test.html file
    touch index.html
    vi index.html
    touch test.html
  2. Add all to staging area and commit to master branch
    git add .
    git commit -m "Initial commit"
  3. Create new branch named 'feature-branch'
    git checkout -b feature-branch
  4. Edit file test.html and add content <h2>This is a test</h2>
    vi test.html
  5. Stash the changes
    git stash
  6. Checkout master branch
    git checkout master
  7. Edit file index.html and change <h1> tags to <h3> tags
    vi index.html
  8. Add and commit changes
    git add .
    git commit -m "Change header tags"
  9. Checkout branch feature-branch
    git checkout feature-branch
  10. Check stash list
    git stash list
  11. Apply stash with 'pop' command
    git stash pop
  12. Add and commit changes to feature-branch
    git add .
    git commit -m "Changes applied"
  13. Checkout master branch and merge feature-branch
    git checkout master
    git merge feature-branch

Git - Part 6: Handling Merge Conflicts

Creating conflict and merging branches

git checkout master
git merge feature-branch

Notice on conflicts
Automatic merge failed; fix conflicts and then commit the result.

Show merge conflicts

git diff path/to/file.ext

Merge conflict example

<<<<<<< HEAD
Content on current branch (master)
=======
Content on merged branch (feature-branch)
>>>>>>> feature-branch

Exercise

  1. Create Git repository, add and commit file index.html with content <h1>Hello world!</h1>
    git init
    touch index.html
    vi index.html
    git add index.html
    git commit -m "Initial commit"
  2. Create a new branch called 'conflict-branch'
    git checkout -b conflict-branch
  3. Edit index.html file and change h1 tags to h2
    vi index.html
  4. Add and Commit to conflict-branch branch with a message "Change h1 to h2"
    git add .
    git commit -m "Change h1 to h2"
  5. Checkout master branch
    git checkout master
  6. Edit index.html file and change h2 tags to h3
    vi index.html
  7. Add and Commit to master branch with a message "Change h1 to h3"
    git add .
    git commit -m "Change h1 to h3"
  8. Merge conflict-branch (see: CONFLICT; Automatic merge failed;)
    git merge conflict-branch
  9. Check Git status (see: both modified: index.html)
    git status
  10. Edit index.html file and fix conflicts; change:
    <<<<<<< HEAD
    <h3>Hello World!</h3>
    =======
    <h2>Hello World!</h2>
    >>>>>>> conflict-branch
    To:
    <h2>Hello World!</h2>
  11. Add and Commit to master branch with a message "Fix conflicts"
    git add .
    git commit -m "Fix conflicts"
  12. Check Git log (see: all commits)
    git log --oneline

Git - Part 5: Branching

Create and checkout a new branch

git checkout -b feature-branch

Checkout existing branch (eg. master)

git checkout master

List all branches

git show-branch --list

Merge changes from feature branch

git merge feature-branch

Delete feature branch

git branch -d feature-branch

Exercise

  1. Create new file index.html and add some content
    touch index.html
  2. Add index.html to the staging area and commit to master branch
    git add index.html
    git commit -m "Initial commit"
  3. Create new Git branch called 'feature-branch'
    git checkout -b feature-branch
  4. Create file feature.html and add some content
    touch feature.html
  5. Add and Commit all to this branch with message "Work on branch"
    git add --all
    git commit -m "Work on branch"
  6. Checout master branch
    git checkout master
  7. Merge changes from feature-branch
    git merge feature-branch
  8. Delete feature-branch
    git branch -d feature-branch
  9. Check branch list (see: only master branch)
    git show-branch --list
  10. Check git log (see: both commits)
    git log

Git - Part 4: Fixing and Amending commits

Soft reset - resets to the previous state, but leaves your changes

git reset --soft UniqueHash

Hard reset - resets everything to the previous state, deletes your changes

git reset --hard UniqueHash

Amend commit - saves changes to the same previous commit

git commit --amend

Exercise: fixing commit

  1. Create new Git repository
    git init
  2. Create a new file index.html with content "Hello Wrold!"
    touch index.html
    vi index.html
  3. Add and Commit index.html file with message "Initial commit"
    git add index.html
    git commit -m "Initial commit"
  4. Check git status (see: On branch master nothing to commit, working directory clean)
    git status
  5. Check git log (see:Initial commit)
    git log --oneline
  6. Open index.html and change text to "Hello World!"
    vi index.html
  7. Add and Commit index.html file with message "Fix typo"
    git add index.html
    git commit -m "Fix typo"
  8. Check git log (see: both commits)
    git log --oneline

Exercise: amending commit

  1. Check git log and copy UniqueHash from Initial commit
    git log --oneline
  2. Hard reset repository to the initial commit
    git reset --hard UniqueHash
  3. Review index.html and check that the typo is there
    cat index.html
  4. Open index.html and change text to "Hello World!"
    vi index.html
  5. Add index.html to the staging area
    git add index.html
  6. Check git status (see: Changes to be committed: modified: index.html)
    git status
  7. Amend commit
    git commit --amend
  8. Check git log (see: only initial commit)
    git log --oneline
  9. Review index.html and check that the typo is fixed
    cat index.html

Git - Part 3: Status and Log

Check git status

git status

Check git log

git log

One-line git log

git log --pretty=oneline

One-line git log with short hash id's

git log --oneline

Show the latest commit for current branch

git branch -v

Git - Part 2: Repositories

Create a repository

Initialize a new Git repository

git init

Add and Commit to a repository

Stage Single file

git add index.html

Stage Multiple files

git add index.html index2.html

Stage All (new, modified, deleted) files

git add -A

Stage All (new, modified, deleted) files

git add --all

Stage All (new, modified, deleted) files

git add .

Stage Modified and Deleted files only

git add -u

Stage New and Modified files only

git add --ignore-removal .

Commit to repository

git commit -m "This is commit message"

Exercise

  1. Create a new file index.html and add some content
    touch index.html
  2. Add index.html to Git
    git add index.html
  3. Check git status (see: index.html is ready for commit)
    git status
  4. Open index.html and change some of the content
    vi index.html
  5. Check git status (see: previous index.html is still ready for commit; index.html is modified)
    git status
  6. Commit changes
    git commit -m "Initial commit"
  7. Check git status (see: index.html is still modified)
    git status
  8. Add index.html
    git add index.html
  9. Commit index.html
    git commit -m "Content is changed"
  10. Check git status (see: nothing to commit)
    git status
  11. Check git log (see: both commits)
    git log

Git - Part 1: Settings

Global configuration is stored in a file .gitconfig your root (cd ~) folder.

Set your name

git config --global user.name = 'John Doe'

Set your e-mail

git config --global user.email = 'info@yourdomain.com'

Set default editor, which will be used for commit messages, resolving conflicts, etc.

git config --global core.editor vi

OR

git config --global core.editor notepad

Turn off a warning about LF replacement

git config --global core.autocrlf true