1
1
mirror of https://github.com/theoludwig/theoludwig.git synced 2024-11-05 04:51:30 +01:00
.profile/posts/git-ultimate-guide.md
2022-10-20 22:43:25 +02:00

5.3 KiB

title description isPublished publishedOn
🗓️ Git version control: Ultimate Guide 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. true 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.

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 in 2005 for development of the Linux kernel.

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.

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:

[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.

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

# 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:

.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