You should love git

 Do you like time travel? 
 You should love git! 

git basic, and most used commands

clone / init / pull / push / checkout
log / status / merge / reset

 git merge    git rebase 

Git Merge
- Simple and familiar.
- Preserves complete history and chronological order.
- Maintains the context of the branch.

Git Rebase
- Streamlines a potentially complex history.
- Avoids merge commit “noise” in busy repositories.
- Cleans intermediate commits by making them a single commit. (interactive)

 use git aliases 

git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status git config --global alias.sw switch git config --global alias.re restore git config --global alias.visual '!gitk' # Go to root of the repository alias cdg='cd $(git rev-parse --show-toplevel)' # Add everything and ammend last commit alias ameno='cdg && git add . && git ci --am --no-edit' # Push everything to origin in last commit alias anarchy='ameno && git push --force-with-lease'

 remotes / git is a distributed system 

# Add a new remote git remote add origin git@github.com:iamandrewluca/voluntar-web.git # Removes the remote with upstream name git remote remove upstream # Rename upstream remote in origin git remote rename upstream origin # List all remotes git remote -v

 DEMO 

 Conventional Commits 

196a829 refactor: replace zone title pipe with translate pipe 74313af refactor: replace special conditions constants with l10n bb74f05 feat(volunteers): refactor volunteers list (#194) d5a1336 feat: add Zone coords, deprecate old "enums" (#193) de5ff39 feat(demands): updated new demands form component (#188) ecab94e chore(deps): bump socket.io from 2.3.0 to 2.4.1 d201c37 feat(users): add user detail page ee56a19 fix(users): remove password from user edit when is empty ee11d45 chore: sync all languages when default was changed

 Merging Pull Requests 

                                 🚀  Create a merge commit 
                                 🚀  Squash and merge 
                                 🚀  Rebase and merge 
                                                         what to do with big PR's?

 git reflog the saviour 

 TIPS 

Working mostly in the main branch of the repository made some commits, and when pushing/pulling you get a lot of commits like this?
Merge remote-tracking branch 'origin/main' into main

This means someone also pushed into this branch on origin remote. Activate this config.
git config --global pull.rebase true

This will rebase instead of merging your local commits with commits that are already on origin

 TIPS 

Rerere is an abbreviation for "reuse recorded resolution". rerere is a feature that will memorize all resolved conflicts, and resolve them automatically if happens to encounter them in the future.

https://www.git-scm.com/book/en/v2/Git-Tools-Rerere

Enable the feature

git config --global rerere.enabled true
git config --global rerere.autoupdate true

Now when you sync your feature branch with main using merge, you resolve any conflicts you encounter, how many times you want (this works well with long living branches). In the end you see your feature branch history with all merge commits. You run once, git rebase main and git will stop again at each commit that had conflicts, but the commit is already resolved, you just run git rebase --continue . When rebase is finished, poof, no merge commits, linear history.

 TIPS 

In the last version of git, checkout was split in 2 new commands, switch and restore , this was done because checkout had too much responsibility

git switch -c some-new-branch-name
git switch main
git restore --source main path/to/some/file

 TIPS 

Git minus operator (couldn't find any official docs)

# We are on long branch named feature/ABC-123-long-branch-name
git switch main
git pull
git switch -
git rebase -

In this context - will be the previous branch

 TIPS 

GitHub offers a big list of git ignore files for different occasions http://github.com/github/gitignore

Also they recommend to include in your local .gitignore file only files related toy your project, no system generated files, no IDE files. Exclude them globally in your whole system.

https://github.com/github/gitignore/tree/master/Global

https://docs.github.com/en/github/using-git/ignoring-files#configuring-ignored-files-for-all-repositories-on-your-computer

https://docs.github.com/en/github/using-git/ignoring-files#configuring-ignored-files-for-all-repositories-on-your-computer

 TIPS 

Set fetch.prune config to true, this way, when any fetches are made, git will report any remotes branches that were removed.

git config --global fetch.prune true

 TIPS 

BFG Repo-Cleaner. If you have a repository that weights a lot, you can scan it with BFG Repo-Cleaner, and it will report, and help you to get rid of big files in the repository, that are long time no needed but keep space. https://rtyley.github.io/bfg-repo-cleaner/

 Are you ready for open source? 
 git reset —soft origin/main 

 LINKS 

https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh
https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases
https://git-scm.com/docs/gitkhttps://www.git-scm.com/book/en/v2/Git-Tools-Rerere
https://github.com/github/gitignore
https://github.com/github/gitignore/tree/master/Global
https://rtyley.github.io/bfg-repo-cleaner/
https://www.conventionalcommits.org/en/v1.0.0/
https://github.com/semantic-release/semantic-release
https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github
https://github.com/sindresorhus/.github/blob/main/contributing.md
https://egghead.io/courses/fix-common-git-mistakes
https://egghead.io/courses/productive-git-for-developers
https://egghead.io/courses/practical-git-for-everyday-professional-use
https://egghead.io/courses/github-tips-tricks-6fc4
https://www.notion.so/Pentabar-Do-you-like-time-travel-You-should-love-git-24b4cf770f744332a3fba41b060d7684

Author

Andrew Luca
Technical Lead at  Code for Moldova  (free time)
Technical Lead at  Muume