|
| 1 | +--- |
| 2 | +title: My Git Cheat Sheet |
| 3 | +lead: Useful git commands |
| 4 | +description: Another git cheat sheet like you'll find plenty of, but this one doesn't list basic commands. Instead it focuses on explaining which less-known git commands you will need depending on the situation you find yourself in. |
| 5 | +image: |
| 6 | + src: /goodies/gitcheatsheet.webp |
| 7 | +--- |
| 8 | +You can find many nice git cheat sheets everywhere on the web, but let's just quote GitHub's [git cheat sheet](https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf) which provides you with most useful git commands. That is not my intent, I won't enumerate all basic commands everybody knows and uses everyday, but list some commands that I use less often and that I want to remind myself of, often because I can never remember their exact syntax. |
| 9 | + |
| 10 | +I tried to organize git commands by situation where you can need them and I added the link to the official documentation. |
| 11 | + |
| 12 | +### When you need to add a range of commits into your current branch history |
| 13 | + |
| 14 | +- `git cherry-pick ebe6952^..905e379` |
| 15 | + - pick all the commits from commit `ebe6952` to commit `905e379` |
| 16 | + - add them in your current branch |
| 17 | +- [Doc](https://git-scm.com/docs/git-cherry-pick) |
| 18 | + |
| 19 | +### When you only want to push some of your commits in your branch |
| 20 | + |
| 21 | +- `git push ebe6952:main` where `ebe6952` is the latest commit you want to push |
| 22 | + - push your main branch until the commit `ebe6952` |
| 23 | + - it's useful in caseswhen you have `A -> B -> C -> D -> E` on your local repository and you only want to push `C` and `D` |
| 24 | +- [Doc](https://git-scm.com/docs/git-push#Documentation/git-push.txt-ltrefspecgt82308203) |
| 25 | + |
| 26 | + |
| 27 | +### When you messed up your repository with git commands like rebase and want to recover changes from a commit not in your history anymore |
| 28 | +- `git reflog` |
| 29 | + - give you an history of the references of your HEAD (current active branch) |
| 30 | + - allow you to see on which commit was your HEAD after each git command you did |
| 31 | + - once you have the commit hash you are interested in, you can see what changes were made in this commit or even reset the branch to this commit |
| 32 | +- [Doc](https://git-scm.com/docs/git-reflog) |
| 33 | + |
| 34 | +### Quickly change last commit |
| 35 | + |
| 36 | +```bash |
| 37 | +git add . //(to stage modifications to integrate to commit) |
| 38 | +git commit --amend |
| 39 | +``` |
| 40 | +- `git commit --amend` will open your git editor to allow you to change the commit message |
| 41 | +- [Doc](https://git-scm.com/docs/git-commit) |
| 42 | + |
| 43 | +### When you are working in a feature branch and want to integrate the changes done by your colleagues in the main branch |
| 44 | +```bash |
| 45 | +git checkout dev |
| 46 | +git pull |
| 47 | +git checkout featurebranch |
| 48 | +git rebase dev |
| 49 | +git push --force |
| 50 | +``` |
| 51 | +- This will rewrites your branch history, that is why the "--force" is needed (if you have already pushed your commits that have been rewritten) |
| 52 | +- Check this [post](https://jeffkreeftmeijer.com/git-rebase/) to understand in a schema what happens when reintegrating changes from another branch |
| 53 | +- [Doc](https://git-scm.com/docs/git-rebase) |
| 54 | + |
| 55 | +As a colleague suggested me, if you don't need your local main branch to be up-to-date, you can win a few keystrokes and some time by replacing the above commands by the following commands: |
| 56 | +```bash |
| 57 | +git fetch origin |
| 58 | +git rebase origin/dev |
| 59 | +git push --force |
| 60 | +``` |
| 61 | +It does the same thing than the previous set of commands, but just does not merge the changes of dev on your local dev branch. And as most of the time you don't need to and want to stay on your feature branch, that's easier and quicker to do it this way. |
| 62 | + |
| 63 | +### When you want to have a clean commit history on your branch before creating your pull request |
| 64 | +```bash |
| 65 | +git rebase -i HEAD~3 |
| 66 | +``` |
| 67 | +- Opens up an editor to pick, reword, edit, squash or fixup your last 3 commits |
| 68 | +- Check this [post](https://delicious-insights.com/en/posts/getting-solid-at-git-rebase-vs-merge/#cleaning-up-your-local-history-before-pushing) to deep dive into interactive rebase ... best git command ever ... |
| 69 | +- [Doc](https://git-scm.com/docs/git-rebase) |
| 70 | + |
| 71 | +### When you want to stash your local changes with a specific name you can easily find later |
| 72 | +- `git stash save "myFriendlyStashName"` will save your local changes in a stash with the name "myFriendlyStashName" |
| 73 | +- When you list all you current stash later with `git stash list`, you can easily find its number: |
| 74 | + |
| 75 | +{ .rounded-lg .mx-auto width=600} |
| 76 | + |
| 77 | +- Here we can see that the myFriendlyStashName is the first one and we can pop it with `git stash pop "stash@{0}"` or apply it with `git stash apply "stash@{0}"` |
| 78 | +- [Doc](https://git-scm.com/docs/git-stash#Documentation/git-stash) |
| 79 | + |
| 80 | +### When someone did a `git push --force` on the repository and you want to reset your local repository |
| 81 | +```bash |
| 82 | +git fetch |
| 83 | +git reset origin/master --hard |
| 84 | +``` |
| 85 | +- All your local changes will be erased, if you want to keep them use `--soft` |
| 86 | +- See [this Stackoverflow post](https://stackoverflow.com/questions/9813816/git-pull-after-forced-update) for more information |
| 87 | + |
| 88 | +### When you want to reset a file to its previous version |
| 89 | +- `git reset HEAD^ filename` |
| 90 | +- [Doc](https://git-scm.com/docs/git-reset) |
| 91 | + |
| 92 | +### When you want to "remove" the last commit |
| 93 | +- `git reset --soft HEAD~1` |
| 94 | +- [Doc](https://git-scm.com/docs/git-reset) |
| 95 | + |
| 96 | + |
| 97 | +### When you want to move an existing branch to another commit |
| 98 | +- `git branch -f myBranch ebe6952` |
| 99 | +- [Doc](https://git-scm.com/docs/git-branch) |
| 100 | + |
| 101 | +### Update git when using Windows |
| 102 | +```bash |
| 103 | +git update-git-for-windows |
| 104 | +``` |
| 105 | +- Sometimes, once Git is installed, we forget to update it |
| 106 | +- Doing this command from time to time will allow you to benefit from git last version with new features and corrected issues |
| 107 | + |
| 108 | +### Checkout a PR when using Azure Repos |
| 109 | +```bash |
| 110 | +git config --add remote.origin.fetch +refs/pull/*/merge:refs/remotes/origin/pr/* |
| 111 | +git checkout pr/196 |
| 112 | +``` |
| 113 | +- You only need to do the `git config` command once and each fetch will also fetch the PR |
| 114 | +- You can then directly checkout the PR number you want to review |
| 115 | +- If you use Visual Studio there is a much better way to review PR directly in your IDE by using the Microsoft Extension [Pull Requests for Visual Studio](https://marketplace.visualstudio.com/items?itemName=VSIDEVersionControlMSFT.pr4vs) |
| 116 | + |
| 117 | +### When you want to trigger a CI pipeline you want to test without changing the code |
| 118 | +Instead of adding a space to a file just to have something to commit to trigger a pipeline you are testing, just do : |
| 119 | +```bash |
| 120 | +git commit --allow-empty -m "improve ci" |
| 121 | +``` |
| 122 | +[Doc](https://git-scm.com/docs/git-commit) |
| 123 | + |
| 124 | +### Flag a bash script as executable when using Linux or WSL |
| 125 | +```bash |
| 126 | +git update-index --chmod=+x script.sh |
| 127 | +``` |
| 128 | +- This command will make the script.sh file executable (can be useful when you need your CI like GitHub Actions to execute scripts in your repo) |
| 129 | +- [Doc](https://git-scm.com/docs/git-update-index) |
| 130 | + |
| 131 | +### When you want to reorganize you git repository |
| 132 | +- Imagine you have created your code project at the root of your git repository and want to reorganize it to have a `src`,`build`, `docs` ... folders like suggested in this [.NET project structure](https://gist.github.com/davidfowl/ed7564297c61fe9ab814). |
| 133 | +- You will want to create an src folder and move all your existing files to this `src` folder while keeping the files history. |
| 134 | +- For this you can use the `git mv` command but you have to first remove or move your untracked files and folder (`csproj.user`, `bin/`, `obj/`) and to specify in the command not to move your `src` folder (using `!(src)`). |
| 135 | +```bash |
| 136 | +git mv ./!(src) src/ |
| 137 | +``` |
| 138 | +- [Doc](https://git-scm.com/docs/git-mv) |
| 139 | + |
| 140 | +### When you are bored with writing git commands in your terminal |
| 141 | +- If you are used to vs code and like it, if you are using vs code to write your code, or if you just want something more powerful than Visual Studio git integration : |
| 142 | + - Install the excellent [GitLens extension](https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens) for vs code |
| 143 | + |
| 144 | +- If you prefer standalone git client, have a look at [Fork](https://git-fork.com/) |
| 145 | + |
| 146 | +- Keep using git commands but enhance your terminal with tab completion and repository status in prompt by using |
| 147 | + - [Posh-Git](https://github.com/dahlbyk/posh-git) and [Oh-My-Posh](https://github.com/JanDeDobbeleer/oh-my-posh) when working in PowerShell (see more in [Scott Hanselman blog post](https://www.hanselman.com/blog/HowToMakeAPrettyPromptInWindowsTerminalWithPowerlineNerdFontsCascadiaCodeWSLAndOhmyposh.aspx)) |
| 148 | + - [Oh my zsh](https://github.com/robbyrussell/oh-my-zsh) and its git plugin when working in Bash (Ubuntu / WSL) |
0 commit comments