3. Introduction in git¶
3.1. About git¶
- source code management tool
- stores your file in different versions
- it’s possible to sync your repo with a remote-repo over ssh/https
- every checkout of a repo is a copy of the whole history
3.1.1. why git & Ansible?¶
- who changed what
- rollback to an other state
- history of your changes
- workflow for changes
3.2. git Basics¶
3.2.1. Repo / Checkout¶
# initialize a git repo
git init
# clone a repo
git clone git@git.confirm.ch:pstauffer/ansible-training.git
# checkout a branch / tag / commit
git checkout <branch/tag/commit>
3.2.2. file handling¶
# stage a file for the commit
git add <filename>
# add all files for the commit
git add *
# rename file
git mv <filename> <new-filename>
# delete a file
git rm <filename>
3.2.3. commit¶
# commit a change (after the ``git add``)
git commit -m 'commit message'
3.2.4. Differences / Status¶
# status
git status
# diff
git diff
# show history
git log
3.2.5. Remote Repo¶
# pull from remote repo
git pull
# push to remote repo
git push
# push a new branch
git push -u origin newfeature
# delete remote branch
git push origin :newfeature
3.2.6. Merge¶
# merge actual branch with the newfeature branch
git merge --no-ff newfeature
3.2.7. Archive¶
# export git repository
git archive --format zip --output repo.zip master
3.3. Branches / Tags¶
- use tags when you want a snapshot of the code as reference for the future
- when you develop a new feature, create a new branch
Hint
Create a master and develop branching model for your Ansible repository and create tags for stable releases.
3.3.1. Branches¶
# show all branches (incl. remote)
git branch -a
# checkout a branch
git checkout <branch-name>
# create a new branch
git branch -b <branch-name>
# delete a local branch
git branch -d newfeature
3.3.2. Tags¶
# show all tags (incl. remote)
git tag -a
# create a new tag
git tag -a v1.0 -m 'version 1.0 message'
# remote push with tags
git push --tags
Hint
Use tags to create a snapshot of the code.