List your current config:
git config --list
Set a config option:
git config <name> <value>
Pass the --global
option to set the config globally, not just in current git repo.
Initial setup:
git config --global user.name "<value>" # Your name
git config --global user.email "<value>" # Your email associated with github.com
git config --global core.editor "<value>" # I would use vim
Resetting is destructive. Reverting is non-destructive.
git pull
is performing two commands:git fetch
andgit merge FETCH_HEAD
add # adds files to staging, a '.' will match everything
-u # update flag, try this when git status shows deleted files
branch # alone, it lists all branches, with a value it will create a new branch
-m # rename a branch
-d # delete a branch
-D # force delete
checkout # switch branches
-b # create a new branch, and switch to it
commit # commit changes to repo
--amend # a small change that is not worthy of its own commit
--no-edit # don't edit the commit message when ammending a commit
config # sets git configs
--global # set a config option globally
core.fileMode [true|false] # set file permissions tracking
color.ui [true|false] # turns on/off colours
clean # removes untracked changes
-f # files
-d # directories
log # shows the commit history
--oneline # shows one line per commit (good for scoping out a quick sha1 value)
--stat # abbreviated stats
-p # full changelog
-n<number> # only shows last <number> of commits
reflog # log of ref updates (checkout, reset, commit, merge)
diff
<branch>..<other_branch> # diff two branches
--name-status # just show file names of diff files
--cached # show diff log of change in working directory
ls-files # shows non-git files
--ignored --exclude-standard --others # .gitignore'd files
--exclude-standard --others # untracked files
remote # lists remotes
-v # more detailed info on remotes
add <name> # add a new remote (tyically 'origin')
set-url <name> # change the url of a remote (if you've moved the repo)
rename <old> <new> # rename the name of a remote
rm <name> # remove a remote
update # grabs the remote's branches
reset # destructive method of changing HEAD
HEAD ~<n> # moves the HEAD back <n> commits
--hard # discards any changes
revert # reverts a commit, non-destructive
<sha1> # commit to revert
push # push changes to remote
-u <remote> --all # pushes up the repo and its refs for the first time
-u <remote> --tags # pushes up any tags
-u <remote> <branch> # creates a branch on the remote repo and sets the upstream
<remote> --delete <branch> # deletes a branch
pull # pulls changes from remote
rm # removes from the index
--cached <file|directory>
-r # recursive
## Tips
Woops I just commited, but there's an untracked file
Simply add it to the stage (git add .
) and use the --amend
flag while commiting again – git commit --amend
Woops I'm working in the wrong branch
Put the changes in to stash
and then apply that stash to the different branch.
git stash
git checkout <whatever>
git stash apply
Woops I just stashed something, then stashed again - how can I apply a previous stash?
You can get a list of all your stashes:
git stash list
From the list, you can use:
git stash apply stash@{1}
Or simply, twice:
git stash pop
Do I really have to write git push <remote> <branch>
each time?
No, you can set the upstream for each branch so it will automatically go to the right place.
git checkout a-branch
git push -u origin a-branch
# then in future you can just 'git push'
Add to staging and commit at the same time
Any currently tracked files can be added at commit time with the -a
flag.
git commit -am "Watch out for untracked files though, they wont be added"
Go to a particular commit
You can checkout to a specific commit you have made previously in a type of sandbox mode. Any changes you make here will be destroyed when you reattach the head. If you want to save the changes you must create a new branch.
git checkout <sha1>
Global .gitignore
If you have commonly ignored files across your projects, you can create a global .gitignore
file in your home (~/
) folder. An example would be:
# ~/.gitignore
DS_Store
This would mean that those pesky files that OS X creates for you in all your directories wont clutter your repo. Remember, you need to add this file in the global git config like so:
git config --global core.excludesfile ~/.gitignore