Git Cheatsheet

Install git

http://git-scm.com/book/en/Getting-Started-Installing-Git

Setting up git

Set your name and email
git config --global user.name "Your Name"
git config --global user.email "your_email@gmail.com”

Line ending preferences
git config --global core.autocrlf input
git config --global core.safecrlf true

Don’t save file permission changes
git config core.fileMode false

Add Aliases
nano ~/.gitconfig

Add shell Aliases
nano ~/.profile

Add the following to this file to allow shortcuts in the command line

Show current git branch in terminal
nano ~/.bash_profile

Start using git

git init

Add/update file to git
git add file.php

Add/update all files in directory to git
git add .

Commit code and view of what you are committing
git commit
This will then nano with all the files you are committing

Commit your code with message
git commit -m “commit message”

Amend a commit
git commit --amend -m “your amended commit message“

Show status
git status

Moving files

Move with git
git mv test.php test2.php

If you moved the file already some other way
git mv test.php test2.php
git add test2.php
git rm test.php

Show log

git log
(type q when you want to exit)

Show clearer log
git log --pretty=oneline

Show all commits by you
git log --pretty=oneline --author=“Your Name”

Best usage
git hist
(q to exit)

Go to branch
git checkout branchname

Reverting code

reset a file to the last commit
git checkout test.php

reset all code to last commit
git reset —-hard
(warning: this with make your current changes unrecoverable)

Clear any files not in git

git ls-files --others --directory | xargs -l1 -i{} rm -rf "{}"

Tag

show tags
git tag

Add new tag
git tag 0.0.1.0

I use this tagging reference which we increment
major.minor.build.revision
rarely we increment the revision point

Delete tag
git tag -d 0.0.2.0

Go to tag version
git checkout 0.0.1.0

Undo last commit
git revert HEAD
(will show the original commit and revert commit in history)

Revert to tag
git reset —-hard 0.0.1.0

Revert to hash commit
git reset —-hard

Remove all files that are not on current head
git clean -f -d -x

Show commit message from commit hash
git cat-file -p

create branches
git checkout -b staging

switch between branches
git checkout master

show local branches
git branch

show all branches
git branch -a

merge branches
git merge staging
Will merge defined branch into current branch

track branches
git branch —track staging origin/staging

Conflicts

Show conflicts
git diff --name-only --diff-filter=U

If a conflict occurs when merging, then edit those files resolving those conflicts then commit those changes

Rebase
checkout a older version of the code
git rebase master

This is like merge but will make the chain of commits linear so easier to read
Best to not use this as a multi developer environment would become hard to manage.
Only use on short-lived local branches before merging into public repos.

Clone a repo
Git clone will copy all files and history to a new area
git clone originalfolder clonefolder

clone a bare repo
It will only get the .git directory
git clone —bare originalfolder

Show origin
git remote show origin

Fetch
git fetch
will fetch new commits from the remote repository, but it will not merge these commits into the local branches

git pull
will pull the latest commit versions and merge it into the local branch

git push
Will push all committed changes on all branches live

git push origin master
Will push the master branch only to the repo

git reset --soft HEAD~3
git commit -m 'new commit message'

Squash commits to 1 commit

Cherry Pick

Go to the support branch
git checkout support/release-1

See what is outstanding
git cherry -v HEAD master

Grep to get what you are looking for
git cherry -v HEAD master | grep TICKET-01

Check if commit exists in release
git branch --contains 33aa11a

Cherry pick out commits
git cherry-pick -x 33aa11a

If conflicts then correct and continue
git diff --name-only --diff-filter=U
git cherry-pick --continue

Stashing changes

Sometimes you will want to change branch during your work flow but not commit what you currently have completed.

git stash

View all stashed items

git stash list

Stash only some files

git stash -p

Remove last stash item

git stash drop

Hosting

Best to use a third party to host your git repos. Github or Bitbucket are commonly used

Host locally
git daemon --verbose --export-all --base-path=.

Good tutorial to practise git http://gitimmersion.com/index.html

Leave a Reply

Your email address will not be published. Required fields are marked *