Guide to Git
CLI
Getting Started:
Initialise a repo:
- Navigate to dir e.g.
./repos/things -
git init(message shown:Initialized empty Git repository in c:/repos/things/.git/) -
ls -ato view files git statusto show status of repo
Commiting a change:
git add file.txtto stage file (can usegit stagebut less common)-
git commit -m "commit message"(-mflag to add message) -
git logto see commit history gitkto see revision graph
Undo/Delete commit:
- undo commit but keep at staging area (after
git add):$ git reset --soft HEAD~1 - undo and remove from staging (before
git add):$ git reset --mixed HEAD~1 - completely delete commit (discards any changes):
$ git reset --hard HEAD~1 - change 1 to any n number of commits
Ignoring file:
1. Create file .gitignore in working/root directory
2. Add file name to .gitignore file
- if e.g.
temp/*.tmp, git wil ignore all.tmpfiles intempdirectory - files usually ommited include binary files (
.class,.jar,.exe) temp, local and sensitive files
Adding tags:
-
git tag tagcontent -
view tags with
git tag
View differences:
git diffshow uncommited changes since last commitgit diff hash1 hash2show between the 2 commits (use first 7-10 char of each commit hash)git diff tag..HEADshow changes from tagged commit to most recent
Retreive specific revision
Use the checkout <commit-identifier> command to change the working directory to the state it was in at a specific past commit.
git checkout v1.0: loads the state as at commit tagged v1.0git checkout 0023cdd: loads the state as at commit with the hash 0023cddgit checkout HEAD~2: loads the state that is 2 commits behind the most recent commit For now, you can ignore the warning about ‘detached HEAD’.
If you checkout a commit that comes before the commit in which you added the .gitignore file, Git will now show ignored files as ‘unstaged modifications’ because at that point Git hasn’t been told to ignore those files.
Pushing to github:
git push origin master- type username and password
Push specific tags: git push origin tagname
Push all tags: git push origin --tags
Branching:
Create: git branch branchname
Switch to branch: git checkout branchname or git switch branchname
1 step process: git switch/checkout -c branchname
merge branch: git merge master
no fast-forward: git merge --no-ff add-countries