Git resources
- Primer
- A Visual Git Reference
- Mermaid live editor
- Setup
- Exercises
This is a collection of online resources for those learning Git.
Setup
- Remote Git server (e.g. GitHub, Bitbucket)
- Account with (shared) username
- Local install
- Git config with own name
Running git config
sets up your local git client with stateful data, which in turn is applied to each commit.
git config --global user.name 'Lightenna training'
git config --global user.email 'lighttrain@users.noreply.github.com'
What is git?
- Distributed version control system
- The ‘master’ copy can be anywhere
- Conventionally, we adopt a single remote
- All contributions pushed to that remote
- Demonstrate with an example
File tree
/home/myuser/devops-workstream/
kubernetes
service.yaml
deployment.yaml
terraform
vagrant
README.md
Git helps us manage sets of files.
This example set is taken from the open-source Devops-Workstream repo.
Changes
/home/myuser/devops-workstream/
kubernetes
service.yaml
deployment.yaml
terraform
vagrant
- README.md
When we change a file, we need everyone else to see those changes.
We could simple copy the files around, but that risks overwriting others’ changes.
Git provides a mechanism for us to contribute those changes, while managing the risk of overwriting other changes.
Past: CVS/Subversion
svn checkout
- get a working copysvn update
- get latest changessvn commit
- push my changes
Git pull (simplified)
git clone
- create local repo and working copygit pull
- update repo and working copy
Git pull
git clone
- create local repo and working copygit pull
- update repo and working copygit fetch
- update local repo onlygit checkout
- update working copy only
Local repo stored in .git
/home/myuser/devops-workstream/
- .git
kubernetes
service.yaml
deployment.yaml
terraform
vagrant
README.md
Pull process
git pull
- Combines
fetch
andcheckout
- Fetch from remote
- Update local repo
- Update working copy
- Combines
Git push (simplified)
git commit .
- stage and commit to localgit push
- push local to remote
Git push (including stage)
git add
- stage file for commitgit commit
- commit to localgit push
- push local to remote
Commit and undo (Visual Git Reference)
Push process
git commit .
- Combines
git add
andgit commit
- Stage for commit
- Commit to local repo
- Combines
git push
* Push local repo to remote
Exercise: clone remote
- Clone remote repo locally (
git clone <repo_address>
) - Change README.md, edit a random line
- Stage and commit (
git commit . -m "message"
) - Push (
git push
) - Check online
Create a local branch
- Done from a source branch
git checkout -b newbranch
Push to remote
- So far only local
git push origin newbranch
git push --set-upstream origin newbranch
Merge back
- Make changes to new branch
git checkout newbranch
- Edit, commit (
git commit .
) git checkout master
git merge newbranch
Branches as chains of commits
Exercise: create repo
- Create remote repo
- Clone locally
- Copy files into working directory
- Stage
- Commit
- Push
- Check online
Rollback
Exercise: undo a bad commit
- Clone locally (already done)
- Mess up a file
- Stage and commit
- View history
- Checkout a previous commit
- Move HEAD back to previous commit
- Make rational change
- Stage and commit again
- Push
- Check history online
Exercise: dump local, fetch remote
- Clone locally (already done)
- Mess up a file
- Stage and commit
- View history
- Fetch remote
- Reset local
- View history again
Exercise: merge feature branch
- Clone locally (already done)
- Create branch
- Checkout branch
- Make changes
- Stage and commit
- Push (optional)
- Switch to (checkout) master
- Merge
- Push
Exercise: tag a branch for release
- Clone locally (already done)
- Checkout master
- Check latest commit ID
- Create tag
- Update files
- Stage and commit
- Checkout tag
Exercise: group collaboration
- Clone shared repo
- Assign ticket numbers
- Create feature branch
- Add unique file
- Stage and commit
- Switch to (checkout) master
- Merge
- Push
Leave a comment