This is a reference guide for the git commands that we've gone over in lecture and that you will need for the lab.
- Create a repository in the current folder
git init
- Tell git to track a new file
git add <filename(s)>
# OR to simply add all the files in the current working directory:
git add .
- Commit changes in staging area
git commit -m "<message here>"
# ex.
git commit -m "adds initial java files for lab 8"
- Looking at old commits
git show <commit hash>
# ex.
git show 68a3056
- Looking at files in an old commit
git show <commit hash, branch, or tag>:<file_name>
# ex.
git show master:Example.java
git show 68a3056:Example2.java
- Undoing an old commit
git revert <commit hash>
# ex.
git revert 68a3056
- Check status of repository
git status
- Check log of recent commits
git log
# OR, for a simpler view
git log pretty=oneline
- To remove a file from the staging area
This will unstage the given file, meaning when you do
git commit
, the changes to this file will NOT be included
You will only ever need to do this if you accidentally
add
a file
git rm --cached <filename>
- To view all the remotes connected to the repository
git remote -v
- To clone a repository
git clone <repository URL>
- To pull from a remote repository
git pull <remote> <branch>
# Example:
git pull origin master
- To push to a remote repository
git push <remote> <branch>
# Example:
git push origin master
- To view branches
git branch
- To create a new branch
git branch <branch name>
# Or to create and switch to a new branch
git checkout -b <branch name>
- To switch branches
git checkout <branch name>
- To merge a branch into the current branch
git merge <branch name>
If something goes wrong, use git status
and git log
to see if you can figure out what happened.
Remember, don't panic, as long as you committed something, its virtually impossible to lose it
For branching issues, try git log --graph --full-history --pretty=format:"%h%x09%d%x20%s"
Since we should all have color set up we can try git log --graph --full-history --all --color --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
for an even prettier view