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# Windowshttp://git-scm.com/download/winOrhttp://windows.github.comFirst-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 ConfigC:\Program Files (x86)\Git\etc\gitconfigConfig 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 addressgit config --global user.name "John Doe"git config --global user.email johndoe@example.com# Editorgit config --global core.editor vim# orgit config --global core.editor "'C:\Program Files (x86)\Notepad++\notepad++.exe' -multilnst -nosession"Check you settings
git config --listGit 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 initClone an existing repository
git clone https://github.com/repo.gitCheckout an existing repository into a non-empty directory
cd ${non-empty directory}git init .git remote add origin git@server2:/git/control-repogit fetch origingit checkout productionUsing a Repo
Add a file to staging
git add filenameAdd files to the master repository
git commit -m "Details of changes"Show status of Repository
git status# Shortgit status -sIgnoring files
cat .gitignore*.[ao]# Start lines with a slash to prevent recursion/# Slash at the end denotes a directorydirectory_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 matchDisplays the actual changes rather than the changed files that git status does
git diff #Compared working directoy with staginggit diff --staged # Compares staged to last commitRemoving files
#Remove files from hard drive as well.git rm filegit commit#Remove files from git tracking but not hard drivegit rm --cached filegit commitRename a file
git mv old_file new_fileViewing Commit History
git log-p #Show differences-2 # Limit to 2 entries--stat #Abbreviated stats--pretty= #Changes the format of the output.onelineshortfullfullerformat--graphLimiting 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 stringUndoing 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 --amendUnstage a staged file
git reset HEAD filenameUnmodifying a Modified File
git checkout -- filenameWorking With Remotes
List remote servers
git remotegit remote -vAdding a Remote Repository
git remote add shortname URLThen you can use git fetch to pull down new files from shortname
git fetch shortnameFetching 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_namegit pull will fetch the remote content and attempt to merge the content automatically.
git pull remote_namePushing 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_namegit push origin masterInspecting a Remote
Finding out more about a remote can be done with git remote show.
git remote show originRemoving and Rename Remotes
Rename
git remote rename old_name new_nameRemove
git remote remove nameTagging
You can tag specific points in history as important. Maybe a release point like v1.0
List tags
git tagSearching 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.4Tagging after severAll other commits can be done by specifying the commit checksum.
git tag -a v1.4 fd4dfa7Sharing Tags
git push does not transfer tags to remote servers. You need to explicitly push tags to remote servers.
#Push a single taggit push origin v1.4#Push all tagsgit push origin --tagsChecking 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_nameAliases
Aliases can be created as shortend versions of regular commands.
git config --global alias.co checkoutThis 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.gitgit init --bare new_repo.gitYou can now clone from this repository.
git clone user@server1.grow4.co.uk:/apps/git/new_repo.git/