Skip to content

Instantly share code, notes, and snippets.

@oilsinwater
Last active February 5, 2018 03:09
Show Gist options
  • Save oilsinwater/6b5bbafb1e08e5974ecdec665a60ce74 to your computer and use it in GitHub Desktop.
Save oilsinwater/6b5bbafb1e08e5974ecdec665a60ce74 to your computer and use it in GitHub Desktop.
Git Immersion Notes
## Setup name and email
git config --global user.name "..."
git config --global user.email "..."
## Setup line ending preferences, for mac
git config --global core.autocrlf input
git config --global core.safecrlf true
## Set default text editor
git config --global core.editor "..."
## check settings
git config --list
## Create the repository
git init
## stages file to add to datbase after commit
git add "..."
## stages all the files to add to the database
git add .
## take a snapshot with message
git commit -m "..."
## Get the status of the repo
git status
## Separate staging supposing you have 4 files a-d
git add a.js
git add b.js
git commit -m "Changes for a and b"
git add c.js
git commit -m " Unrelated change to c "
## get a list of what changes have been made
git log
## one line histories
git log --pretty=oneline
## max of 2 one-line histories
git log --pretty=oneline --max-count=2
## list since 5 minutes ago
git log --pretty=oneline --since='5 minutes ago'
## list until 5 minutes ago
git log --pretty=oneline --until='5 minutes ago'
## list from a specific author
git log --pretty=oneline --author=<your name>
## list of all information
git log --pretty=oneline --all
git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
/** --pretty="..." defines the format of the output.
%h is the abbreviated hash of the commit
%d are any decorations on that commit (e.g. branch heads or tags)
%ad is the author date
%s is the comment
%an is the author name
--graph informs git to display the commit tree in an ASCII graph layout
--date=short keeps the date format nice and short
*/
## common aliases in .gitconfig
[alias]
co = checkout
ci = commit
st = status
br = branch
hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
type = cat-file -t
dump = cat-file -p
## see what tags are available
git tag
## tag commits with names for future reference
git tag v1
## notion below means 'the first ancestor of v1'
git checkout v1^
## view tags in the logs
git hist master --all
/** revert file changes in the working directory by
checking the file out after a mistake */
git checkout "file name"
## resets the staging area to whatever is in the head and clears
the staging area of any changes made, when used with previous cmd
is very useful
git reset HEAD "file name"
## undo a committed change using a commit to remove it,
which is shown in git log. Rest like this can be bad for shared remote repos
git revert HEAD
## shows even the removed commits
git hist --all
## delete a tag
git tag -d 'name'
## Amend the previous commit
git commit --amend -m "Add an author/email comment"
## move files with git command
git mv hello.rb lib
## stages file
git add lib/hello.rb
## removes file
git rm hello.rb
##search .git directory
ls -C .git
## /objects dir names are the first 2 letters of hash of the object stored in git
ls -C .git/objects
## inside /object dirs are files with 38-char names, these contain the objects stored in git
ls -C .git/objects/<dir>
## project specific configuration file. override .gitconfig file in home directory
cat .git/config
## each file corresponds to a tag created with git command, heads dir is similar but for branches
ls .git/refs
ls .git/refs/heads
ls .git/refs/tags
## HEAD file contans a reference to the current branch
cat .git/HEAD
## show the latest commit in the repository
git hist --max-count=1
## cat git type
git cat-file -t <hash>
## dump the object in the commit
git cat-file -p <hash>
## find the dir tree from treehash output of git cat-file -p
git cat-file -p <treehash>
## dump the directory from lib hash from previous command
git cat-file -p <libhash>
## dump the file contents using rbhash from prev cmd
git cat-file -p <rbhash>
## create a new branch
git checkout -b greet
## merge branches periodically you can pick up any changes to master and keep your changes in branches compatiable in mainline
git checkout <branch name>
git merge master
## rebasing: don't use rebase if the branch is public, since can screw up other's work; dont use when exact history of commit branch is important
## rebase is good for short-lived local branches
git checkout <branch>
git rebase master
## create a clone of a repository
git clone <dir> <cloned_dir>
## check the repos remote origin
git remote
git remote show origin
## Set a new remote origin
git remote add origin https://...
git push -u origin master
## Verify new remote
git remote-v
## Give remote a shorthand name
git remote add origin <shorthand> https://...
## list all branches with origins
git branch -a
## gets all commits from the original repo but not integrated into the clone repo's local branches, so files are unchanged
git fetch
## merges changes with origin repo, changes files integrating them in clone repo's local branches
git merge origin/master
## pull command is the equilvalent of using fetch then merge origin/master
git pull
## add a local branch that tracks a remote branch
git branch --track <branch> origin/<branch>
git branch -a
## create a bare repo
git clone --bare hello hello.git
## add hello.git repo to our original repo
git remote add shared ../hello.git
## push changes to shared repo
git push shared master
## pull down changes from shared repo
cd ../cloned_dir
git remote add shared ../hello.git (or cloned_dir.git)
git branch --track shared master
git pull shared master
## share git repos over the network, starting a git server
git daemon --verbose --export-all --base-path=.
(separate window)
git clone git://localhost/hello.git network_hello
cd network_hello
## no authentication necessary if you want to push
git daemon --enable=receive-pack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment