Git
Getting Started
Tell git who you are first; the name and email are recorded in every commit you make.$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
Start a new repository in the current directory, or clone an existing one.$ git init
$ git clone [url]
Everyday Workflow
Changes move in two steps: add puts them in the staging area, commit turns the staged changes into a permanent snapshot. status shows where everything stands.$ git status
$ git add [file]
$ git add .
$ git commit -m "describe the change"
Stage interactively, hunk by hunk: ideal for committing only part of your changes.$ git add -p
Exchange commits with the remote repository. pull is fetch (download) plus merge in one step.$ git push
$ git pull
Branches
Branches are cheap pointers to commits. switch changes branches, -c creates one; checkout is the older command that does both jobs.$ git branch
$ git branch -a
$ git switch [branchName]
$ git switch -c [newBranch]
$ git checkout -b [newBranch]
Merge another branch into the current one.$ git merge [branchName]
Delete branches locally and on the remote. -d refuses to delete unmerged work; -D forces it.$ git branch -d [branchName]
$ git branch -D [branchName]
$ git push origin --delete [branchName]
Inspecting History
$ git log
$ git log --oneline --graph --all
$ git show [commitHash]
$ git blame [fileName]
diff alone shows unstaged changes; --staged shows what the next commit will contain.$ git diff
$ git diff --staged
$ git diff [branch1]..[branch2]
Undoing Things
Discard uncommitted changes to a file, or unstage it without losing the changes.$ git restore [fileName]
$ git restore --staged [fileName]
Fix the most recent commit (message or forgotten files), or undo a commit safely by creating a new commit that reverses it.$ git commit --amend
$ git revert HEAD
reset moves the branch pointer back. --soft keeps your changes staged, --hard throws them away.$ git reset --soft HEAD~1
$ git reset --hard HEAD~1
revert is safe on shared branches because it only adds a commit. reset --hard and --amend rewrite history: avoid them on commits you have already pushed.Stop tracking files that are now in .gitignore but were committed earlier.$ git rm -r --cached .
$ git add .
$ git commit -m "remove ignored files"
Stashing
Put uncommitted work aside to get a clean working directory, then bring it back later. -u includes untracked files.$ git stash -u
$ git stash list
$ git stash pop
$ git stash apply
pop applies the stash and removes it from the list; apply keeps it around, useful when applying the same work to several branches.Tags
Tags mark specific commits, typically releases. Annotated tags (-a) store author, date, and a message.$ git tag [tagName]
$ git tag -a v1.0 -m "release 1.0"
$ git tag -d [tagName]
$ git push --tags
Tags are not pushed by default. Push them explicitly with --tags or git push origin [tagName].Remotes
A remote is a named URL of another copy of the repository; origin is the conventional name for the main one.$ git remote -v
$ git remote add [remoteName] [remoteURL]
$ git fetch [remoteName]
$ git pull [remoteName] [branchName]
push -u links the local branch to the remote one, so future git push and git pull work without arguments.$ git push -u [remoteName] [branchName]
Rewriting History
rebase replays your commits on top of another branch for a linear history; cherry-pick copies a single commit onto the current branch.$ git rebase [branchName]
$ git rebase -i HEAD~3
$ git cherry-pick [commitHash]
Interactive rebase (-i) lets you reorder, squash, and reword commits. As with reset, never rebase commits that others may already have pulled.