Git
Configuration
$ git config [<options>]
Options:
- Location
- --global
- --local
- --file <file>
- Action
- --list: list all current configuration settings
Useful settings:
$ git config --global core.editor "atom --wait"
$ git config --global user.name "Rick Sanchez"
$ git config --global user.email [email protected]
And aliases:
## converts
## $ git checkout other-branch
## to
## $ git co other-branch
$ git config --global alias.co checkout
$ git config --global alias.co branch
$ git config --global alias.ci commit
$ git config --global alias.st status
Add aliases as you see fit. They can include options like so:
$ git config --global alias.unstage 'reset HEAD --'
$ git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
Staging
Use git status to see which files are staged and which are not.
Stage files with git add. Wildcards are supported for file names:
$ git add file.txt
## adds all files in directory
$ git add .
## adds all files in directory with .txt extension
$ git add *.txt
Unstage files with git reset, essentially undoing anadd.
$ git reset file.txt
## will reset directory to pre-commit state, erasing any changes to files, even if they're saved to the
## to the machine
$ git reset --hard HEAD
## go back to a previous commit on the current branch
$ git reset <commit-hash>
You can save the changes to a file without having to commit them, allowing you to return to its uncommitted state, using git stash.
$ git stash
## use the pop command to unstash stashed files, so you can commit them
$ git stash pop
View changes to a file since the previous commit using git diff.
$ git diff file.txt
## use the staged option if the file has been staged
$ git diff --staged
Committing
$ git commit -m 'A message about the changes'
Options:
- -a: commit all changes to files in repo
- --amend: make edit to previous commit message, or add files to previous commit
- -p: interactively add changes. Allow you to commit partial sections of a file.
Branching
## list branches
$ git branch
## switch to branch
$ git checkout some-other-branch
## creates a new branch and switches to that branch
$ git checkout -b a-new-branch
## merge branch to current branch
$ git merge some-other-branch
When merging with master, you likely want to use git rebase. If anyone else has made changes to master, this will allow you to include those changes to your current branch before you merge back to master. Thus, you get a chance to resolve merge conflicts with those new changes locally and then make a clean merge with master.
$ git rebase master
Remotes
## view remote repos
$ git remote
## add a new remote
$ git remote add <remote-name> <remote-url>
## push to remote
$ git push <remote-name> <remote-branch>
## pull latest update from remote and merge it
$ git pull
## only retrieve the latest data
$ git fetch