knowledge/technology/dev/Git.md

305 lines
7.4 KiB
Markdown
Raw Permalink Normal View History

2023-12-04 10:02:23 +00:00
---
obj: application
2023-12-12 16:57:02 +00:00
wiki: https://en.wikipedia.org/wiki/Git
repo: https://github.com/git/git
website: https://git-scm.com
2024-12-04 13:07:33 +00:00
rev: 2024-12-04
2023-12-04 10:02:23 +00:00
---
# Git
Git is the version control system (VCS) designed and developed by Linus Torvalds, the creator of the [Linux](../linux/Linux.md) kernel. Git is now used to maintain AUR packages, as well as many other projects, including sources for the [Linux](../linux/Linux.md) kernel.
There is a commit naming scheme called [gitmoji](gitmoji.md)
2023-12-12 16:57:02 +00:00
To control a git repo graphically [GitHub Desktop](../applications/development/GitHub%20Desktop.md) can be used.
2023-12-04 10:02:23 +00:00
## Configuration
2023-12-12 16:57:02 +00:00
Git can be configured with a `.gitconfig` file or via the command line.
2023-12-04 10:02:23 +00:00
In order to use Git you need to set at least a name and [email](../internet/eMail.md):
```shell
git config --global user.name "John Doe"
git config --global user.email "johndoe@example.com"
```
## Usage
2024-01-17 08:44:04 +00:00
A Git repository is contained in a `.git` directory, which holds the revision history and other metadata. The directory tracked by the repository, by default the parent directory, is called the working directory. Changes in the working tree need to be staged before they can be recorded (committed) to the repository. Git also lets you restore, previously committed, working tree files.
2023-12-04 10:02:23 +00:00
2023-12-12 16:57:02 +00:00
### Git repository
2023-12-04 10:02:23 +00:00
Initialize a repository
2023-12-12 16:57:02 +00:00
```shell
git init
git init --bare
git init -b main
```
2023-12-04 10:02:23 +00:00
Clone an existing repository
2023-12-12 16:57:02 +00:00
```shell
2024-01-17 08:44:04 +00:00
git clone repository
2023-12-12 16:57:02 +00:00
git clone repository folder
git clone --recursive repository
git clone --bare repository
git clone --sparse repository
git clone -b master repository
```
2023-12-04 10:02:23 +00:00
2023-12-12 16:57:02 +00:00
See status of tracked and untracked files:
```shell
git status
```
2023-12-04 10:02:23 +00:00
2023-12-12 16:57:02 +00:00
Rename a file or folder:
```shell
git mv old new
```
2023-12-04 10:02:23 +00:00
2023-12-12 16:57:02 +00:00
See Commits:
```shell
git log
git log --graph --oneline --decorate
2024-02-06 07:12:49 +00:00
# Graph of all branches
git log --graph --all
# Extended graph
git log --graph
2023-12-12 16:57:02 +00:00
```
2023-12-04 10:02:23 +00:00
2023-12-12 16:57:02 +00:00
### Commits
2024-01-17 08:00:45 +00:00
Git allows commits and tags to be signed using [GnuPG](../cryptography/GPG.md).
2023-12-12 16:57:02 +00:00
```shell
git config --global commit.gpgSign true`
```
2024-04-15 08:27:46 +00:00
See how a file come together over commits and authors:
```shell
git blame file
# Ignore code movements
git blame -C -C -C file
```
2023-12-12 16:57:02 +00:00
Add files to version control:
```shell
git add file
# Add everything
git add -A
```
Commit changes:
```shell
git commit -m "message"
# Signed commit
git commit -s -m "message"
2024-04-15 08:27:46 +00:00
# Fix last commit with new changes
git commit --amend
2023-12-12 16:57:02 +00:00
```
2024-02-06 07:18:11 +00:00
See commit changes:
```shell
git show commit
# See only changed filenames or status
git show --name-only commit
git show --name-status commit
# Show commit statistics
git show --stat commit
# Show patch of commit
git show -p commit
git show --patch commit
```
2023-12-12 16:57:02 +00:00
Revert changes:
```shell
git revert <commit>
# Signed revert commit
git revert -s <commit>
# Revert to the fourth last commit from HEAD
git revert HEAD~3
```
Bring back old revisions of files:
```shell
git restore file
# Restore state before two commits from master
git restore --source master~2 Makefile
```
2023-12-04 10:02:23 +00:00
## Branching
Fixes and new features are usually tested in branches. When changes are satisfactory they can merged back into the default (master) branch.
Create a branch, whose name accurately reflects its purpose:
2023-12-12 16:57:02 +00:00
```shell
git branch help-section-addition
```
2023-12-04 10:02:23 +00:00
List branches:
2023-12-12 16:57:02 +00:00
```shell
git branch
git branch --all
git branch --remotes
```
2023-12-04 10:02:23 +00:00
Switch branches:
2023-12-12 16:57:02 +00:00
```shell
git checkout <ref>
# Switch to ref and create new branch
git checkout -B <branch> <ref>
```
2023-12-04 10:02:23 +00:00
Merge a branch back to the master branch:
```shell
git checkout master
git merge branch
```
2024-01-17 08:44:04 +00:00
The changes will be merged if they do not conflict. Otherwise, Git will print an error message, and annotate files in the working tree to record the conflicts. The annotations can be displayed with `git diff`. Conflicts are resolved by editing the files to remove the annotations, and committing the final version.
2023-12-04 10:02:23 +00:00
2023-12-12 16:57:02 +00:00
Any conflicts will look something like this:
```
Here are lines that are either unchanged from the common
ancestor, or cleanly resolved because only one side changed,
or cleanly resolved because both sides changed the same way.
<<<<<<< yours:sample.txt
Conflict resolution is hard;
let's go shopping.
=======
Git makes conflict resolution easy.
>>>>>>> theirs:sample.txt
And here is another line that is cleanly resolved or unmodified.
```
Continue the merge after resolving merge conflicts:
```shell
git merge --continue
```
2023-12-04 10:02:23 +00:00
When done with a branch, delete it with:
2023-12-12 16:57:02 +00:00
```shell
git branch -d branch
2024-09-13 07:43:33 +00:00
# Also delete on remote
git push -d <remote_name> <branchname>
2023-12-12 16:57:02 +00:00
```
2023-12-04 10:02:23 +00:00
## Remotes
2023-12-12 16:57:02 +00:00
Working with remotes:
```shell
git remote [-v | --verbose]
git remote add [-t <branch>] [-m <master>] [-f] <name> <URL>
git remote rename <old> <new>
git remote remove <name>
git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
git remote set-branches [--add] <name> <branch>...
git remote get-url [--push] [--all] <name>
git remote set-url [--push] <name> <newurl> [<oldurl>]
git remote set-url --add [--push] <name> <newurl>
git remote set-url --delete [--push] <name> <URL>
git remote [-v | --verbose] show [-n] <name>...
git remote prune [-n | --dry-run] <name>...
git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]
```
2023-12-04 10:02:23 +00:00
Pull from a reposity:
2023-12-12 16:57:02 +00:00
```shell
git pull
2024-05-21 06:44:25 +00:00
# Rebase instead of merge (append your commit if remote is newer)
git pull --rebase
2023-12-12 16:57:02 +00:00
```
2023-12-04 10:02:23 +00:00
Push to a repository:
2023-12-12 16:57:02 +00:00
```shell
git push
git push --force
git push remote branch
```
2023-12-04 10:02:23 +00:00
## Tagging
Tag commits for versioning:
2023-12-12 16:57:02 +00:00
```shell
git tag 2.14 <commit>
# Signed Tag
git tag -s 2.14
# Tag with message
git tag -a 2.14 -m "Version 2.14"
```
2023-12-04 10:02:23 +00:00
List tags:
2023-12-12 16:57:02 +00:00
```shell
git tag -l
```
2023-12-04 10:02:23 +00:00
Delete a tag:
2023-12-12 16:57:02 +00:00
```shell
git tag -d 2.08
```
2023-12-04 10:02:23 +00:00
Update remote tags:
2023-12-12 16:57:02 +00:00
```shell
git push --tags
```
## Patches & Diffs
Show differences:
```shell
# Show uncommited changes
git diff
# Output a summary of file creations, renames and mode changes since a given commit:
git diff --summary commit
2024-04-15 08:27:46 +00:00
# Output a difference by word instead of line
git diff --word-diff
2023-12-31 06:01:10 +00:00
# Output a statistic of changes
git diff --stat commit
2023-12-12 16:57:02 +00:00
# Compare a single file between two branches or commits:
git diff branch_1..branch_2 [--] path/to/file
# Compare changes between commits
git range-diff <commit> <commit>
```
Generate a patch file:
```shell
git format-patch <revision range>
```
Apply patch files to repository:
```shell
git apply patch
# Revert patch
git apply --reverse patch
# Another way to apply patches
git am < patch
git am --signoff < patch
git am --continue < patch
git am --abort < patch
```
## .gitignore
A `.gitignore` file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.
2024-12-04 13:07:33 +00:00
This file contains pattern on each line which exclude files from git versioning.
## Git Hooks
Git hooks are custom scripts that run automatically in response to certain Git events or actions. These hooks are useful for automating tasks like code quality checks, running tests, enforcing commit message conventions, and more. Git hooks can be executed at different points in the Git workflow, such as before or after a commit, push, or merge.
Git hooks are stored in the `.git/hooks` directory of your repository. By default, this directory contains example scripts with the `.sample` extension. You can customize these scripts by removing the `.sample` extension and editing them as needed.
Hooks only apply to your local repository. If a hook script fails it prevents the associated action as well.
### Common Git Hooks
- pre-commit
- prepare-commit-msg
- commit-msg
- post-commit
- post-checkout
- pre-rebase