Table of Contents

Git

Concept

Below is a simplified example of a workflow.

A working tree is created by initializing a directory (git init) or by cloning an existing repository (git clone "path/to/repository"). Work is then done with the files in the working tree to create a feature or resolve a bug.

At a significant point in the work, add your changes to the index (git add <file name>). Once all of the changes to create the feature or resolve the bug are added to the index, the changes are committed to the repository (git commit -m "commit message").

flowchart LR
A(git) -->|make changes| B(git add) -->|make changes| C(git add) -->|resolved bug| F[git commit]

Workflow

Generally, the workflow follows these steps:

  1. Get the latest copy of main/master:

    git pull
    
  2. Create and switch to a feature branch:

    git branch <branch-name>
    git switch <branch-name>
    
  3. Make changes, stage, and commit:

    git add .
    git commit -m "<message describing change>"
    
  4. Push changes to the remote branch (create it if needed):

    git push
    # or
    git push --set-upstream origin <branch-name>
    
  5. Create a Pull Request in GitHub or Azure DevOps. Approval will merge the changes into main/master.

gitGraph
    commit
    commit
    branch ISSUE496
    checkout ISSUE496
    commit
    commit
    checkout main
    merge ISSUE496
    commit
    commit

Snippets

Alias

git config --global alias.stash 'stash --all'
git config --global alias.bb '!script.sh'

Logs

git log --oneline
git log -S files -p

Diff

git diff
git diff --word-diff

Commits

git commit -m "Commit message"
git commit -a -m "Commit message"
git commit --amend

Creating commits signed by GPG:

git config gpg.format ssh
git config user.signingkey ~/.ssh/key.pub

Maintenance

git maintenance start

Recover lost commit

git reflog  # copy the commit hash before the action that deleted it
git branch <new-branch-name> <commit-hash>

Recover lost branch

git reflog  # copy the commit hash before the action that deleted it
git branch <branch-name> <commit-hash>

Workflow Summary

git clone <repo>
git branch <branch-name>
git switch <branch-name>
# make changes
git add .
git commit -m "<commit message>"
git push --set-upstream origin <branch-name>
git switch main
git merge <branch-name>

References

Notes

Workflow path and getting out of trouble

Typical feature branch workflow:

git branch feature > git switch feature > (make changes) > git add . > git commit -m 'commit message' > git push > git switch main > git merge feature

If you have a merge conflict:

# Edit the conflicted file(s)
git add .
git commit -m 'resolve merge conflict'

If you get a divergent branch error:

git switch feature
git rebase main
git switch main
git merge feature