Git Cheatsheet
Contents
Git Keywords
- HEAD - Dynamic pointer to the latest comment of a branch
- Tracking
- Stage
- Commit
- Fast forward - move the pointer / vs recursive
Git Basic
Basic Workflow
git add ...
git commit -m "your message here"
Check Git status
$ git status
Clone a repository
$ git clone <repoURL>
Create a new repository
$ git init
$ git init <repoName>
Stage files for the next commit
$ git add <file1> <file2> <...>
$ git add .
Commit staged files
$ git commit -m <message>
$ git commit -a -m <message>
# Add and commit all modified$ git commit --amend
View all commits
$ git log
Git Branches
Branches could be considered as pointers to commits
Checkout a branch
$ git checkout <branch>
Checkout into a new branch
$ git checkout -b <branch>
Same as git branch <branch>
, then git checkout <branch>
Create a new branch but don't checkout
$ git branch <branch>
Delte a branch
$ git branch -d <branch>
Git Merging
From the branch you want merge intogit merge <branch>
Resolving commits
1)cd {master}
git merge <branch>
# Creates conflict file
<Manually modify>git commit
2)cd {branch}
git merge <master>
Remote
$ git remote add <remoteName> <remoteURL>
$ git push <remoteName> <branch>
Other Stuff
Merge unrelated respositories
Useful for me to merge the private lab repositories from this course to my own repo.
At the end of each lab…
1 2 3 4 | # cd <lab_folder> mkdir Labs/lab<n> -p git mv !(Labs) Labs/lab<n> git commit -a -m "Move lab into correct repo folder" |
1 2 3 4 5 | # cd <UNSW-COMP1531> git remote add lab<n> <lab_folder> git fetch lab<n> git merge -S --allow-unrelated-histories lab<n>/master git remote rm lab<n> |
Set up your SSH Keypair
$ ssh-keygen -t rsa -b 4096 -C "<comment>"
Example