refactor
This commit is contained in:
parent
5ba581eb03
commit
f611fa9038
1 changed files with 187 additions and 36 deletions
|
@ -1,62 +1,123 @@
|
||||||
---
|
---
|
||||||
obj: application
|
obj: application
|
||||||
|
wiki: https://en.wikipedia.org/wiki/Git
|
||||||
|
repo: https://github.com/git/git
|
||||||
|
website: https://git-scm.com
|
||||||
---
|
---
|
||||||
|
|
||||||
# Git
|
# Git
|
||||||
#refactor
|
|
||||||
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.
|
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)
|
There is a commit naming scheme called [gitmoji](gitmoji.md)
|
||||||
|
|
||||||
To control a git repo graphically [GitHub Desktop](https://github.com/desktop/desktop) can be used.
|
To control a git repo graphically [GitHub Desktop](../applications/development/GitHub%20Desktop.md) can be used.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
Git can be configured with a `.gitconfig` file or via the command line.
|
||||||
In order to use Git you need to set at least a name and [email](../internet/eMail.md):
|
In order to use Git you need to set at least a name and [email](../internet/eMail.md):
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
git config --global user.name "John Doe"
|
git config --global user.name "John Doe"
|
||||||
git config --global user.email "johndoe@example.com"
|
git config --global user.email "johndoe@example.com"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
### Using a Git repository
|
### Git repository
|
||||||
|
|
||||||
Initialize a repository
|
Initialize a repository
|
||||||
`git init`
|
```shell
|
||||||
|
git init
|
||||||
|
git init --bare
|
||||||
|
git init -b main
|
||||||
|
```
|
||||||
|
|
||||||
Clone an existing repository
|
Clone an existing repository
|
||||||
`git clone repository`
|
```shell
|
||||||
|
git clone repository
|
||||||
|
git clone repository folder
|
||||||
|
git clone --recursive repository
|
||||||
|
git clone --bare repository
|
||||||
|
git clone --sparse repository
|
||||||
|
git clone -b master repository
|
||||||
|
```
|
||||||
|
|
||||||
Commit:
|
See status of tracked and untracked files:
|
||||||
`git commit -m message`
|
```shell
|
||||||
|
git status
|
||||||
|
```
|
||||||
|
|
||||||
|
Rename a file or folder:
|
||||||
|
```shell
|
||||||
|
git mv old new
|
||||||
|
```
|
||||||
|
|
||||||
See Commits:
|
See Commits:
|
||||||
`git log`
|
```shell
|
||||||
|
git log
|
||||||
|
git log --graph --oneline --decorate
|
||||||
|
```
|
||||||
|
|
||||||
See structured history:
|
### Commits
|
||||||
`git log --graph --oneline --decorate`
|
|
||||||
|
|
||||||
### Signing commits
|
|
||||||
Git allows commits and tags to be signed using [GnuPG](../tools/GPG.md).
|
Git allows commits and tags to be signed using [GnuPG](../tools/GPG.md).
|
||||||
`git config --global commit.gpgSign true`
|
```shell
|
||||||
|
git config --global commit.gpgSign true`
|
||||||
|
```
|
||||||
|
|
||||||
|
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"
|
||||||
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
## Branching
|
## Branching
|
||||||
Fixes and new features are usually tested in branches. When changes are satisfactory they can merged back into the default (master) branch.
|
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:
|
Create a branch, whose name accurately reflects its purpose:
|
||||||
`git branch help-section-addition`
|
```shell
|
||||||
|
git branch help-section-addition
|
||||||
|
```
|
||||||
|
|
||||||
List branches:
|
List branches:
|
||||||
`git branch`
|
```shell
|
||||||
|
git branch
|
||||||
|
git branch --all
|
||||||
|
git branch --remotes
|
||||||
|
```
|
||||||
|
|
||||||
Switch branches:
|
Switch branches:
|
||||||
`git checkout branch`
|
```shell
|
||||||
|
git checkout <ref>
|
||||||
Create and switch:
|
# Switch to ref and create new branch
|
||||||
`git checkout -b branch`
|
git checkout -B <branch> <ref>
|
||||||
|
```
|
||||||
|
|
||||||
Merge a branch back to the master branch:
|
Merge a branch back to the master branch:
|
||||||
```shell
|
```shell
|
||||||
|
@ -66,32 +127,122 @@ git merge branch
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
When done with a branch, delete it with:
|
When done with a branch, delete it with:
|
||||||
`git branch -d branch`
|
```shell
|
||||||
|
git branch -d branch
|
||||||
|
```
|
||||||
|
|
||||||
## Remotes
|
## Remotes
|
||||||
Create a remote:
|
Working with remotes:
|
||||||
`git remote add label location`
|
```shell
|
||||||
|
git remote [-v | --verbose]
|
||||||
View remotes for the current repository:
|
git remote add [-t <branch>] [-m <master>] [-f] <name> <URL>
|
||||||
`git remote -v`
|
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>)...]
|
||||||
|
```
|
||||||
|
|
||||||
Pull from a reposity:
|
Pull from a reposity:
|
||||||
`git pull`
|
```shell
|
||||||
|
git pull
|
||||||
|
```
|
||||||
|
|
||||||
Push to a repository:
|
Push to a repository:
|
||||||
`git push location branch`
|
```shell
|
||||||
|
git push
|
||||||
|
git push --force
|
||||||
|
git push remote branch
|
||||||
|
```
|
||||||
|
|
||||||
## Tagging
|
## Tagging
|
||||||
Tag commits for versioning:
|
Tag commits for versioning:
|
||||||
`git tag 2.14 checksum`
|
```shell
|
||||||
`git tag -a 2.14 -m "Version 2.14"`
|
git tag 2.14 <commit>
|
||||||
|
|
||||||
|
# Signed Tag
|
||||||
|
git tag -s 2.14
|
||||||
|
|
||||||
|
# Tag with message
|
||||||
|
git tag -a 2.14 -m "Version 2.14"
|
||||||
|
```
|
||||||
|
|
||||||
List tags:
|
List tags:
|
||||||
`git tag -l`
|
```shell
|
||||||
|
git tag -l
|
||||||
|
```
|
||||||
|
|
||||||
Delete a tag:
|
Delete a tag:
|
||||||
`git tag -d 2.08`
|
```shell
|
||||||
|
git tag -d 2.08
|
||||||
|
```
|
||||||
|
|
||||||
Update remote tags:
|
Update remote tags:
|
||||||
`git push --tags`
|
```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
|
||||||
|
|
||||||
|
# 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.
|
||||||
|
This file contains pattern on each line which exclude files from git versioning.
|
Loading…
Reference in a new issue