|
|
|
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.
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
# commands
|
|
|
|
## add
|
|
|
|
|
|
|
|
To prepare a [commit](#commit) : each added file will be in your next [commit](#commit)
|
|
|
|
```bash
|
|
|
|
git add <file/folder>
|
|
|
|
```
|
|
|
|
To remove an added file use the (restore --staged <path>)[#args] command .
|
|
|
|
|
|
|
|
## bisect
|
|
|
|
allow you to research when a bug has occurred within your history : if a [commit](#commit) is good or bad.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git bisect start
|
|
|
|
git bisect good
|
|
|
|
git bisect bad
|
|
|
|
git bisect good
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## branch
|
|
|
|
To handle git branches, be it listing, renaming, copying, deletion.
|
|
|
|
|
|
|
|
Shows repo branches:
|
|
|
|
```bash
|
|
|
|
git branch
|
|
|
|
```
|
|
|
|
|
|
|
|
#### -m / --move
|
|
|
|
rename a branch (pro tip : `-M` to force)
|
|
|
|
```bash
|
|
|
|
git branch -m <old_branch> <new_branch>
|
|
|
|
```
|
|
|
|
|
|
|
|
#### --show-current
|
|
|
|
will output your current git branch
|
|
|
|
```bash
|
|
|
|
git branch --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.
|
|
|
|
```bash
|
|
|
|
git cherry-pick <commit_hash>
|
|
|
|
```
|
|
|
|
|
|
|
|
## clone
|
|
|
|
clone a repository
|
|
|
|
```bash
|
|
|
|
git clone <repository> [<dest_dir>]
|
|
|
|
```
|
|
|
|
repository is either ssh or https address of the repo.
|
|
|
|
|
|
|
|
## 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 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.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git commit
|
|
|
|
```
|
|
|
|
will open a text edition in the terminal where you be able to create the message
|
|
|
|
|
|
|
|
## diff
|
|
|
|
shows you differences between current branch and given branch.
|
|
|
|
```bash
|
|
|
|
git diff
|
|
|
|
```
|
|
|
|
will show you differences between your local branch and `origin`.
|
|
|
|
|
|
|
|
### args
|
|
|
|
#### `- p <commit>`
|
|
|
|
Will show differences between current branch and given `<COMMIT>`
|
|
|
|
```bash
|
|
|
|
git diff -p <COMMIT>
|
|
|
|
```
|
|
|
|
#### `--compact-summary`
|
|
|
|
Show a short summary of modifications file-by-file before going in-depth
|
|
|
|
|
|
|
|
## log
|
|
|
|
shows commit history of the current repo.
|
|
|
|
|
|
|
|
#### `-s`
|
|
|
|
```bash
|
|
|
|
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.
|
|
|
|
|
|
|
|
#### `-n`
|
|
|
|
retrieves the n last commits (here the last 10)
|
|
|
|
```bash
|
|
|
|
git log -10
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `--format=`
|
|
|
|
retrieves only a part of the commit, either date, hash or message.
|
|
|
|
to retrieve the current commit hash.
|
|
|
|
```bash
|
|
|
|
git log -1 --format=format:"%H"
|
|
|
|
```
|
|
|
|
### search through your history
|
|
|
|
#### using grep
|
|
|
|
```bash
|
|
|
|
git log --grep=<pattern>
|
|
|
|
```
|
|
|
|
#### filtering by date
|
|
|
|
```bash
|
|
|
|
git log --since=<date> | --after=<date>
|
|
|
|
```
|
|
|
|
|
|
|
|
### fancy log with graph
|
|
|
|
```bash
|
|
|
|
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`
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
### args
|
|
|
|
#### `-m`
|
|
|
|
Most common use of `git commit` is to directly include the message with the `-m` flag that stands for **message**
|
|
|
|
```
|
|
|
|
git commit -m "message"
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `-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.
|
|
|
|
```bash
|
|
|
|
git commit -am "message"
|
|
|
|
```
|
|
|
|
|
|
|
|
### `--allow-empty`
|
|
|
|
Will allow an empty commit to be done. Useful to trigger a pipeline.
|
|
|
|
```bash
|
|
|
|
git commit --allow-empty -m "chore : trigger CI"
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `--amend`
|
|
|
|
allow you to modify stuff on the last commit that hasn't been pushed
|
|
|
|
- here we modify the message of the previous commit
|
|
|
|
```bash
|
|
|
|
git commit -m "yeeet"
|
|
|
|
git commit --amend -m "yeet"
|
|
|
|
```
|
|
|
|
- here we add a file to the previous without modifying the message
|
|
|
|
```bash
|
|
|
|
git commit -m "yeeet"
|
|
|
|
git add somefile.🔥
|
|
|
|
git commit --amend --no-edit
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `--squash`
|
|
|
|
Will indicate that the given commit will be squashed if you call the follwing
|
|
|
|
```bash
|
|
|
|
git rebase -i --autosquash <hash>
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `--fixup`
|
|
|
|
same as above but with the fixup command
|
|
|
|
|
|
|
|
## merge
|
|
|
|
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
|
|
|
|
```bash
|
|
|
|
# in branch_A
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
{width=300}
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## 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."
|
|
|
|
```bash
|
|
|
|
git mv <old_path_file> <new_path_file>
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## push
|
|
|
|
pushes commited modifications to origin branch. Base usage is
|
|
|
|
```bash
|
|
|
|
git push
|
|
|
|
```
|
|
|
|
which is actually sugar syntax for
|
|
|
|
```
|
|
|
|
git push <remote> <current_branch>
|
|
|
|
```
|
|
|
|
### `--set-upstream`/`-u`
|
|
|
|
Sets the branch given as arg as the reference branch to follow.
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## rebase
|
|
|
|
allows you to edit your git history to make it clearer before merging
|
|
|
|
```bash
|
|
|
|
git rebase -i <hash>
|
|
|
|
```
|
|
|
|
⚠ since you edit your history you will need to force push afterward.
|
|
|
|
### tutorial (fr)
|
|
|
|
https://www.youtube.com/watch?v=A_jreWjCl4s
|
|
|
|
|
|
|
|
### `--autosquash`
|
|
|
|
automatically squashes commits that you previously marked with `--squash` while comitting
|
|
|
|
|
|
|
|
---
|
|
|
|
## 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.
|
|
|
|
### `add <remote_name> <url>`
|
|
|
|
If you are working in a fork and want to have both origins
|
|
|
|
```bash
|
|
|
|
git remote add fork <fork_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.
|
|
|
|
```bash
|
|
|
|
git remote set-url origin <new_url>
|
|
|
|
```
|
|
|
|
### `remove <remote_name>`
|
|
|
|
self-explanatory
|
|
|
|
```bash
|
|
|
|
git remote remove <remote_name>
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
## 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.
|
|
|
|
```bash
|
|
|
|
git restore <file/folder/path>
|
|
|
|
```
|
|
|
|
|
|
|
|
### `--staged`
|
|
|
|
|
|
|
|
```bash
|
|
|
|
restore --staged <path>
|
|
|
|
```
|
|
|
|
will remove given file of the list of (added)[#add] files.
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## revert
|
|
|
|
Reverts the modifications of a specific commit, often paired with [cherry pick](#Cherry pick).
|
|
|
|
```bash
|
|
|
|
git revert <commit_hash>
|
|
|
|
```
|
|
|
|
### `-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.
|
|
|
|
|
|
|
|
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 what the given commit has modified
|
|
|
|
```bash
|
|
|
|
git show <commit_hash>
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## 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 :
|
|
|
|
```bash
|
|
|
|
git **stash**
|
|
|
|
```
|
|
|
|
|
|
|
|
## status
|
|
|
|
Once you have set up your repo in local you can start modifying it.
|
|
|
|
```bash
|
|
|
|
git status
|
|
|
|
```
|
|
|
|
will show you what files you have modified / [added](#add) and not [commited](#commit).
|
|
|
|
|
|
|
|
### save
|
|
|
|
allows you to give a specific name to the stash you are creating :
|
|
|
|
```bash
|
|
|
|
git stash save <killer_feature_name>
|
|
|
|
```
|
|
|
|
|
|
|
|
### pop
|
|
|
|
to keep uncommited modifications on the side in a stack, you you stash something else it'll be stacked upon the first one.
|
|
|
|
```bash
|
|
|
|
git stash pop
|
|
|
|
```
|
|
|
|
pop the stack of changes to add them to the code.
|
|
|
|
|
|
|
|
### apply
|
|
|
|
to apply a specific stash on your code
|
|
|
|
```
|
|
|
|
git stash apply <killer_feature_name>
|
|
|
|
```
|
|
|
|
|
|
|
|
### display stash
|
|
|
|
to display the stashes use :
|
|
|
|
```bash
|
|
|
|
git stash list
|
|
|
|
```
|
|
|
|
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>
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
# 🤔 How to & Troubleshootings 🏹
|
|
|
|
|
|
|
|
## 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] ⚠ VERY DANGEROUS ⚠
|
|
|
|
> 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 |
|
|
|
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.
|
|
|
|
|
|
|
|
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_]]
|
|
|
|
|
|
|
|
# commands
|
|
|
|
|
|
|
|
## add
|
|
|
|
|
|
|
|
To prepare a [commit](#commit) : each added file will be in your next [commit](#commit)
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git add <file/folder>
|
|
|
|
```
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git bisect start
|
|
|
|
git bisect good
|
|
|
|
git bisect bad
|
|
|
|
git bisect good
|
|
|
|
```
|
|
|
|
|
|
|
|
## branch
|
|
|
|
|
|
|
|
To handle git branches, be it listing, renaming, copying, deletion.
|
|
|
|
|
|
|
|
Shows repo branches:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git branch
|
|
|
|
```
|
|
|
|
|
|
|
|
#### \-m / --move
|
|
|
|
|
|
|
|
rename a branch (pro tip : `-M` to force)
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git branch -m <old_branch> <new_branch>
|
|
|
|
```
|
|
|
|
|
|
|
|
#### \--show-current
|
|
|
|
|
|
|
|
will output your current git branch
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git branch --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.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git cherry-pick <commit_hash>
|
|
|
|
```
|
|
|
|
|
|
|
|
## clone
|
|
|
|
|
|
|
|
clone a repository
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git clone <repository> [<dest_dir>]
|
|
|
|
```
|
|
|
|
|
|
|
|
repository is either ssh or https address of the repo.
|
|
|
|
|
|
|
|
## 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 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.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git commit
|
|
|
|
```
|
|
|
|
|
|
|
|
will open a text edition in the terminal where you be able to create the message
|
|
|
|
|
|
|
|
## diff
|
|
|
|
|
|
|
|
shows you differences between current branch and given branch.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git diff
|
|
|
|
```
|
|
|
|
|
|
|
|
will show you differences between your local branch and `origin`.
|
|
|
|
|
|
|
|
### args
|
|
|
|
|
|
|
|
#### `- p <commit>`
|
|
|
|
|
|
|
|
Will show differences between current branch and given `<COMMIT>`
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git diff -p <COMMIT>
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `--compact-summary`
|
|
|
|
|
|
|
|
Show a short summary of modifications file-by-file before going in-depth
|
|
|
|
|
|
|
|
## log
|
|
|
|
|
|
|
|
shows commit history of the current repo.
|
|
|
|
|
|
|
|
#### `-s`
|
|
|
|
|
|
|
|
```bash
|
|
|
|
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.
|
|
|
|
|
|
|
|
#### `-n`
|
|
|
|
|
|
|
|
retrieves the n last commits (here the last 10)
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git log -10
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `--format=`
|
|
|
|
|
|
|
|
retrieves only a part of the commit, either date, hash or message. to retrieve the current commit hash.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git log -1 --format=format:"%H"
|
|
|
|
```
|
|
|
|
|
|
|
|
### search through your history
|
|
|
|
|
|
|
|
#### using grep
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git log --grep=<pattern>
|
|
|
|
```
|
|
|
|
|
|
|
|
#### filtering by date
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git log --since=<date> | --after=<date>
|
|
|
|
```
|
|
|
|
|
|
|
|
### fancy log with graph
|
|
|
|
|
|
|
|
```bash
|
|
|
|
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`
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
### args
|
|
|
|
|
|
|
|
#### `-m`
|
|
|
|
|
|
|
|
Most common use of `git commit` is to directly include the message with the `-m` flag that stands for **message**
|
|
|
|
|
|
|
|
```
|
|
|
|
git commit -m "message"
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `-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.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git commit -am "message"
|
|
|
|
```
|
|
|
|
|
|
|
|
### `--allow-empty`
|
|
|
|
|
|
|
|
Will allow an empty commit to be done. Useful to trigger a pipeline.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git commit --allow-empty -m "chore : trigger CI"
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `--amend`
|
|
|
|
|
|
|
|
allow you to modify stuff on the last commit that hasn't been pushed
|
|
|
|
|
|
|
|
- here we modify the message of the previous commit
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git commit -m "yeeet"
|
|
|
|
git commit --amend -m "yeet"
|
|
|
|
```
|
|
|
|
|
|
|
|
- here we add a file to the previous without modifying the message
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git commit -m "yeeet"
|
|
|
|
git add somefile.🔥
|
|
|
|
git commit --amend --no-edit
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `--squash`
|
|
|
|
|
|
|
|
Will indicate that the given commit will be squashed if you call the follwing
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git rebase -i --autosquash <hash>
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `--fixup`
|
|
|
|
|
|
|
|
same as above but with the fixup command
|
|
|
|
|
|
|
|
## merge
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
```bash
|
|
|
|
# in branch_A
|
|
|
|
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.
|
|
|
|
|
|
|
|
{width="300"}
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## 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."
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git mv <old_path_file> <new_path_file>
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## push
|
|
|
|
|
|
|
|
pushes commited modifications to origin branch. Base usage is
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git push
|
|
|
|
```
|
|
|
|
|
|
|
|
which is actually sugar syntax for
|
|
|
|
|
|
|
|
```
|
|
|
|
git push <remote> <current_branch>
|
|
|
|
```
|
|
|
|
|
|
|
|
### `--set-upstream`/`-u`
|
|
|
|
|
|
|
|
Sets the branch given as arg as the reference branch to follow.
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## rebase
|
|
|
|
|
|
|
|
### `-i`
|
|
|
|
|
|
|
|
allows you to edit your git history to make it clearer before merging
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git rebase -i <hash>
|
|
|
|
```
|
|
|
|
|
|
|
|
:warning: since you edit your history you will need to force push afterward.
|
|
|
|
|
|
|
|
### tutorial
|
|
|
|
|
|
|
|
https://www.youtube.com/watch?v=rt9ZOVciJm8
|
|
|
|
|
|
|
|
### `--autosquash`
|
|
|
|
|
|
|
|
automatically squashes commits that you previously marked with `--squash` while comitting
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## 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.
|
|
|
|
|
|
|
|
### `add <remote_name> <url>`
|
|
|
|
|
|
|
|
If you are working in a fork and want to have both origins
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git remote add fork <fork_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.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git remote set-url origin <new_url>
|
|
|
|
```
|
|
|
|
|
|
|
|
### `remove <remote_name>`
|
|
|
|
|
|
|
|
self-explanatory
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git remote remove <remote_name>
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## 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.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git restore <file/folder/path>
|
|
|
|
```
|
|
|
|
|
|
|
|
### `--staged`
|
|
|
|
|
|
|
|
```bash
|
|
|
|
restore --staged <path>
|
|
|
|
```
|
|
|
|
|
|
|
|
will remove given file of the list of (added)\[#add\] files.
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## revert
|
|
|
|
|
|
|
|
Reverts the modifications of a specific commit, often paired with [cherry pick](#Cherry%20pick).
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git revert <commit_hash>
|
|
|
|
```
|
|
|
|
|
|
|
|
### `-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.
|
|
|
|
|
|
|
|
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 what the given commit has modified
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git show <commit_hash>
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## 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 :
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git **stash**
|
|
|
|
```
|
|
|
|
|
|
|
|
## status
|
|
|
|
|
|
|
|
Once you have set up your repo in local you can start modifying it.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git status
|
|
|
|
```
|
|
|
|
|
|
|
|
will show you what files you have modified / [added](#add) and not [commited](#commit).
|
|
|
|
|
|
|
|
### save
|
|
|
|
|
|
|
|
allows you to give a specific name to the stash you are creating :
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git stash save <killer_feature_name>
|
|
|
|
```
|
|
|
|
|
|
|
|
### pop
|
|
|
|
|
|
|
|
to keep uncommited modifications on the side in a stack, you you stash something else it'll be stacked upon the first one.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git stash pop
|
|
|
|
```
|
|
|
|
|
|
|
|
pop the stack of changes to add them to the code.
|
|
|
|
|
|
|
|
### apply
|
|
|
|
|
|
|
|
to apply a specific stash on your code
|
|
|
|
|
|
|
|
```
|
|
|
|
git stash apply <killer_feature_name>
|
|
|
|
```
|
|
|
|
|
|
|
|
### display stash
|
|
|
|
|
|
|
|
to display the stashes use :
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git stash list
|
|
|
|
```
|
|
|
|
|
|
|
|
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 |