4. Git Workflow

4.1. Initialize repository

$ git init
Initialized empty Git repository in /home/devops/.git/

$ git init --bare
Initialized empty Git repository in /home/devops/.git/
$ find .
./config
./objects
./objects/pack
./objects/info
./HEAD
./info
./info/exclude
./description
./hooks
./hooks/commit-msg.sample
./hooks/pre-rebase.sample
./hooks/pre-commit.sample
./hooks/applypatch-msg.sample
./hooks/fsmonitor-watchman.sample
./hooks/pre-receive.sample
./hooks/prepare-commit-msg.sample
./hooks/post-update.sample
./hooks/pre-merge-commit.sample
./hooks/pre-applypatch.sample
./hooks/pre-push.sample
./hooks/update.sample
./refs
./refs/heads
./refs/tags

4.2. Clone repository

  • Protocols:

    • local filesystem

    • ssh

    • http

    • https

  • Sometimes ssh port is blocked (especially in public wifi)

  • HTTP is unsafe

  • HTTPS is the most robust option

$ git clone URL [dest dir]
$ git clone file:///...
$ git clone ssh://...
$ git clone https://...
$ git clone --recursive URL
$ git clone https://github.com/AstroMatt/book-devops.git

4.3. Pull

$ git pull --rebase

4.4. Add

$ git add .

4.5. Move

$ git mv [source] [destination]

4.6. Remove

$ git rm
$ git rm -fr
$ git rm --cached

4.7. Status

$ git status

4.8. Commit

  • -a, --all - commit all changed files

  • --amend - amend previous commit

  • -C, --reuse-message <commit> - reuse message from specified commit

  • --reset-author - the commit is authored by me now (used with -C/-c/--amend)

  • -S, --gpg-sign[=<key-id>] - GPG sign commit

$ git commit --all --message "ID-10 fix, now working"
$ git commit -a -m "ID-10 fix, now working"
$ git commit -am "ID-10 fix, now working"

4.9. Revert

  • Undo committed file

$ git reset --soft HEAD^
$ git reset HEAD <file-path>
$ git commit -c ORIG_HEAD

4.10. Push

$ git push

4.11. Further Reading

4.12. Assignments

4.12.1. Clone, Push and Pull

  1. Clone repository via ssh

  2. Add file lastname_firstname.txt

  3. Edit .git/config and add following [user] section

    [user]
        name = Your Name
        email = your.email@example.com
    
  4. Commit using git commit

  5. Push commit git push

4.12.2. Advanced Options

  1. Set branch permissions

  2. Make pull request

  3. Squash and merge pull request