git
Version control system.
Tips
Use fetch
and merge
instead of pull
(see).
Commands
git commit --amend
edit message of most recent commit.git branch -m new-name
: Rename branch.git push origin :old-name new-name
: Delete the old-name remote branch and push the new-name local branch.git diff develop master -- . ':(exclude)**/*.test.js'
: Non-test changes between branches.git reset HEAD~1
: Undo most recent commit.git add -A
: Stage all files (same as-u
but also adds untracked files).git add -u
: Stage all tracked files (does allgit add
andgit rm
commands).git diff branch_1..branch_2
: Compare branches.git blame [file]
: Show what revision and author last modified each line of a file.git checkout -
: Checkout previous branch.git checkout [commit-id] [filename]
: Restore specific file from specific commit.git clone
: Get a copy from the upstream branch to prime your local repository.git commit -m "message"
to advance the current branch.git diff [filename]
: To see exactly what was modified.git fetch
: Update local cache of remote branch.git log -- [filename]
: Show commit history of a file.git log -p [filename]
: Show change history of a file (i.e. code changes).git log
: Show a log of all commits starting from HEAD back to the initial commit.git mv [filename1] [filename2]
: Rename a file in the repository.git reflog show
: List all local commits.git reset --hard [commit]
: Roll branch back to previous commit permanently erasing all newer commits.git reset HEAD [file]
: Unstage a Staged file.git revert HEAD
: Remove the most recent commit.git rm -f [filename]
: Remove file from the repository.git stash drop
: Delete most recent stashed changes.git stash pop
: Apply stashed changes and remove them from stash.git stash
: Moves uncommitted changes to the stash.git status
: Show which files have changed between the current project state and HEAD.git mv [filename1] [filename2]
: Rename a file in the repository.
Life Cycle of a change
- Run git status.
- Possibly run git stash to hide uncommitted changes.
- Create Branch:
git branch <new_branch> <current_branch>
. - Switch to new branch:
git checkout <new_branch>
. - Make changes...
- Run
git status
. - Stage all changes to tracked files:
git add -u
(orgit add -A
to add new files also). - Run
git status
. - Commit changes:
git commit -m "[comment]"
. - Ensure current branch is up to date:
git fetch
. - Merge branches; At each stage resolve any conflicts:
- Switch to the parent branch:
git checkout [parent_branch]
. - Ensure branch is up to date:
git fetch
followed bygit merge origin
. - Switch to the new branch:
git checkout [new_branch]
. - Merge parent with new_branch:
git merge [parent_branch]
. Best to try merging this way first. - Switch to the parent branch:
git checkout [parent_branch]
. - Merge new_branch with parent_branch:
git merge [new_branch]
. - Publish your changes:
git push
.
- Switch to the parent branch:
- Delete new branch:
git branch -d [new_branch]
.
Setup
Use git init
: to create a new repository.
Add a .gitignore
file to the repositories root directory to exclude files from Git
(example).
Configuration file
~/.gitconfig
:
[user]
name = Paul Ahern
email = ahernp@ahernp.com
[merge]
tool = vimdiff
[core]
editor = vi
[alias]
co = checkout
ci = commit
st = status
br = branch
hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
[color]
ui = auto
[push]
default = simple
# Use ssh instead of https
[url "git@github.com:"]
insteadOf = https://github.com/