Git cheat sheet

Ref:

Configuration

# Configure local user info:
git config --global user.name "John Doe"
git config --global user.email "johndoe@example.com"

Setup Repository (server)

# Create repository:
git init --bare --shared foo.git # initialized empty shared Git repository in foo.git

# If you access your Git repository over http: or https: URLs
git  update-server-info

# Now change group access to repository (linux)
chgrp -R <group_name> foo.git

Setup Repository (local)

# Create local repository:
git init
git add <file_name> # you can use '.' (without quotes) to add everything
git commit -a -m 'Added something'

# Then make the link to the shared repository
git remote add origin <user>@<host>:/<repository_path>/foo.git

# We push to the remote repository
git push origin master

Working with git

# Clone existing repository (ssh)
git clone <user>@<host>:/<repository_path>/foo.git

# Show local repository status
git status

# Show remote branches
git branch -r

# Checkout remote branch
git checkout -t origin/<branch_name>

# Checkout remote tag
git checkout tags/<tag_name>

# Switch local branches
git checkout <branch_name>

# Update local branch
git pull origin <branch_name>

# Update remote branch with local commits
git push origin <branch_name>

# Commit changes in local branch
git add <file_name>
git commit -m "<message>"
# or commit all modified files
git commit -a -m "<message>"

#Revert local changes (http://progit.org/2011/07/11/reset.html)
git reset --soft HEAD
git reset --mixed HEAD
git reset --hard HEAD

# Working with remotes (http://progit.org/book/ch2-5.html)
git remote -v
# change remote 
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git


# Delete tag
git tag -d <tag_name>
git push origin :refs/tags/<tag_name>

Track remote branch

# As of Git 1.8.0:
git branch -u upstream/foo

# Or, if local branch foo is not the current branch:
git branch -u upstream/foo foo

# Or, if you like to type longer commands, these are equivalent to the above two:
git branch --set-upstream-to=upstream/foo
git branch --set-upstream-to=upstream/foo foo

# As of Git 1.7.0:
git branch --set-upstream foo upstream/foo

Undo merge

git merge --abort # since git version 1.7.4
git reset --merge # prior git versions

December 1st, 2014

Posted In: tutorials

Tags:

Leave a Comment