You are on page 1of 3

Git help: Use git help <thing>

Clone an existing repository with "git clone URL [name_of_directory]"


Include the [name_of_directory] if you want to call the directory something othe
r than the name it has.
When you clone a repository, everything will be tracked and unmodified.
There are different types of transfer protocols. https://, git://, user@server:p
ath/to/repo.git (SSH)
Git status is very verbose, you can add -s at the end to shorten the output
Sometimes you don't want git to automatically add/show you untracked files.
You can create a file listing patterns to match them named .gitignore
IE echo "*.class" >> .gitignore
Or a specific directory: DIRECTORY/
But say you want KEEPME.class, just put a "!" before it IE !KEEPME.class
Comments = #
Git status tells you about which files were modified, but if you want to see exa
ctly
what was modified, you can run "git diff"
If you only want to compare your previous commit to those that are staged, use "
git diff --staged"
Pass -v to git commit to see a bit more info about the changes before you commit
NOTE: "git diff" by itself will not display any changes of staged files.
You can skip the staging area by passing in -a to git commit
This will automatically stage every file that is already tracked before doing th
e commit.
You can also stage all files being tracked but NOT commit yet by using -u instea
d of -a
Removing Files
To remove a file from Git, you must un-track it first by removing it from your
staging area and --THEN-- commit.
git rm does that and ALSO removes the file from your working directory.
NOTE: If you simply remove a file, it will show up in the un-staged area.
If you want to keep a file in your working tree but remove it from your staging
area,
like when you forget to add a .gitignore file that ignored .class, use "git rm -cached FILE"
NOTE: to remove a bunch of *.blah files, use "git rm \*.blah"
IE to remove Vim backups, run git rm \*~
Moving/Renaming files
git mv FILE_FROM FILE_TO
Commit history
git log
append -p to see difference in commit. Basically it runs diff follwoing each ent
ry.
You can also check changes in specific files IE git log -p awesome.java

append --stat to see a more condensed version of -p


append --graph to see branch and merge history
append -S NAME to show commits adding/removing code matching the string
append -NUMBER to see last NUMBER commits
or append --since=2.weeks for last two weeks
or --after In both cases you can use "2008-01-15"... WAIT THIS IS BRITBONG FORMA
T
Undoing things
If you commit to early and forget to add some files, you can run commit with --a
mend
git commit --amend
This command uses your staging area for the commit.
ex:
git commit -m 'initial commit'
git add forgotten.file
git commit --amend
This also allows you to change the message, so if you forgot to add it this is t
he chance.
This will end with a SINGLE commit - the second commit replaces the result of th
e first
Retrieving a Modified file
git checkout -- file.file
NOTE: This undoes any changes you made to the file. You just copied a file over
another file.
If you want to keep changes to that file but want it out of the way for now, bra
nch it instead
Key: Anything that is COMMITED in Git can almost always be recovered
Remote Repositories
These are versions of your projects hosted on the internet or network somewhere.
git remote will display the remote servers you have configured.
In a clone repository, you can see the origin. add -v to see the URLs for origin
(the shortname)
Adding Remote Repositories
git remote add [short-name] [url]
You can fetch either by the short-name or URL after you've added a remote server
IE git fetch origin
This will fetch any new work that has been pushed to the server since you last c
loned (fetched).
NOTE: This pulls the data to your local repository - it doesn't automatically me
rge it with
any of your work or modify what you're currently working on. You have to m
anually merge it
when you're ready. If you do want it to merge, use pull.
NOTE:The only difference between git pull and git fetch is that : git pull pulls
from a remote branch AND merges it.
A fetch only brings the files in, but no merging is done. IE the branch is
modified w/ a pull, but fetch leaves it alone.
Pushing your remote
git push [remote-name] [branch-name]
git push origin master

If you want more info on a remote,


git remote show [remote-name]
This also lists info about merges using pull and pushing info for different bran
ches
Tagging
This is useful in things like marking release points (v1.0)
git tag will list all tags.
git tag -a v1.0 -m 'the version number thing'
now to view that commit, use git show TAGNAME
Light tagging
git tag v1.0-lw
running git show for this tag displays less info
To tag something in a previous commit, find the checksum
git tag -a v1.WhoCares CHECKSUM
NOTE: By default, git push doesn't transfer tags to remote servers. You have to
specify it.
git push origin TAGNAME
For pushing ALL previous commit tags, git push origin --tags
Aliases for git commands:
git config --global alias.co commit
shortens commit to co
git config --global alias.unstage 'reset HEAD --'
To run a external command, insert a '!' IE '!echo hello'

You might also like