-
Notifications
You must be signed in to change notification settings - Fork 19
Git Cheatsheet
Clone a repository - i.e. Checkout and initalise a local copy of the remove repository located at "":
git clone <url>
Fetch latest from remote repository and merge with your repository:
git pull
Show your current git status - lists modified files, as well as other files that require adding and deletion:
git status
Add a newly created file to your local repository:
git add <path_to_file>
TIP: avoid the use of "git add ." a better approach is to git add each file as you create them, this ensures you only add the files you want to the repository
Remove file from your local repository:
git rm <path_to_file>
Commit changes to your local repository:
git commit -a -m "<commit_message>"
For visually merging conflicts within git, I use DiffMerge (it is available for a swag of platforms) to resolve conflicts. To set up git to use Diffmerge, you need to update your .gitconfig file with the following:
Mac OSX (.gitconfig is located in your user directory i.e. ~/.giconfig):
[merge]
tool = diffmerge # This will be the default merge tool invoked by git
[mergetool "diffmerge"]
cmd = "/Applications/DiffMerge.app/Contents/MacOS/DiffMerge" \
--nosplash \
--result="$PWD/$MERGED" \
"$PWD/$BASE" \
"$PWD/$LOCAL" \
"$PWD/$REMOTE"
keepBackup = false
trustExitCode = false
Windows (.gitconfig is located in your user directory i.e. C:\Documents and Settings<user_name>.gitconfig):
[merge]
tool = diffmerge # This will be the default merge tool invoked by git
[mergetool "diffmerge"]
cmd = "/c/Progra~1/SourceGear/DiffMerge/DiffMerge.exe" \
--nosplash \
--result="$PWD/$MERGED" \
"$PWD/$BASE" \
"$PWD/$LOCAL" \
"$PWD/$REMOTE"
keepBackup = false
trustExitCode = false
TIP: Use 'git bash' on the windows platform so that file paths are correctly interpreted by git.
Create a patch:
git format-patch -o <output_dir> origin
Push changes made to your local repository to the remote repository:
git push
Create remote git repository with group permissions
$ ssh myserver.com
Welcome to myserver.com!
$ mkdir /var/git/myapp.git
$ chgrp grpName /var/git/myapp.git
$ chmod 775 /var/git/myapp.git
$ chmod g+s /var/git/myapp.git
$ cd /var/git/myapp.git
$ git --bare init --shared=group
Initialized empty Git repository in /var/git/myapp.git
$ exit
Add the remote repository to your existing local git repo and push
$ cd ~/Sites/myapp
$ git remote add origin ssh://myserver.com/var/git/myapp.git
$ git push origin master
#Tags:
Create a tag on a remote repository:
git push origin origin:refs/heads/<remote_tag>
Delete a tag on a remote repository:
git push origin :refs/heads/<remote_tag>
#Branches
Branching procedure:
Create a remote branch Create a local branch that tracks it Work, Test, Commit (repeat) – this is all local Push (pushes commits to the remote repository) Create the remote branch
git push origin origin:refs/heads/new_feature_name
Make sure everything is up-to-date
git fetch origin
Then you can see that the branch is created.
git branch -r
Start tracking the new branch
git checkout --track -b new_feature_name origin/new_feature_name
Pushing changes to branch
git push origin/new_feature_name