Skip to content

Guide to Git

CLI

Getting Started:

Initialise a repo:

  1. Navigate to dir e.g. ./repos/things
  2. git init (message shown: Initialized empty Git repository in c:/repos/things/.git/)

  3. ls -a to view files

  4. git status to show status of repo

Commiting a change:

  1. git add file.txt to stage file (can use git stage but less common)
  2. git commit -m "commit message" (-m flag to add message)

  3. git log to see commit history

  4. gitk to 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 .tmp files in temp directory
  • files usually ommited include binary files (.class, .jar, .exe) temp, local and sensitive files

Adding tags:

  1. git tag tagcontent

  2. view tags with git tag

View differences:

  • git diff show uncommited changes since last commit
  • git 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.0
  • git checkout 0023cdd: loads the state as at commit with the hash 0023cdd
  • git 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