1
1
mirror of https://github.com/theoludwig/theoludwig.git synced 2024-11-05 13:01:30 +01:00
.profile/posts/git-ultimate-guide.md

194 lines
5.3 KiB
Markdown
Raw Normal View History

2022-09-04 20:40:58 +02:00
---
title: '🗓️ Git version control: Ultimate Guide'
description: 'What is `git`, what are the most used commands, best practices, and tips and tricks. The Ultimate guide to master `git` in your daily workflow.'
isPublished: true
publishedOn: '2022-09-04T14:33:07.465Z'
---
Hello! 👋
Welcome to the Ultimate Guide to master `git` in your daily workflow, we will see what are the most used commands, what are the best practices, and tips and tricks.
**Note:** Sources used to write this blog post are available at the [end of this post](#sources).
## Introduction
**Git** is a free and open source distributed **version control system** for keeping track of changes across a set of files.
Git was originally authored by [Linus Torvalds](https://en.wikipedia.org/wiki/Linus_Torvalds) in 2005 for development of the [Linux kernel](https://kernel.org/).
Git allows:
- to be able to work with several people on the same codebase.
- track changes to know who did what and when.
- revert changes.
Git is **decentralized**, which means that every developer has a full copy of the repository and the complete history of the project.
## Get started with `git` and `.gitconfig` config file
The first thing you should do when you install Git is to set your user name and email address.
```sh
git config --global user.name "Username"
git config --global user.email "email@example.com"
```
These configurations are stored in the `.gitconfig` file in your home directory (e.g: `~/.gitconfig`) with this format:
```sh
[user]
name = Username
email = email@example.com
```
You can find more information and useful `git` configurations in the official documentation: [git-scm.com/docs/git-config](https://git-scm.com/docs/git-config).
## How `git` works?
Each `git` project is called a **repository** (or **repo** for short) and it contains all the files and folders for a project, as well as each file's revision history (**commits**) stored in the `.git` folder.
The history of a repository is represented by a graph.
Each node is called commit and contains:
- an instantaneous view (snapshot) of the state of the repository at a specific moment
- metadata: message, author, creation date, etc.
## Commands cheatsheet
```sh
# Initialize a new git repository
git init
# Clone a repository
git clone <url>
# Add all the files to staging area
git add .
# Add specific file to staging area
git add <file>
# Commit changes
git commit -m "chore: initial commit"
# Add remote repository
git remote add origin <url>
# Add forked repository
git remote add upstream <url>
# Sync forked repository
git fetch upstream
git merge upstream/<branch>
# Push changes to remote repository
git push
# Pull changes from remote repository
git pull
# Show the status of the working tree
git status
# Show the commit history
git log
# Create a new branch
git checkout -b <branch>
# Switch to a branch (or tag or commit)
git checkout <branch>
# Merge a branch into the current branch
git merge <branch>
# Delete a branch
git branch -d <branch>
git push origin --delete <branch>
# Fetch branches from remote repository and prune
git fetch --prune
# Revert a commit
git revert <commit>
# Change several past commits (interactive rebase)
# HEAD points to the current consulted commit.
git rebase -i HEAD~<number-of-commits>
# Reset the current branch, delete all commits since <branch> (without removing the changes)
git reset --soft <branch>
# Apply the changes introduced by some existing commits
git cherry-pick <commit>
```
## `.gitignore` file
The `.gitignore` file is a text file that tells `git` which files (or patterns) it should ignore.
The `.gitignore` file is usually placed in the root directory of the repository.
We usually ignore files that are generated by the build process or files that contain sensitive information.
Example of `.gitignore` file:
```sh
.env
build
*.exe
```
## `.gitkeep` file
The `.gitkeep` file is a file that is used to keep an empty directory in a Git repository.
This is useful when you want to keep an empty directory in your repository but you don't want to commit any file inside it.
## Git remote repositories (GitHub/GitLab)
Explain what are the differences, how it works, + basic vocabulary...
### SSH vs HTTPS authentication
Explain the differences + quickly setup a SSH key...
### Sign `git` commits with `gpg`
Explain, how and why to sign `git` commits...
### Continous Integration/Continuous Delivery
GitHub Actions, Vercel, why is it so important...
## Best practices and `git` workflows
### Commit messages and semver
### GitFlow
...
### GitHub Flow
...
### Trunk-based
...
## Conclusion
`git` is the tool that every programmer should know to do collaborative work and keeping track of changes across a set of files.
## Sources
- [Git official website and documentation](https://git-scm.com/)
- [Git Explained in 100 Seconds](https://www.youtube.com/watch?v=hwP7WQkmECE)
- [Understand Git in 7 minutes](https://www.jesuisundev.com/en/understand-git-in-7-minutes/)
- [How (and why) to sign Git commits | With Blue Ink](https://withblue.ink/2020/05/17/how-and-why-to-sign-git-commits.html?utm_source=tiktok&utm_campaign=codetok-sign)
- [Creator of Gitflow blog post](https://nvie.com/posts/a-successful-git-branching-model/)
- [What Are the Best Git Branching Strategies](https://www.flagship.io/git-branching-strategies/)