Version Control

Git Basics – Part 1

Initializing a Repository in an Existing Directory

git init

Cloning existing repository

git clone <url> <folder>

Track existing files

git add *.php
git add .
git add README

Commit changes

git commit -m 'my comment'

Automatically stage all tracked-modified files and commit – Skip the staging area

git commit -am 'minor'

the above command runs git add on every file that is tracked, allowing to snapshot every file that has been modified since your last commit.

Check repository status

git status -s

Ignoring files and folders

Create a .gitignore file in the folder where you would like to ignore files and directories and add your rules.

for example:

*~ #ignore backup files
logs/**/* #ignore all directories and files in a directory
<pre>*.txt       # ignore all txt files
!README.txt # but track README.txt
/NOTES      # only ignore the NOTES file in root directory
tmp/        # ignore all files in the tmp/ directory

Removing file from staging area and from disk

git rm filename

Remove file from staging area but leave it in the working directory

git rm --cached filename

Unstage a file

git reset HEAD test.php

See what you’ve changed but not yet staged

git diff

See what you’ve staged and is ready for your next commit

git diff --staged

Moving files

git mv move_from_filename move_to_filename

Setting your username

git config --global "gkarpouzas"

Setting your email

git config --global ""

Undo your last commit in case you forgot to add some files

git commit --amend

Revert file back

git checkout -- test.php

Listing your remotes

git remote -v

Sending your work to the server

git push <remote_name> <branch_name>

for example:

git push origin master