Git Basic Guide (on Windows)

This post describes how to setup Git (on Windows), and also describes basic Git commands.
These commands should be sufficient to perform most of Git operations for revision control.

Setting up Git on Windows

  1. Install Git for Windows. The installer can be downloaded from here.
  2. Open Git Bash.
  3. git config --global user.name "username"
  4. git config --global user.email [email protected]
  5. Generate SSH key to access remote repositories.

Git Commands

  • Repository Management

    • git init
      Creates an empty repository.
    • git add .
      Adds all files at current directory, recursively.
    • git mv current-file new-file
      Moves/renames a file Git-way.
    • git rm removing-file
      Removes a file Git-way.
  • Showing Changes

    • git status
      Shows current changes.
    • git diff
      Shows differences from the last commit.
  • Committing Changes

    • git commit -m "commit message"
      Commits changes to a local repository.
    • git commit -am "commit message"
      Commits all modifications to a local repository.
    • git add -A && git commit -m "commit message"
      Commits all changes including created and deleted files.
    • git commit --amend -m "new commit message"
      Amend/change last commit message.
    • git log
      Lists commits. ('q' to quit)
    • git checkout
      Restores previous commit. ('-f' to force overwrite)
  • Branch Management

    • git checkout -b new-branch-name
      Checkouts and creates a new branch.
    • git branch
      Lists existing branches.
    • git checkout master
      Switches to master branch.
    • git merge branch-name
      Merges a branch to master.
    • git branch -d branch-name
      Deletes a branch.
  • Command Alias

    • git config --global alias.alias-name command-string
      Associates command-string to alias-name.
  • Remote Repository Management

    • git remote add remote-name remote-url
      Adds an entry for a remote repository at remote-url and refer it as remote-name.
    • git push -u remote-name master
      Pushes master to remote-name (at remote-url).

      git push remote-name
      Pushes master to remote-name (after pushing once).
    • git remote rename current-remote-name new-remote-name
      Renames a remote name.
    • git clone remote-url
      Clones a remote repository from remote-url.

Creating New Local and Remote (SSH) Repository

Below shows how to create a new local and remote repository, connecting through SSH.
The environment on the remote repository must have Git installed as well.
Also, this tutorial shows how to setup the post-receive hook.
This is very useful for deploying as it can checkout all files when pushed.

  1. Setup Command Alias (on local)

    1. git config --global alias.commit-all '!git add -A && git commit'
      Create a command alias to commits all changes including created and deleted files.
      This is just to simplify the commit command (i.e., git commit-all), and only needs to be done once.
  2. Setup Local Repository (on local)

    1. mkdir new-app-dir
    2. cd new-app-dir
    3. git init
      Creates an empty repository.
    4. Create .gitignore file and list files to exclude from revision control.
    5. git commit-all -m "commit message"
      Commits all changes including created and deleted files.
  3. Setup Remote Repository (on remote)

    1. mkdir new-app-dir
    2. cd new-app-dir
    3. mkdir .git
    4. cd .git
    5. git init --bare
      Creates a bare repository.
    6. cd hooks
    7. Create a file called post-receive with the following lines:

      #!/bin/sh GIT_WORK_TREE=.. export GIT_WORK_TREE git checkout -f
      [open]
    8. chmod +x post-receive
  4. Push to Remote Repository (from local)

    • git remote add remote-name ssh://username@example.com/app-path/.git
      Adds an entry for a remote repository using SSH and refer it as remote-name.
    • git push -u remote-name master
      Pushes master to remote-name (using SSH).

      git push remote-name
      Pushes master to remote-name (after pushing once).
    • Add all files excluded by .gitignore but needed for the application.

Setting up GitHub Repository

Below shows how to setup a remote repository on GitHub.com.
GitHub provides a great service for maintaining central repositories (free for public repositories).

  1. Create SSH Key

    1. Open Git GUI
    2. Go to Help -> Show SSH Key
    3. Click Generate Key
    4. Enter passphrase
    5. Click Copy To Clipboard
  2. Setup GitHub Repository

    1. Go to GitHub.com.
    2. Create an account if you haven't.
    3. Go to Account Settings.
    4. Go to SSH Keys.
    5. Click on Add SSH key.
    6. Paste the SSH key (from the previous section).
      (Title can be anything, i.e., "Git GUI generated key")
    7. Create a new repository.
  3. Push to GitHub.com Remote Repository (from local)

    1. git remote add origin [email protected]:username/repository-name.git
      Adds an entry for a remote repository on GitHub and refer it as origin.
    2. git push -u origin master
      Pushes master to origin (on GitHub).

      git push
      Pushes master to origin (after pushing once).

Resource: