Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Install Git
# Linux. You can install git-all which will install other git files you may not need like cvs, svn, p4, etc.
yum install git
# Windows
http://git-scm.com/download/win
Or
http://windows.github.com
First-Time Git Setup
Config file locations
# Linux
## System config
/etc/gitconfig
## User specific config
~/.gitconfig or ~/.config/git/config
## Git directory config file.git/config
# Windows
$HOME\.gitconfig (C:\Users\$USER for most people)
## System Config
C:\Program Files (x86)\Git\etc\gitconfig
Config settings
You need to do this only once if you pass the --global option, because then Git will always use that information for anything you do on that system. If you want to override this with a different name or email address for specific projects, you can run the command without the --global option when you’re in that project.
# Username and Email address
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
# Editor
git config --global core.editor vim
# or
git config --global core.editor "'C:\Program Files (x86)\Notepad++\notepad++.exe' -multilnst -nosession"
Check you settings
git config --list
Git States
Modified - are files that have been changed but not yet committed.
Staged - are modified files marked to be committed with the next commit.
Committed - are files stored in the database.
Setting Up Repositories
Initialise a New Repository
git init
Clone an existing repository
git clone https://github.com/repo.git
Checkout an existing repository into a non-empty directory
cd ${non-empty directory}
git init .
git remote add origin git@server2:/git/control-repo
git fetch origin
git checkout production
Using a Repo
Add a file to staging
git add filename
Add files to the master repository
git commit -m "Details of changes"
Show status of Repository
git status
# Short
git status -s
Ignoring files
cat .gitignore
*.[ao]
# Start lines with a slash to prevent recursion
/
# Slash at the end denotes a directory
directory_name/
# Regular Expressions
* Zero or more characters
? one or more characters
** Match nested directories a/**/z will match a/z, a/b/z, a/b/c/z
! Negate a match
Displays the actual changes rather than the changed files that git status does
git diff #Compared working directoy with staging
git diff --staged # Compares staged to last commit
Removing files
#Remove files from hard drive as well.
git rm file
git commit
#Remove files from git tracking but not hard drive
git rm --cached file
git commit
Rename a file
git mv old_file new_file
Viewing Commit History
git log
-p #Show differences
-2 # Limit to 2 entries
--stat #Abbreviated stats
--pretty= #Changes the format of the output.
oneline
short
full
fuller
format
--graph
Limiting Log Output
-(n) Show only the last n commits
--since, --after Limit the commits to those made after the specified date.
--until, --before Limit the commits to those made before the specified date.
--author Only show commits in which the author entry matches the specified string.
--committer Only show commits in which the committer entry matches the specified string.
--grep Only show commits with a commit message containing the string
-S Only show commits adding or removing code matching the string
Undoing Things
The --amend will amend your last commit using the staging area. If no files have been changed or added, only the commit message will be amended.
git commit --amend
Unstage a staged file
git reset HEAD filename
Unmodifying a Modified File
git checkout -- filename
Working With Remotes
List remote servers
git remote
git remote -v
Adding a Remote Repository
git remote add shortname URL
Then you can use git fetch to pull down new files from shortname
git fetch shortname
Fetching and Pulling from Remotes
Once a repository has been cloned, you can run git fetch to pull new content. This only places the new content into your local repository. You still need to merge it into your work.
git fetch remote_name
git pull will fetch the remote content and attempt to merge the content automatically.
git pull remote_name
Pushing to Remotes
Once you finished working locally and you have commited your content, you can push your changes to the remote server.
git push remote_name branch_name
git push origin master
Inspecting a Remote
Finding out more about a remote can be done with git remote show.
git remote show origin
Removing and Rename Remotes
Rename
git remote rename old_name new_name
Remove
git remote remove name
Tagging
You can tag specific points in history as important. Maybe a release point like v1.0
List tags
git tag
Searching for tags
git tag -l "v1.8.5*"
Creating Tags
Git uses 2 types of tags. Lightweight and annotated. Annotated is the recommended tag as it has a lot more information and are stored as full objects.
git tag -a v1.4 -m "Info message"
git show tag v1.4
Tagging after sever
All other commits can be done by specifying the commit checksum.
git tag -a v1.4 fd4dfa7
Sharing Tags
git push does not transfer tags to remote servers. You need to explicitly push tags to remote servers.
#Push a single tag
git push origin v1.4
#Push all tags
git push origin --tags
Checking out Tags
You can't really check out tags but you can create a new branch from a tag.
git checkout -b branch_name tag_name
Aliases
Aliases can be created as shortend versions of regular commands.
git config --global alias.co checkout
This would make git checkout the same as git co.
External commands can be run with an ! infront of it.
git config --global alias.visual '!gitk'
Configure a Git Server
Git can be server using the protocols SSH, HTTP, Git and Local. The Git documentation has all this information and so we will just do a quick run through.
Create a bare repository by either cloning an existing repository or creating a new one.
git clone --bare existing_repo new_repo.git
git init --bare new_repo.git
You can now clone from this repository.
git clone user@server1.grow4.co.uk:/apps/git/new_repo.git/