Basic knowledge
- To move around with the use of the terminal a few commands are necessary
cd // Moves you to the home directory
cd .. // Moves you to the one directory out (Parent directory)
cd <DIRECTORY NAME> // Moves you into directory
ls // Lists the directory contents
mkdir <DIRECTORY NAME> // Creates a directory (folder) at current location
Basic usage
- Initializing a git repository
git init
- Cloning an existing repository
Most common is to clone a remote repository (From the interwebz)git clone <REPOSITORY>
- Pulling all new changes from the remote
git pull
- Displaying git status
git status // Gives you all the information about current git status
- Staging files
git add <PATH> // Stages a single file or directory git add . // Adds all changed files and untracked in all sub-directories git add -a // Adds all tracked files that are changed
- Committing files
git commit git commit -m "MESSAGE" // Quick committing with short message
- Pushing all committed changes to the remote
git push git push -f // Force pushes the commits. Allows for altering history (CARE)
git reset // Unstages all files git reset --hard // Resets all files back to HEAD git reset HEAD~<NUMBER> --hard // Resets ALL files and commits back <NUMBER> commits git reset HEAD~<NUMBER> // Moves head back <NUMBER> commits, but maintains files changed
- Help
man git // Shows the manual entries of git git <ANY COMMAND> -h // Shows the help entry for any given command git help everyday // Shows a short list of everyday git commands
More advanced usage
- Branches
git branch // Lists local branches git branch -a // Lists local branches & remote tracked branches git branch -d // Deletes a fully merged branch git branch -D <BRANCH NAME> // Deletes branch even if it is not merged git branch <BRANCH NAME> // Creates a new local branch git checkout <BRANCH NAME> // Checks out a branch (Switches to branch)
- Stashing
- Stashing is used to save changes that you would like to keep “in memory” but not commit. You can stash changes made, and reapply them at any later time. Very useful if you’ve made some messy changes, and you would like to switch branches, or work on something else.
-
git stash // Stashes all changed files git stash list // Lists all the stashes git stash show // Shows changes within stashes (*) git stash apply // Applies the stashed changes (*)
- (*) Stashes are saved separate, and can be shown/applied separate by appending a
stash@{NUMBER}
onto those commands.NUMBER
can be found in with thelist
command
Commands coming soon™
git clean
git merge
git rebase
git fetch
Personal favourites
git commit -a --amend --no-edit // Adds and amends changes to the last commit
git checkout -b <branch> // Shortcut for creating and checking out a new branch
git blame <FILENAME> // Shows file with log of who committed which line
Pretty git log?
Do you want that pretty pretty log instead of the bulky old one?
It looks like this (With slightly more color)
* 0575e8f - Testing theme (4 days ago) <Jon Johansen>
* 9770331 - Init github pages (7 days ago) <Jon Johansen>
|\
| * 01442b9 - Set theme jekyll-theme-hacker (7 days ago) <Jon Johansen>
* | e697e75 - Init gitignore and github pages (7 days ago) <Jon Johansen>
|/
* 6523eae - Init / Add readme (7 days ago) <Jon Johansen>
- To get the log you have to create an alias.
- To create the alias, simply copy paste this into your terminal:
-
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --"
- And to use it with your newly created alias
git lg //git lg is a custom alias you can create for log
Source: Pretty git Log
Other great resources
- Adding an existing project to GitHub using the command line
- Github For The Rest Of Us (Video)
- How to undo (almost) anything with Git
- Learn Git Branching
- Oh, shit, git!
- A successful Git branching model
- gitignore.io
Anything missing?
Feel free to create a pull request.
Written & managed by jonjohansen for Tromsøstudentenes Dataforening
- With contributions from: