|
|
Git is a versionning tool allowing multiple programmers to work simultaneously on the same codebase.
|
|
Git is a versionning tool allowing multiple programmers to work simultaneously on the same codebase. Remember one day as a programmer you will have issues with git, wether you want it or not. So you better get good at it.
|
|
|
Remember one day as a programmer you will have issues with git, wether you want it or not. So you better get good at it.
|
|
|
|
|
|
|
|
|
|
Once you have learnt how these commands work I strongly advise you to use a git tui (Terminal user Interface) such as [gitui](https://github.com/extrawurst/gitui), lazygit or vscode.
|
|
Once you have learnt how these commands work I strongly advise you to use a git tui (Terminal user Interface) such as [gitui](https://github.com/extrawurst/gitui), lazygit or vscode. This will help you curate your commits to have meaningful commits.
|
|
|
This will help you curate your commits to have meaningful commits.
|
|
|
|
|
|
|
|
|
|
[TOC]
|
|
[[_TOC_]]
|
|
|
|
|
|
|
|
# commands
|
|
# commands
|
|
|
|
|
|
|
## add
|
|
## add
|
|
|
|
|
|
|
|
To prepare a [commit](#commit) : each added file will be in your next [commit](#commit)
|
|
To prepare a [commit](#commit) : each added file will be in your next [commit](#commit)
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git add <file/folder>
|
|
git add <file/folder>
|
|
|
```
|
|
```
|
|
|
To remove an added file use the (restore --staged <path>)[#args] command .
|
|
|
|
|
|
To remove an added file use the (restore --staged )\[#args\] command .
|
|
|
|
|
|
|
|
## bisect
|
|
## bisect
|
|
|
|
|
|
|
allow you to research when a bug has occurred within your history : if a [commit](#commit) is good or bad.
|
|
allow you to research when a bug has occurred within your history : if a [commit](#commit) is good or bad.
|
|
|
|
|
|
|
|
```bash
|
|
```bash
|
| ... | @@ -23,137 +25,179 @@ git bisect start |
... | @@ -23,137 +25,179 @@ git bisect start |
|
|
git bisect good
|
|
git bisect good
|
|
|
git bisect bad
|
|
git bisect bad
|
|
|
git bisect good
|
|
git bisect good
|
|
|
|
|
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## branch
|
|
## branch
|
|
|
|
|
|
|
To handle git branches, be it listing, renaming, copying, deletion.
|
|
To handle git branches, be it listing, renaming, copying, deletion.
|
|
|
|
|
|
|
|
Shows repo branches:
|
|
Shows repo branches:
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git branch
|
|
git branch
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
#### -m / --move
|
|
#### \-m / --move
|
|
|
|
|
|
|
rename a branch (pro tip : `-M` to force)
|
|
rename a branch (pro tip : `-M` to force)
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git branch -m <old_branch> <new_branch>
|
|
git branch -m <old_branch> <new_branch>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
#### --show-current
|
|
#### \--show-current
|
|
|
|
|
|
|
will output your current git branch
|
|
will output your current git branch
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git branch --show-current
|
|
git branch --show-current
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## cherry-pick
|
|
## cherry-pick
|
|
|
|
|
|
|
Re-apply a given commit on the current branch. Useful when you want a feature from another branch without merging everything.
|
|
Re-apply a given commit on the current branch. Useful when you want a feature from another branch without merging everything.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git cherry-pick <commit_hash>
|
|
git cherry-pick <commit_hash>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## clone
|
|
## clone
|
|
|
|
|
|
|
clone a repository
|
|
clone a repository
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git clone <repository> [<dest_dir>]
|
|
git clone <repository> [<dest_dir>]
|
|
|
```
|
|
```
|
|
|
|
|
|
|
repository is either ssh or https address of the repo.
|
|
repository is either ssh or https address of the repo.
|
|
|
|
|
|
|
|
## commit
|
|
## commit
|
|
|
|
|
|
|
A commit is like a checkpoint : you have modified multiple files, and you [push](#push) them to the [origin](#origin) with a commit message like "hey I've done this and that".
|
|
A commit is like a checkpoint : you have modified multiple files, and you [push](#push) them to the [origin](#origin) with a commit message like "hey I've done this and that".
|
|
|
|
|
|
|
|
A commit can but doesn't have to contain all your modified files, it will only include [added](#add) files.
|
|
A commit can but doesn't have to contain all your modified files, it will only include [added](#add) files. any commit will be accompanied by a **commit message** that NEEDS to contain the important things that has been modified in the added files.
|
|
|
any commit will be accompanied by a **commit message** that NEEDS to contain the important things that has been modified in the added files.
|
|
|
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git commit
|
|
git commit
|
|
|
```
|
|
```
|
|
|
|
|
|
|
will open a text edition in the terminal where you be able to create the message
|
|
will open a text edition in the terminal where you be able to create the message
|
|
|
|
|
|
|
|
## diff
|
|
## diff
|
|
|
|
|
|
|
shows you differences between current branch and given branch.
|
|
shows you differences between current branch and given branch.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git diff
|
|
git diff
|
|
|
```
|
|
```
|
|
|
|
|
|
|
will show you differences between your local branch and `origin`.
|
|
will show you differences between your local branch and `origin`.
|
|
|
|
|
|
|
|
### args
|
|
### args
|
|
|
|
|
|
|
#### `- p <commit>`
|
|
#### `- p <commit>`
|
|
|
|
|
|
|
Will show differences between current branch and given `<COMMIT>`
|
|
Will show differences between current branch and given `<COMMIT>`
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git diff -p <COMMIT>
|
|
git diff -p <COMMIT>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
#### `--compact-summary`
|
|
#### `--compact-summary`
|
|
|
|
|
|
|
Show a short summary of modifications file-by-file before going in-depth
|
|
Show a short summary of modifications file-by-file before going in-depth
|
|
|
|
|
|
|
|
## log
|
|
## log
|
|
|
|
|
|
|
shows commit history of the current repo.
|
|
shows commit history of the current repo.
|
|
|
|
|
|
|
|
#### `-s`
|
|
#### `-s`
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git log -S<keyword>
|
|
git log -S<keyword>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
search for `keyword` in commits modifications to show you when it was modified, very useful to know when a function was removed for example.
|
|
search for `keyword` in commits modifications to show you when it was modified, very useful to know when a function was removed for example.
|
|
|
|
|
|
|
|
#### `-n`
|
|
#### `-n`
|
|
|
|
|
|
|
retrieves the n last commits (here the last 10)
|
|
retrieves the n last commits (here the last 10)
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git log -10
|
|
git log -10
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
#### `--format=`
|
|
#### `--format=`
|
|
|
retrieves only a part of the commit, either date, hash or message.
|
|
|
|
|
to retrieve the current commit hash.
|
|
retrieves only a part of the commit, either date, hash or message. to retrieve the current commit hash.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git log -1 --format=format:"%H"
|
|
git log -1 --format=format:"%H"
|
|
|
```
|
|
```
|
|
|
|
|
|
|
### search through your history
|
|
### search through your history
|
|
|
|
|
|
|
#### using grep
|
|
#### using grep
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git log --grep=<pattern>
|
|
git log --grep=<pattern>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
#### filtering by date
|
|
#### filtering by date
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git log --since=<date> | --after=<date>
|
|
git log --since=<date> | --after=<date>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
### fancy log with graph
|
|
### fancy log with graph
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git log --graph --oneline --decorate
|
|
git log --graph --oneline --decorate
|
|
|
```
|
|
```
|
|
|
> [!tip] pretty printing log
|
|
|
|
|
> there are tons of nice formatting for commits of git log, look for them in `git log --help`
|
|
|
|
|
|
|
|
|
|
|
> \[!tip\] pretty printing log there are tons of nice formatting for commits of git log, look for them in `git log --help`
|
|
|
|
|
|
|
|
---
|
|
---
|
|
|
|
|
|
|
|
### args
|
|
### args
|
|
|
|
|
|
|
#### `-m`
|
|
#### `-m`
|
|
|
|
|
|
|
Most common use of `git commit` is to directly include the message with the `-m` flag that stands for **message**
|
|
Most common use of `git commit` is to directly include the message with the `-m` flag that stands for **message**
|
|
|
|
|
|
|
```
|
|
```
|
|
|
git commit -m "message"
|
|
git commit -m "message"
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
#### `-a`
|
|
#### `-a`
|
|
|
|
|
|
|
Even more radical : `-a` stands for **all** and will add all files un the current working directory and you create the message on the fly.
|
|
Even more radical : `-a` stands for **all** and will add all files un the current working directory and you create the message on the fly.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git commit -am "message"
|
|
git commit -am "message"
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
### `--allow-empty`
|
|
### `--allow-empty`
|
|
|
|
|
|
|
Will allow an empty commit to be done. Useful to trigger a pipeline.
|
|
Will allow an empty commit to be done. Useful to trigger a pipeline.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git commit --allow-empty -m "chore : trigger CI"
|
|
git commit --allow-empty -m "chore : trigger CI"
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
#### `--amend`
|
|
#### `--amend`
|
|
|
|
|
|
|
allow you to modify stuff on the last commit that hasn't been pushed
|
|
allow you to modify stuff on the last commit that hasn't been pushed
|
|
|
|
|
|
|
- here we modify the message of the previous commit
|
|
- here we modify the message of the previous commit
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git commit -m "yeeet"
|
|
git commit -m "yeeet"
|
|
|
git commit --amend -m "yeet"
|
|
git commit --amend -m "yeet"
|
|
|
```
|
|
```
|
|
|
|
|
|
|
- here we add a file to the previous without modifying the message
|
|
- here we add a file to the previous without modifying the message
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git commit -m "yeeet"
|
|
git commit -m "yeeet"
|
|
|
git add somefile.🔥
|
|
git add somefile.🔥
|
| ... | @@ -161,18 +205,23 @@ git commit --amend --no-edit |
... | @@ -161,18 +205,23 @@ git commit --amend --no-edit |
|
|
```
|
|
```
|
|
|
|
|
|
|
|
#### `--squash`
|
|
#### `--squash`
|
|
|
|
|
|
|
Will indicate that the given commit will be squashed if you call the follwing
|
|
Will indicate that the given commit will be squashed if you call the follwing
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git rebase -i --autosquash <hash>
|
|
git rebase -i --autosquash <hash>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
#### `--fixup`
|
|
#### `--fixup`
|
|
|
|
|
|
|
same as above but with the fixup command
|
|
same as above but with the fixup command
|
|
|
|
|
|
|
|
## merge
|
|
## merge
|
|
|
|
|
|
|
Action of bringing modifications of modifications brought in a branch to another branch.
|
|
Action of bringing modifications of modifications brought in a branch to another branch.
|
|
|
|
|
|
|
|
The following command will apply commits of branch_B in branch_A in 1 commit
|
|
The following command will apply commits of branch_B in branch_A in 1 commit
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
# in branch_A
|
|
# in branch_A
|
|
|
git merge <branch_B>
|
|
git merge <branch_B>
|
| ... | @@ -180,14 +229,14 @@ git merge <branch_B> |
... | @@ -180,14 +229,14 @@ git merge <branch_B> |
|
|
|
|
|
|
|
If you are on your own branch and want to merge your branch into `main`, It is strongly advised to merge main into your branch to keep track of latest updates of main and add them in your branch.
|
|
If you are on your own branch and want to merge your branch into `main`, It is strongly advised to merge main into your branch to keep track of latest updates of main and add them in your branch.
|
|
|
|
|
|
|
|
|
|
{width="300"}
|
|
|
{width=300}
|
|
|
|
|
|
|
|
|
|
---
|
|
---
|
|
|
|
|
|
|
|
## mv
|
|
## mv
|
|
|
Move a file inside the directory and stage that the file has been moved at the same time.
|
|
|
|
|
If you do a regular `mv` within a directory, the history of the file will be lost because git will understand this as "you have deleted a file here and added a new file there."
|
|
Move a file inside the directory and stage that the file has been moved at the same time. If you do a regular `mv` within a directory, the history of the file will be lost because git will understand this as "you have deleted a file here and added a new file there."
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git mv <old_path_file> <new_path_file>
|
|
git mv <old_path_file> <new_path_file>
|
|
|
```
|
|
```
|
| ... | @@ -195,55 +244,83 @@ git mv <old_path_file> <new_path_file> |
... | @@ -195,55 +244,83 @@ git mv <old_path_file> <new_path_file> |
|
|
---
|
|
---
|
|
|
|
|
|
|
|
## push
|
|
## push
|
|
|
|
|
|
|
pushes commited modifications to origin branch. Base usage is
|
|
pushes commited modifications to origin branch. Base usage is
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git push
|
|
git push
|
|
|
```
|
|
```
|
|
|
|
|
|
|
which is actually sugar syntax for
|
|
which is actually sugar syntax for
|
|
|
|
|
|
|
```
|
|
```
|
|
|
git push <remote> <current_branch>
|
|
git push <remote> <current_branch>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
### `--set-upstream`/`-u`
|
|
### `--set-upstream`/`-u`
|
|
|
|
|
|
|
Sets the branch given as arg as the reference branch to follow.
|
|
Sets the branch given as arg as the reference branch to follow.
|
|
|
|
|
|
|
|
---
|
|
---
|
|
|
|
|
|
|
|
## rebase
|
|
## rebase
|
|
|
|
|
|
|
|
### `-i`
|
|
|
|
|
|
|
allows you to edit your git history to make it clearer before merging
|
|
allows you to edit your git history to make it clearer before merging
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git rebase -i <hash>
|
|
git rebase -i <hash>
|
|
|
```
|
|
```
|
|
|
⚠ since you edit your history you will need to force push afterward.
|
|
|
|
|
### tutorial (fr)
|
|
:warning: since you edit your history you will need to force push afterward.
|
|
|
https://www.youtube.com/watch?v=A_jreWjCl4s
|
|
|
|
|
|
### tutorial
|
|
|
|
|
|
|
|
https://www.youtube.com/watch?v=rt9ZOVciJm8
|
|
|
|
|
|
|
|
### `--autosquash`
|
|
### `--autosquash`
|
|
|
|
|
|
|
automatically squashes commits that you previously marked with `--squash` while comitting
|
|
automatically squashes commits that you previously marked with `--squash` while comitting
|
|
|
|
|
|
|
|
---
|
|
---
|
|
|
|
|
|
|
## remote
|
|
## remote
|
|
|
|
|
|
|
Allows you to handle the remote repository. This is mostly useful when you are working on a fork and need to retrieve modifications from multiple remotes.
|
|
Allows you to handle the remote repository. This is mostly useful when you are working on a fork and need to retrieve modifications from multiple remotes.
|
|
|
|
|
|
|
### `add <remote_name> <url>`
|
|
### `add <remote_name> <url>`
|
|
|
|
|
|
|
If you are working in a fork and want to have both origins
|
|
If you are working in a fork and want to have both origins
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git remote add fork <fork_url>
|
|
git remote add fork <fork_url>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
### `set-url <remote name> <url>`
|
|
### `set-url <remote name> <url>`
|
|
|
|
|
|
|
Changes the url of a remote name. for example If you just moved the repository in your gitlab you might need to use.
|
|
Changes the url of a remote name. for example If you just moved the repository in your gitlab you might need to use.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git remote set-url origin <new_url>
|
|
git remote set-url origin <new_url>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
### `remove <remote_name>`
|
|
### `remove <remote_name>`
|
|
|
|
|
|
|
self-explanatory
|
|
self-explanatory
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git remote remove <remote_name>
|
|
git remote remove <remote_name>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
---
|
|
---
|
|
|
|
|
|
|
## restore
|
|
## restore
|
|
|
|
|
|
|
Undoes all modifications done to given file/folder/path done since last commit
|
|
Undoes all modifications done to given file/folder/path done since last commit
|
|
|
> :warning: This command cannot be undone
|
|
|
|
|
> If you git restored files that contained valuable modifications you will have to redo it by hand.
|
|
> :warning: This command cannot be undone If you git restored files that contained valuable modifications you will have to redo it by hand.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git restore <file/folder/path>
|
|
git restore <file/folder/path>
|
|
|
```
|
|
```
|
| ... | @@ -253,15 +330,19 @@ git restore <file/folder/path> |
... | @@ -253,15 +330,19 @@ git restore <file/folder/path> |
|
|
```bash
|
|
```bash
|
|
|
restore --staged <path>
|
|
restore --staged <path>
|
|
|
```
|
|
```
|
|
|
will remove given file of the list of (added)[#add] files.
|
|
|
|
|
|
will remove given file of the list of (added)\[#add\] files.
|
|
|
|
|
|
|
|
---
|
|
---
|
|
|
|
|
|
|
|
## revert
|
|
## revert
|
|
|
Reverts the modifications of a specific commit, often paired with [cherry pick](#Cherry pick).
|
|
|
|
|
|
Reverts the modifications of a specific commit, often paired with [cherry pick](#Cherry%20pick).
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git revert <commit_hash>
|
|
git revert <commit_hash>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
### `-m <parent_number>` `--mainline <parent_number>`
|
|
### `-m <parent_number>` `--mainline <parent_number>`
|
|
|
|
|
|
|
|
Usually you cannot cherry-pick a merge because **you do not know which side of the merge** should be considered the mainline.
|
|
Usually you cannot cherry-pick a merge because **you do not know which side of the merge** should be considered the mainline.
|
| ... | @@ -269,89 +350,115 @@ Usually you cannot cherry-pick a merge because **you do not know which side of t |
... | @@ -269,89 +350,115 @@ Usually you cannot cherry-pick a merge because **you do not know which side of t |
|
|
This option specifies the parent number (starting from 1) of the mainline and allows cherry-pick to replay the change relative to the specified parent.
|
|
This option specifies the parent number (starting from 1) of the mainline and allows cherry-pick to replay the change relative to the specified parent.
|
|
|
|
|
|
|
|
---
|
|
---
|
|
|
|
|
|
|
## show
|
|
## show
|
|
|
|
|
|
|
Show what the given commit has modified
|
|
Show what the given commit has modified
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git show <commit_hash>
|
|
git show <commit_hash>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
---
|
|
|
|
|
|
|
|
## stash
|
|
## stash
|
|
|
|
|
|
|
For when you have uncommited modifications you want to temporarly keep on the side to do something else, you can stash them. It works like restore but saves the modifications. They will be kept in a stack that you can access with different commands :
|
|
For when you have uncommited modifications you want to temporarly keep on the side to do something else, you can stash them. It works like restore but saves the modifications. They will be kept in a stack that you can access with different commands :
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git **stash**
|
|
git **stash**
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## status
|
|
## status
|
|
|
|
|
|
|
Once you have set up your repo in local you can start modifying it.
|
|
Once you have set up your repo in local you can start modifying it.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git status
|
|
git status
|
|
|
```
|
|
```
|
|
|
|
|
|
|
will show you what files you have modified / [added](#add) and not [commited](#commit).
|
|
will show you what files you have modified / [added](#add) and not [commited](#commit).
|
|
|
|
|
|
|
|
### save
|
|
### save
|
|
|
|
|
|
|
allows you to give a specific name to the stash you are creating :
|
|
allows you to give a specific name to the stash you are creating :
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git stash save <killer_feature_name>
|
|
git stash save <killer_feature_name>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
### pop
|
|
### pop
|
|
|
|
|
|
|
to keep uncommited modifications on the side in a stack, you you stash something else it'll be stacked upon the first one.
|
|
to keep uncommited modifications on the side in a stack, you you stash something else it'll be stacked upon the first one.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git stash pop
|
|
git stash pop
|
|
|
```
|
|
```
|
|
|
|
|
|
|
pop the stack of changes to add them to the code.
|
|
pop the stack of changes to add them to the code.
|
|
|
|
|
|
|
|
### apply
|
|
### apply
|
|
|
|
|
|
|
to apply a specific stash on your code
|
|
to apply a specific stash on your code
|
|
|
|
|
|
|
```
|
|
```
|
|
|
git stash apply <killer_feature_name>
|
|
git stash apply <killer_feature_name>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
### display stash
|
|
### display stash
|
|
|
|
|
|
|
to display the stashes use :
|
|
to display the stashes use :
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git stash list
|
|
git stash list
|
|
|
```
|
|
```
|
|
|
|
|
|
|
To show what files are modified by each stash use :
|
|
To show what files are modified by each stash use :
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git stash show
|
|
git stash show
|
|
|
```
|
|
```
|
|
|
|
|
|
|
To show how files are modified by a given stash :
|
|
To show how files are modified by a given stash :
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git stash show -p
|
|
git stash show -p
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## switch
|
|
## switch
|
|
|
|
|
|
|
change current active branch
|
|
change current active branch
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git switch <branch>
|
|
git switch <branch>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## checkout
|
|
## checkout
|
|
|
Like [switch](switch) but not on a [commit](#commit)], allow you to modify and apply modification on chosen branch afterward
|
|
|
|
|
|
Like [switch](switch) but not on a [commit](#commit)\], allow you to modify and apply modification on chosen branch afterward
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git checkout <commit_hash>
|
|
git checkout <commit_hash>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
---
|
|
---
|
|
|
# 🤔 How to & Troubleshootings 🏹
|
|
|
|
|
|
# :thinking: How to & Troubleshootings :bow_and_arrow:
|
|
|
|
|
|
|
|
## REVERT a commited and pushed commit
|
|
## REVERT a commited and pushed commit
|
|
|
|
|
|
|
Very useful if you pushed something you didn't want : simply undo what the commit has done.
|
|
Very useful if you pushed something you didn't want : simply undo what the commit has done.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git revert <COMMIT_HASH>
|
|
git revert <COMMIT_HASH>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
> [!info] what does it reverts ?
|
|
> \[!info\] what does it reverts ? this command only reverts the precise commit that has been done, not all the commits made since.
|
|
|
> this command only reverts the precise commit that has been done, not all the commits made since.
|
|
|
|
|
|
|
|
|
|
## DELETE a commited and pushed git
|
|
## DELETE a commited and pushed git
|
|
|
|
|
|
|
|
> [!danger] ⚠ VERY DANGEROUS ⚠
|
|
> \[!danger\] :warning: VERY DANGEROUS :warning: The following command DESTROYS EVERY COMMITS PUSHED SINCE THE ONE YOU WANT TO DELETE
|
|
|
> The following command DESTROYS EVERY COMMITS PUSHED SINCE THE ONE YOU WANT TO DELETE
|
|
>
|
|
|
> - always prefer the [revert](#revert) command.
|
|
> - always prefer the [revert](#revert) command.
|
|
|
> - NEVER do it on `main` branch
|
|
> - NEVER do it on `main` branch
|
|
|
> - ALWAYS
|
|
> - ALWAYS
|
| ... | @@ -367,14 +474,17 @@ git push -f |
... | @@ -367,14 +474,17 @@ git push -f |
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## Re-apply a `commit`
|
|
## Re-apply a `commit`
|
|
|
|
|
|
|
[git cherry-pick](#cherry-pick)
|
|
[git cherry-pick](#cherry-pick)
|
|
|
|
|
|
|
|
## undo an unpushed commit
|
|
## undo an unpushed commit
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git reset --soft HEAD~1
|
|
git reset --soft HEAD~1
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## .gitignore is ignored and tracks files mentionned in it
|
|
## .gitignore is ignored and tracks files mentionned in it
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git rm -r --cached .
|
|
git rm -r --cached .
|
|
|
git add .
|
|
git add .
|
| ... | @@ -382,33 +492,42 @@ git commit -m "fix : fixed untracked files" |
... | @@ -382,33 +492,42 @@ git commit -m "fix : fixed untracked files" |
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## get a log of the exchanges between git & the server
|
|
## get a log of the exchanges between git & the server
|
|
|
|
|
|
|
`<command>` can either be fetch pull push or checkout
|
|
`<command>` can either be fetch pull push or checkout
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
GIT_SSH_COMMAND="ssh -vv" git <command>
|
|
GIT_SSH_COMMAND="ssh -vv" git <command>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## Run a git command for a repo outside of it
|
|
## Run a git command for a repo outside of it
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git -C <path_to_repo> <git_command>
|
|
git -C <path_to_repo> <git_command>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## Change the author of a commit
|
|
## Change the author of a commit
|
|
|
|
|
|
|
Use [git rebase](#rebase) if you need to apply that to multiple commits.
|
|
Use [git rebase](#rebase) if you need to apply that to multiple commits.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git rebase -i <hash>
|
|
git rebase -i <hash>
|
|
|
```
|
|
```
|
|
|
This will open your default text editor (defined with the environment variable $EDITOR).
|
|
|
|
|
Then put `edit` in front of the commit you want to modify.
|
|
This will open your default text editor (defined with the environment variable $EDITOR). Then put `edit` in front of the commit you want to modify.
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
edit <hash>
|
|
edit <hash>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
and quit the editor. rebase will put you on the wanted commit and you will be able to modify its author with
|
|
and quit the editor. rebase will put you on the wanted commit and you will be able to modify its author with
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git commit --amend --author="your name <your@email.org>" --no-edit
|
|
git commit --amend --author="your name <your@email.org>" --no-edit
|
|
|
git rebase --continue
|
|
git rebase --continue
|
|
|
```
|
|
```
|
|
|
|
|
|
|
> :warning: for once the `<>` put above are not placeholders for arguments but needed in the syntax
|
|
> :warning: for once the `<>` put above are not placeholders for arguments but needed in the syntax
|
|
|
>
|
|
|
|
|
|
|
|
|
|
# Sources
|
|
# Sources
|
|
|
|
|
|
|
https://www.youtube.com/watch?v=ecK3EnyGD8o |
|
https://www.youtube.com/watch?v=ecK3EnyGD8o |
|
|
|
\ No newline at end of file |