|
|
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. This will help you curate your commits to have meaningful commits.
|
|
|
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.
|
|
[[_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
|
|
|
|
|
allow you to research when a bug has occurred within your history : if a [commit](#commit) is good or bad.
|
|
## bisect
|
|
|
|
|
|
|
|
```bash
|
|
allow you to research when a bug has occurred within your history : if a [commit](#commit) is good or bad.
|
|
|
git bisect start
|
|
|
|
|
git bisect good
|
|
```bash
|
|
|
git bisect bad
|
|
git bisect start
|
|
|
git bisect good
|
|
git bisect good
|
|
|
|
|
git bisect bad
|
|
|
```
|
|
git bisect good
|
|
|
|
|
```
|
|
|
## branch
|
|
|
|
|
To handle git branches, be it listing, renaming, copying, deletion.
|
|
## branch
|
|
|
|
|
|
|
|
Shows repo branches:
|
|
To handle git branches, be it listing, renaming, copying, deletion.
|
|
|
```bash
|
|
|
|
|
git branch
|
|
Shows repo branches:
|
|
|
```
|
|
|
|
|
|
|
```bash
|
|
|
#### -m / --move
|
|
git branch
|
|
|
rename a branch (pro tip : `-M` to force)
|
|
```
|
|
|
```bash
|
|
|
|
|
git branch -m <old_branch> <new_branch>
|
|
#### \-m / --move
|
|
|
```
|
|
|
|
|
|
|
rename a branch (pro tip : `-M` to force)
|
|
|
#### --show-current
|
|
|
|
|
will output your current git branch
|
|
```bash
|
|
|
```bash
|
|
git branch -m <old_branch> <new_branch>
|
|
|
git branch --show-current
|
|
```
|
|
|
```
|
|
|
|
|
|
|
#### \--show-current
|
|
|
## cherry-pick
|
|
|
|
|
Re-apply a given commit on the current branch. Useful when you want a feature from another branch without merging everything.
|
|
will output your current git branch
|
|
|
```bash
|
|
|
|
|
git cherry-pick <commit_hash>
|
|
```bash
|
|
|
```
|
|
git branch --show-current
|
|
|
|
|
```
|
|
|
## clone
|
|
|
|
|
clone a repository
|
|
## cherry-pick
|
|
|
```bash
|
|
|
|
|
git clone <repository> [<dest_dir>]
|
|
Re-apply a given commit on the current branch. Useful when you want a feature from another branch without merging everything.
|
|
|
```
|
|
|
|
|
repository is either ssh or https address of the repo.
|
|
```bash
|
|
|
|
|
git cherry-pick <commit_hash>
|
|
|
## 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".
|
|
|
|
|
|
|
## clone
|
|
|
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.
|
|
clone a repository
|
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git commit
|
|
git clone <repository> [<dest_dir>]
|
|
|
```
|
|
```
|
|
|
will open a text edition in the terminal where you be able to create the message
|
|
|
|
|
|
|
repository is either ssh or https address of the repo.
|
|
|
## diff
|
|
|
|
|
shows you differences between current branch and given branch.
|
|
## commit
|
|
|
```bash
|
|
|
|
|
git diff
|
|
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".
|
|
|
```
|
|
|
|
|
will show you differences between your local branch and `origin`.
|
|
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.
|
|
|
|
|
|
|
|
### args
|
|
```bash
|
|
|
#### `- p <commit>`
|
|
git commit
|
|
|
Will show differences between current branch and given `<COMMIT>`
|
|
```
|
|
|
```bash
|
|
|
|
|
git diff -p <COMMIT>
|
|
will open a text edition in the terminal where you be able to create the message
|
|
|
```
|
|
|
|
|
#### `--compact-summary`
|
|
## diff
|
|
|
Show a short summary of modifications file-by-file before going in-depth
|
|
|
|
|
|
|
shows you differences between current branch and given branch.
|
|
|
## log
|
|
|
|
|
shows commit history of the current repo.
|
|
```bash
|
|
|
|
|
git diff
|
|
|
#### `-s`
|
|
```
|
|
|
```bash
|
|
|
|
|
git log -S<keyword>
|
|
will show you differences between your local branch and `origin`.
|
|
|
```
|
|
|
|
|
search for `keyword` in commits modifications to show you when it was modified, very useful to know when a function was removed for example.
|
|
### args
|
|
|
|
|
|
|
|
#### `-n`
|
|
#### `- p <commit>`
|
|
|
retrieves the n last commits (here the last 10)
|
|
|
|
|
```bash
|
|
Will show differences between current branch and given `<COMMIT>`
|
|
|
git log -10
|
|
|
|
|
```
|
|
```bash
|
|
|
|
|
git diff -p <COMMIT>
|
|
|
#### `--format=`
|
|
```
|
|
|
retrieves only a part of the commit, either date, hash or message.
|
|
|
|
|
to retrieve the current commit hash.
|
|
#### `--compact-summary`
|
|
|
```bash
|
|
|
|
|
git log -1 --format=format:"%H"
|
|
Show a short summary of modifications file-by-file before going in-depth
|
|
|
```
|
|
|
|
|
### search through your history
|
|
## log
|
|
|
#### using grep
|
|
|
|
|
```bash
|
|
shows commit history of the current repo.
|
|
|
git log --grep=<pattern>
|
|
|
|
|
```
|
|
#### `-s`
|
|
|
#### filtering by date
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git log --since=<date> | --after=<date>
|
|
git log -S<keyword>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
### fancy log with graph
|
|
search for `keyword` in commits modifications to show you when it was modified, very useful to know when a function was removed for example.
|
|
|
```bash
|
|
|
|
|
git log --graph --oneline --decorate
|
|
#### `-n`
|
|
|
```
|
|
|
|
|
> [!tip] pretty printing log
|
|
retrieves the n last commits (here the last 10)
|
|
|
> there are tons of nice formatting for commits of git log, look for them in `git log --help`
|
|
|
|
|
|
|
```bash
|
|
|
|
|
git log -10
|
|
|
---
|
|
```
|
|
|
|
|
|
|
|
### args
|
|
#### `--format=`
|
|
|
#### `-m`
|
|
|
|
|
Most common use of `git commit` is to directly include the message with the `-m` flag that stands for **message**
|
|
retrieves only a part of the commit, either date, hash or message. to retrieve the current commit hash.
|
|
|
```
|
|
|
|
|
git commit -m "message"
|
|
```bash
|
|
|
```
|
|
git log -1 --format=format:"%H"
|
|
|
|
|
```
|
|
|
#### `-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.
|
|
### search through your history
|
|
|
```bash
|
|
|
|
|
git commit -am "message"
|
|
#### using grep
|
|
|
```
|
|
|
|
|
|
|
```bash
|
|
|
### `--allow-empty`
|
|
git log --grep=<pattern>
|
|
|
Will allow an empty commit to be done. Useful to trigger a pipeline.
|
|
```
|
|
|
```bash
|
|
|
|
|
git commit --allow-empty -m "chore : trigger CI"
|
|
#### filtering by date
|
|
|
```
|
|
|
|
|
|
|
```bash
|
|
|
#### `--amend`
|
|
git log --since=<date> | --after=<date>
|
|
|
allow you to modify stuff on the last commit that hasn't been pushed
|
|
```
|
|
|
- here we modify the message of the previous commit
|
|
|
|
|
```bash
|
|
### fancy log with graph
|
|
|
git commit -m "yeeet"
|
|
|
|
|
git commit --amend -m "yeet"
|
|
```bash
|
|
|
```
|
|
git log --graph --oneline --decorate
|
|
|
- here we add a file to the previous without modifying the message
|
|
```
|
|
|
```bash
|
|
|
|
|
git commit -m "yeeet"
|
|
> \[!tip\] pretty printing log there are tons of nice formatting for commits of git log, look for them in `git log --help`
|
|
|
git add somefile.🔥
|
|
|
|
|
git commit --amend --no-edit
|
|
---
|
|
|
```
|
|
|
|
|
|
|
### args
|
|
|
#### `--squash`
|
|
|
|
|
Will indicate that the given commit will be squashed if you call the follwing
|
|
#### `-m`
|
|
|
```bash
|
|
|
|
|
git rebase -i --autosquash <hash>
|
|
Most common use of `git commit` is to directly include the message with the `-m` flag that stands for **message**
|
|
|
```
|
|
|
|
|
|
|
```
|
|
|
#### `--fixup`
|
|
git commit -m "message"
|
|
|
same as above but with the fixup command
|
|
```
|
|
|
|
|
|
|
|
## merge
|
|
#### `-a`
|
|
|
Action of bringing modifications of modifications brought in a branch to another branch.
|
|
|
|
|
|
|
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.
|
|
|
The following command will apply commits of branch_B in branch_A in 1 commit
|
|
|
|
|
```bash
|
|
```bash
|
|
|
# in branch_A
|
|
git commit -am "message"
|
|
|
git merge <branch_B>
|
|
```
|
|
|
```
|
|
|
|
|
|
|
### `--allow-empty`
|
|
|
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.
|
|
|
|
|
|
|
Will allow an empty commit to be done. Useful to trigger a pipeline.
|
|
|
|
|
|
|
|
{width=300}
|
|
```bash
|
|
|
|
|
git commit --allow-empty -m "chore : trigger CI"
|
|
|
---
|
|
```
|
|
|
|
|
|
|
|
## mv
|
|
#### `--amend`
|
|
|
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."
|
|
allow you to modify stuff on the last commit that hasn't been pushed
|
|
|
```bash
|
|
|
|
|
git mv <old_path_file> <new_path_file>
|
|
- here we modify the message of the previous commit
|
|
|
```
|
|
|
|
|
|
|
```bash
|
|
|
---
|
|
git commit -m "yeeet"
|
|
|
|
|
git commit --amend -m "yeet"
|
|
|
## push
|
|
```
|
|
|
pushes commited modifications to origin branch. Base usage is
|
|
|
|
|
```bash
|
|
- here we add a file to the previous without modifying the message
|
|
|
git push
|
|
|
|
|
```
|
|
```bash
|
|
|
which is actually sugar syntax for
|
|
git commit -m "yeeet"
|
|
|
```
|
|
git add somefile.🔥
|
|
|
git push <remote> <current_branch>
|
|
git commit --amend --no-edit
|
|
|
```
|
|
```
|
|
|
### `--set-upstream`/`-u`
|
|
|
|
|
Sets the branch given as arg as the reference branch to follow.
|
|
#### `--squash`
|
|
|
|
|
|
|
|
---
|
|
Will indicate that the given commit will be squashed if you call the follwing
|
|
|
|
|
|
|
|
## rebase
|
|
```bash
|
|
|
allows you to edit your git history to make it clearer before merging
|
|
git rebase -i --autosquash <hash>
|
|
|
```bash
|
|
```
|
|
|
git rebase -i <hash>
|
|
|
|
|
```
|
|
#### `--fixup`
|
|
|
⚠ since you edit your history you will need to force push afterward.
|
|
|
|
|
### tutorial (fr)
|
|
same as above but with the fixup command
|
|
|
https://www.youtube.com/watch?v=A_jreWjCl4s
|
|
|
|
|
|
|
## merge
|
|
|
### `--autosquash`
|
|
|
|
|
automatically squashes commits that you previously marked with `--squash` while comitting
|
|
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
|
|
|
## 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.
|
|
```bash
|
|
|
### `add <remote_name> <url>`
|
|
# in branch_A
|
|
|
If you are working in a fork and want to have both origins
|
|
git merge <branch_B>
|
|
|
```bash
|
|
```
|
|
|
git remote add fork <fork_url>
|
|
|
|
|
```
|
|
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.
|
|
|
### `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.
|
|
{width="300"}
|
|
|
```bash
|
|
|
|
|
git remote set-url origin <new_url>
|
|
---
|
|
|
```
|
|
|
|
|
### `remove <remote_name>`
|
|
## mv
|
|
|
self-explanatory
|
|
|
|
|
```bash
|
|
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."
|
|
|
git remote remove <remote_name>
|
|
|
|
|
```
|
|
```bash
|
|
|
|
|
git mv <old_path_file> <new_path_file>
|
|
|
---
|
|
```
|
|
|
## restore
|
|
|
|
|
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.
|
|
## push
|
|
|
```bash
|
|
|
|
|
git restore <file/folder/path>
|
|
pushes commited modifications to origin branch. Base usage is
|
|
|
```
|
|
|
|
|
|
|
```bash
|
|
|
### `--staged`
|
|
git push
|
|
|
|
|
```
|
|
|
```bash
|
|
|
|
|
restore --staged <path>
|
|
which is actually sugar syntax for
|
|
|
```
|
|
|
|
|
will remove given file of the list of (added)[#add] files.
|
|
```
|
|
|
|
|
git push <remote> <current_branch>
|
|
|
---
|
|
```
|
|
|
|
|
|
|
|
## revert
|
|
### `--set-upstream`/`-u`
|
|
|
Reverts the modifications of a specific commit, often paired with [cherry pick](#Cherry pick).
|
|
|
|
|
```bash
|
|
Sets the branch given as arg as the reference branch to follow.
|
|
|
git revert <commit_hash>
|
|
|
|
|
```
|
|
---
|
|
|
### `-m <parent_number>` `--mainline <parent_number>`
|
|
|
|
|
|
|
## rebase
|
|
|
Usually you cannot cherry-pick a merge because **you do not know which side of the merge** should be considered the mainline.
|
|
|
|
|
|
|
### `-i`
|
|
|
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.
|
|
|
|
|
|
|
allows you to edit your git history to make it clearer before merging
|
|
|
---
|
|
|
|
|
## show
|
|
```bash
|
|
|
Show what the given commit has modified
|
|
git rebase -i <hash>
|
|
|
```bash
|
|
```
|
|
|
git show <commit_hash>
|
|
|
|
|
```
|
|
:warning: since you edit your history you will need to force push afterward.
|
|
|
|
|
|
|
|
|
|
### tutorial
|
|
|
---
|
|
|
|
|
|
|
https://www.youtube.com/watch?v=rt9ZOVciJm8
|
|
|
## 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 :
|
|
### `--autosquash`
|
|
|
```bash
|
|
|
|
|
git **stash**
|
|
automatically squashes commits that you previously marked with `--squash` while comitting
|
|
|
```
|
|
|
|
|
|
|
---
|
|
|
## status
|
|
|
|
|
Once you have set up your repo in local you can start modifying it.
|
|
## remote
|
|
|
```bash
|
|
|
|
|
git status
|
|
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.
|
|
|
```
|
|
|
|
|
will show you what files you have modified / [added](#add) and not [commited](#commit).
|
|
### `add <remote_name> <url>`
|
|
|
|
|
|
|
|
### save
|
|
If you are working in a fork and want to have both origins
|
|
|
allows you to give a specific name to the stash you are creating :
|
|
|
|
|
```bash
|
|
```bash
|
|
|
git stash save <killer_feature_name>
|
|
git remote add fork <fork_url>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
### pop
|
|
### `set-url <remote name> <url>`
|
|
|
to keep uncommited modifications on the side in a stack, you you stash something else it'll be stacked upon the first one.
|
|
|
|
|
```bash
|
|
Changes the url of a remote name. for example If you just moved the repository in your gitlab you might need to use.
|
|
|
git stash pop
|
|
|
|
|
```
|
|
```bash
|
|
|
pop the stack of changes to add them to the code.
|
|
git remote set-url origin <new_url>
|
|
|
|
|
```
|
|
|
### apply
|
|
|
|
|
to apply a specific stash on your code
|
|
### `remove <remote_name>`
|
|
|
```
|
|
|
|
|
git stash apply <killer_feature_name>
|
|
self-explanatory
|
|
|
```
|
|
|
|
|
|
|
```bash
|
|
|
### display stash
|
|
git remote remove <remote_name>
|
|
|
to display the stashes use :
|
|
```
|
|
|
```bash
|
|
|
|
|
git stash list
|
|
---
|
|
|
```
|
|
|
|
|
To show what files are modified by each stash use :
|
|
## restore
|
|
|
```bash
|
|
|
|
|
git stash show
|
|
Undoes all modifications done to given file/folder/path done since last commit
|
|
|
```
|
|
|
|
|
To show how files are modified by a given stash :
|
|
> :warning: This command cannot be undone If you git restored files that contained valuable modifications you will have to redo it by hand.
|
|
|
```bash
|
|
|
|
|
git stash show -p
|
|
```bash
|
|
|
```
|
|
git restore <file/folder/path>
|
|
|
|
|
```
|
|
|
## switch
|
|
|
|
|
change current active branch
|
|
### `--staged`
|
|
|
```bash
|
|
|
|
|
git switch <branch>
|
|
```bash
|
|
|
```
|
|
restore --staged <path>
|
|
|
|
|
```
|
|
|
## checkout
|
|
|
|
|
Like [switch](switch) but not on a [commit](#commit)], allow you to modify and apply modification on chosen branch afterward
|
|
will remove given file of the list of (added)\[#add\] files.
|
|
|
```bash
|
|
|
|
|
git checkout <commit_hash>
|
|
---
|
|
|
```
|
|
|
|
|
|
|
## revert
|
|
|
---
|
|
|
|
|
# 🤔 How to & Troubleshootings 🏹
|
|
Reverts the modifications of a specific commit, often paired with [cherry pick](#Cherry%20pick).
|
|
|
|
|
|
|
|
## REVERT a commited and pushed commit
|
|
```bash
|
|
|
Very useful if you pushed something you didn't want : simply undo what the commit has done.
|
|
git revert <commit_hash>
|
|
|
```bash
|
|
```
|
|
|
git revert <COMMIT_HASH>
|
|
|
|
|
```
|
|
### `-m <parent_number>` `--mainline <parent_number>`
|
|
|
|
|
|
|
|
> [!info] what does it reverts ?
|
|
Usually you cannot cherry-pick a merge because **you do not know which side of the merge** should be considered the mainline.
|
|
|
> this command only reverts the precise commit that has been done, not all the commits made since.
|
|
|
|
|
|
|
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.
|
|
|
## DELETE a commited and pushed git
|
|
|
|
|
|
|
---
|
|
|
> [!danger] ⚠ VERY DANGEROUS ⚠
|
|
|
|
|
> The following command DESTROYS EVERY COMMITS PUSHED SINCE THE ONE YOU WANT TO DELETE
|
|
## show
|
|
|
> - always prefer the [revert](#revert) command.
|
|
|
|
|
> - NEVER do it on `main` branch
|
|
Show what the given commit has modified
|
|
|
> - ALWAYS
|
|
|
|
|
> - triple check your commit hash before calling `git push -f`
|
|
```bash
|
|
|
> - triple check the commits done before
|
|
git show <commit_hash>
|
|
|
> - AVOID doing that unless necessary just use the revert commit process as this command can potentially destroy whole projects
|
|
```
|
|
|
|
|
|
|
|
```bash
|
|
---
|
|
|
git switch <branch>
|
|
|
|
|
git reset --hard <commit hash>
|
|
## stash
|
|
|
git branch # to check if you are in the good branch
|
|
|
|
|
git push -f
|
|
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
|
|
|
## Re-apply a `commit`
|
|
git **stash**
|
|
|
[git cherry-pick](#cherry-pick)
|
|
```
|
|
|
|
|
|
|
|
## undo an unpushed commit
|
|
## status
|
|
|
```bash
|
|
|
|
|
git reset --soft HEAD~1
|
|
Once you have set up your repo in local you can start modifying it.
|
|
|
```
|
|
|
|
|
|
|
```bash
|
|
|
## .gitignore is ignored and tracks files mentionned in it
|
|
git status
|
|
|
```bash
|
|
```
|
|
|
git rm -r --cached .
|
|
|
|
|
git add .
|
|
will show you what files you have modified / [added](#add) and not [commited](#commit).
|
|
|
git commit -m "fix : fixed untracked files"
|
|
|
|
|
```
|
|
### save
|
|
|
|
|
|
|
|
## get a log of the exchanges between git & the server
|
|
allows you to give a specific name to the stash you are creating :
|
|
|
`<command>` can either be fetch pull push or checkout
|
|
|
|
|
```bash
|
|
```bash
|
|
|
GIT_SSH_COMMAND="ssh -vv" git <command>
|
|
git stash save <killer_feature_name>
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## Run a git command for a repo outside of it
|
|
### pop
|
|
|
```bash
|
|
|
|
|
git -C <path_to_repo> <git_command>
|
|
to keep uncommited modifications on the side in a stack, you you stash something else it'll be stacked upon the first one.
|
|
|
```
|
|
|
|
|
|
|
```bash
|
|
|
## Change the author of a commit
|
|
git stash pop
|
|
|
Use [git rebase](#rebase) if you need to apply that to multiple commits.
|
|
```
|
|
|
```bash
|
|
|
|
|
git rebase -i <hash>
|
|
pop the stack of changes to add them to the code.
|
|
|
```
|
|
|
|
|
This will open your default text editor (defined with the environment variable $EDITOR).
|
|
### apply
|
|
|
Then put `edit` in front of the commit you want to modify.
|
|
|
|
|
```bash
|
|
to apply a specific stash on your code
|
|
|
edit <hash>
|
|
|
|
|
```
|
|
```
|
|
|
and quit the editor. rebase will put you on the wanted commit and you will be able to modify its author with
|
|
git stash apply <killer_feature_name>
|
|
|
```bash
|
|
```
|
|
|
git commit --amend --author="your name <your@email.org>" --no-edit
|
|
|
|
|
git rebase --continue
|
|
### display stash
|
|
|
```
|
|
|
|
|
> :warning: for once the `<>` put above are not placeholders for arguments but needed in the syntax
|
|
to display the stashes use :
|
|
|
>
|
|
|
|
|
|
|
```bash
|
|
|
# Sources
|
|
git stash list
|
|
|
https://www.youtube.com/watch?v=ecK3EnyGD8o |
|
```
|
|
|
|
|
|
|
|
To show what files are modified by each stash use :
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git stash show
|
|
|
|
```
|
|
|
|
|
|
|
|
To show how files are modified by a given stash :
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git stash show -p
|
|
|
|
```
|
|
|
|
|
|
|
|
## switch
|
|
|
|
|
|
|
|
change current active branch
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git switch <branch>
|
|
|
|
```
|
|
|
|
|
|
|
|
## checkout
|
|
|
|
|
|
|
|
Like [switch](switch) but not on a [commit](#commit)\], allow you to modify and apply modification on chosen branch afterward
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git checkout <commit_hash>
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
# :thinking: How to & Troubleshootings :bow_and_arrow:
|
|
|
|
|
|
|
|
## REVERT a commited and pushed commit
|
|
|
|
|
|
|
|
Very useful if you pushed something you didn't want : simply undo what the commit has done.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git revert <COMMIT_HASH>
|
|
|
|
```
|
|
|
|
|
|
|
|
> \[!info\] what does it reverts ? this command only reverts the precise commit that has been done, not all the commits made since.
|
|
|
|
|
|
|
|
## DELETE a commited and pushed git
|
|
|
|
|
|
|
|
> \[!danger\] :warning: VERY DANGEROUS :warning: The following command DESTROYS EVERY COMMITS PUSHED SINCE THE ONE YOU WANT TO DELETE
|
|
|
|
>
|
|
|
|
> - always prefer the [revert](#revert) command.
|
|
|
|
> - NEVER do it on `main` branch
|
|
|
|
> - ALWAYS
|
|
|
|
> - triple check your commit hash before calling `git push -f`
|
|
|
|
> - triple check the commits done before
|
|
|
|
> - AVOID doing that unless necessary just use the revert commit process as this command can potentially destroy whole projects
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git switch <branch>
|
|
|
|
git reset --hard <commit hash>
|
|
|
|
git branch # to check if you are in the good branch
|
|
|
|
git push -f
|
|
|
|
```
|
|
|
|
|
|
|
|
## Re-apply a `commit`
|
|
|
|
|
|
|
|
[git cherry-pick](#cherry-pick)
|
|
|
|
|
|
|
|
## undo an unpushed commit
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git reset --soft HEAD~1
|
|
|
|
```
|
|
|
|
|
|
|
|
## .gitignore is ignored and tracks files mentionned in it
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git rm -r --cached .
|
|
|
|
git add .
|
|
|
|
git commit -m "fix : fixed untracked files"
|
|
|
|
```
|
|
|
|
|
|
|
|
## get a log of the exchanges between git & the server
|
|
|
|
|
|
|
|
`<command>` can either be fetch pull push or checkout
|
|
|
|
|
|
|
|
```bash
|
|
|
|
GIT_SSH_COMMAND="ssh -vv" git <command>
|
|
|
|
```
|
|
|
|
|
|
|
|
## Run a git command for a repo outside of it
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git -C <path_to_repo> <git_command>
|
|
|
|
```
|
|
|
|
|
|
|
|
## Change the author of a commit
|
|
|
|
|
|
|
|
Use [git rebase](#rebase) if you need to apply that to multiple commits.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
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.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
edit <hash>
|
|
|
|
```
|
|
|
|
|
|
|
|
and quit the editor. rebase will put you on the wanted commit and you will be able to modify its author with
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git commit --amend --author="your name <your@email.org>" --no-edit
|
|
|
|
git rebase --continue
|
|
|
|
```
|
|
|
|
|
|
|
|
> :warning: for once the `<>` put above are not placeholders for arguments but needed in the syntax
|
|
|
|
|
|
|
|
# Sources
|
|
|
|
|
|
|
|
https://www.youtube.com/watch?v=ecK3EnyGD8o |
|
|
|
\ No newline at end of file |