Git Cheat Sheet
I have been working with Git for few months and even if at the start was kind of difficult to grasp it after switching from SVN.
Here is a list of most used commands :
Configuring
Cloning the repository
> git clone git://url
> git config --global user.name “Firstname Lastname”
> git config --global user.email “user@email.com”
> git config --global color.ui true
> git config --global alias.st "status" - define an alias to git status : > git st
List the configuration
> git config --list - list all the configuration
Setup a project in the root directory
> git init
Look inside the git file
> tree .git
Add all files to repository
> git add .
Commit files to the repository
> git commit -m “First import”
Commit all changes that have done to the project ( ask for a message)
> git commit -a
Add a single file to the commit (is qued up for commit)
> git add {filename} - add the file
> git rm {filename} - delete the file
> git mv {first_file} {second_file} - rename
See prev change ( DIFF between changes)
git diff {filename} - see the difference between changes
get information about the tracked files , changed or new files
> git status
See log
> git log
> git log --until=2012-06-14
> git log --since=2012-05-12
> git log --author=”James”
> git log --grep=”Init”
> git log --online
HEAD - last state of the repository , what was last checked out
git show {sha} - show the commit information
git diff {sha} - show a diff from a previous commit
git diff {sha1}..{sha2} show a diff between 2 commits
git diff {sha1}..{sha2} {filename} - show a diff between 2 commits on a file
git diff --stat --summary
git log --graph --oneline --decorate --all
Branches
- master is the main branch ( ~ trunk in the svn)
Branch listing
> git branch
Create a new branch
> git branch human => (create the “human” branch )
To switch to the human branch we need to log out from the master branch
> git checkout human ( swich to the human branch )
If I want to get the master changes in the human branch
> git rebase master
Merge human to master
First you want to see the changes
1) git checkout master
2) git diff master .. new_feature - compare 2 branches
3) git merge new_feature - merge the branch with the name new_feature ( I am in the master branch)
To check if the branch is merged you can do a diff between branches git diff master..new_feature
If we want to go back before the merge
4) git reset --hard ORIG_HEAD
If we have conflicts on merge make changes and then commit
> git branch --merged - show which branches are merged
> git branch -m {old_branch_name} {new_branch_name}- rename the branch
> git branch -d {branch_name} - delete a branch
> git push origin :{branch_name} - delete a remote branch
> git push origin --delete {branch_name} - also deletes a remote branch
Repositories
When you work on the public server you need to push the commit to live server.
> git push
Donwload the last changes
> git pull
Git Architecture:
2-tree architecture - repository & working copy (svn use)
3-tree architecture - repository , stagging index & working copy (git use)
working copy -> git add file.txt -> staging index -> git commit file.txt -> repository
Undone changes
- undo changes to the working directory
git checkout {filename} or
git checkout -- {filename}
- unde changes to the staging directory
git reset HEAD {filename}
- change the commit message or add new change to a file that is already on staging directory
git commit --amend -m “Change the files already staged”
- changes to the older commits
git checkout {hash} -- {filename}
- revert a commit
git revert {hash}
- rever multiple commits ( move the HEAD pointer)
git reset
-- soft (move the pointer but doesn’t change the staging index or working directory)
-- mixed (default) = move the head pointer to the specified commit and change the staging index but doesn’t change the working directory
-- hard - move the HEAD , change the staging index and working directory
git clean -f - remove the untracked files
Stashing
- store changes temporary , only visible for you
git stash save “config changes” - save the files to stash
git stash list - show the stash lists with the name of the changes
git stash show stash@{0}- show the file changes in specified stash (to see more add -p option)
git stash pop {stash_item} - get and remove changes from stash , if a stash item is not specified get the last one
git stash apply {stash_item} - get the changes from the stash
git stash drop stash@{0} - delete a stash item
git stash clear - remove all stash items
Ignoring files
/proect_dir/.gitignore - add all the files and directories that will be ignored by git.
Sample file:
# comment
test.txt - ignore file test.txt
*.tmp - ignore all the files that have the extension .tmp
*.gz
log/*.log
assets/photoshop/
assets/video
!assets/video/tour_*.mp4
If you want to ignore specific files you need to alter .gitignore_global file located in the user directory
If you want to track an empty directory , create a empty file .gitkeep in that specific directory