Tuesday, February 8, 2011

Git crib sheet

Been using Git lately. 
Much goodness to be had, but some commands are a little unfamiliar if coming from Subversion.
So, here's my crib sheet of handy Git recipes I think will be useful.


Configure your Git
bash-3.2$ git config --global user.name "CustardCat"
bash-3.2$ git config --global user.email "custard@cpan.org"
bash-3.2$ git config --global -l
user.name=CustardCat
user.email=custard@cpan.org




Create a new project and commit your code...

# Create a project and cd into it..
cd my_project

# Make the project a git project
git init

# Add all the files in the project to git
git add .

# Commit your local files
git commit




Get status, logs & diffs
# Status of changes
git status

# Diffs
git diffs

# Add more files
git add file1 file2 etc..

# See what's been happening
git log
git log --stat --summary
git log --pretty=oneline

# See diffs between versions. nb. only need first few chars of version
git diff 16f8..8bd2
git diff HEAD^..HEAD




Reverting & adding files
# Throw all uncommitted changes away (revert)
git reset --hard

# Edit the commit message
git commit --amend

# Add forgotten files to the last commit
git reset --soft HEAD^
git add forgotten.file.txt
git commit




Ignoring files

#Ignore files - also can use wildcards
vi .gitignore
git add .gitignore
git commit




Branches & Tags
# Making a branch
git branch splendid_branch

# View Branches - asterisk shows the current branch
git branch

# Switch to the new branch
git checkout splendid_branch

# Switch back to master
git checkout master

# Merge the branch into master
git merge splendid_branch

# Tagging - use tag names in place of the revision ids
git tag v1.2.3




Cloning & updating clone
# Clone a repository
git clone git://git.kernel.org/.../blah my_blah
cd my_blah

# Pull changes from origin (update from upstream)
git pull origin

# Check logs since pull
git log -p ORIG_HEAD../some/dir

# Extract patches 
git format-patch HEAD^
git format-patch origin

# Pull from branch and merge
git pull git://git.kernel.org/.../some_branch.git ALL

# Revert a pull
git reset --hard ORIG_HEAD

# Garbage collect
git gc

# Update tags from original
git fetch --tags





No comments:

Post a Comment