├── LICENSE ├── README.md └── README.pt.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Gabriel de Jesus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Main Git Commands 2 | 3 |

4 | Stars 5 | 6 | Forks 7 | 8 | Issues 9 | 10 | GitHub license 11 | 12 | 13 | Follow gabrieldejesus 14 | 15 |

16 | 17 |

18 | Main Git Commands 19 |

20 | 21 |

22 | English   |    23 | Português    24 |

25 | 26 | - Starting a Repository 27 | 28 | `git init` 29 | 30 | --- 31 | 32 | - Listing Modified Files 33 | 34 | `git status` 35 | 36 | --- 37 | 38 | - Undoing Changes 39 | 40 | - Unmonitored files 41 | 42 | `git checkout` 43 | 44 | - To delete new files that have not yet been added to the Stage 45 | 46 | `git clean -df` 47 | 48 | - Removing files from the Stage 49 | 50 | `git reset` 51 | 52 | - Undoing the last commit 53 | 54 | `git revert HEAD` 55 | 56 | --- 57 | 58 | - Rename Commit 59 | 60 | `git commit —amend` 61 | 62 | --- 63 | 64 | - Branches 65 | 66 | - Listing local Branches 67 | 68 | `git branch` 69 | 70 | - Also list the branches that are in the remote repository 71 | 72 | `git branch -a` 73 | 74 | - Going to another branch 75 | 76 | `git checkout my-branch` 77 | 78 | - If you add -b a new branch will be created 79 | 80 | `git checkout -b my-new-branch` 81 | 82 | - Excluding branches 83 | 84 | `git branch -d branch-name // normal` 85 | 86 | `git branch -D branch-name // forcing` 87 | 88 | - Renaming branches 89 | 90 | `git branch -m new-branch-name` 91 | 92 | - If you are on a branch and want to rename another one, you must first pass the current name of the branch you want to rename: 93 | 94 | `git branch -m current-name new-name` 95 | 96 | - Orphan Branch 97 | An orphaned branch has this name because it is not linked to the main branch, so 98 | their histories are not shared. 99 | 100 | ```shell 101 | Example: 102 | i --- j --- k <== branch 'my branch' 103 | / 104 | a --- b --- c --- d --- h --- l <== branch 'main' 105 | \ / 106 | and --- f --- g <== branch 'my other branch' 107 | 108 | 1 --- 2 --- 3 --- 4 <== `orphaned 'branch 109 | ``` 110 | 111 | This can be useful when you want to place more than one project in the same 112 | repository. A good example is when you have a project on Github and want to create 113 | a website to publicize your project. The application and the website are different things, 114 | therefore, their codes must be versioned separately. 115 | Having both in the same repository simplifies management. 116 | 117 | To create an orphaned branch just use the command: 118 | 119 | `git checkout --orphan my-branch-orphan` 120 | 121 | --- 122 | 123 | - Viewing Commit History 124 | 125 | `git log` 126 | 127 | - History of one or more files 128 | 129 | `git log -p my-files` 130 | 131 | - Author's history 132 | 133 | `git log --author = name-author` 134 | 135 | - History by date 136 | 137 | `git log --after = "MMM DD YYYY"` 138 | 139 | `git log --before = "MMM DD YYYY"` 140 | 141 | - History Based on a message (commit) 142 | 143 | `git log --grep products` 144 | 145 | With this command we will have the history of commits in which the commit message 146 | has the word “products”. What we go through can be a regular expression, 147 | and we can spend more than one: 148 | 149 | Examples: 150 | 151 | Search for "products" OR "users" 152 | 153 | `git log --grep products --grep users` 154 | 155 | Search for "products" AND "users" 156 | 157 | `git log --grep products --and --grep users` 158 | 159 | --- 160 | 161 | - Display branches in a more readable mode 162 | 163 | It is possible to have the history printed showing the branches of the repository with something 164 | more readable and in color with a command. We will have a result similar to this: 165 | 166 | ```shell 167 | * a102055 (HEAD -> master) commit 8 168 | | * 196d28e (branch-2) commit 7 169 | | * 07e073c commit 3 170 | | * 2b077ca new fie 171 | | | * c1369d8 (branch-3) commit 6 172 | | | * d11bdcd commit 5 173 | | | / 174 | | / | 175 | * | 2b22b75 commit 2 176 | | / 177 | * d5a12b0 .gitignore 178 | * 9535426 - commit 1 179 | ``` 180 | 181 | The command is a little long: 182 | 183 | `git log --all --decorate --oneline --graph` 184 | 185 | To decorate everything we should write after the log. 186 | 187 | ``` 188 | --all 189 | --decorate 190 | --oneline 191 | --graph 192 | ``` 193 | 194 | --- 195 | 196 | - Working on more than one thing without committing 197 | 198 | There may be times when you need to stop what you are doing and start working 199 | in another task. However, it may not be good to commit something that has not yet been 200 | finalized and then return to it, resulting in a commit that will be in the history 201 | but it has code that doesn't work. We can save these changes made 202 | even without having to perform a commit to later work on it again, which is 203 | called a Stash (something like "hide" or "accumulate"). 204 | 205 | By doing this, your repository will return to the state of the last commit, and the changes 206 | previously made will be “hidden”. 207 | 208 | - Saving changes to a Stash 209 | 210 | `git stash` 211 | 212 | - You can still put a name on this stash 213 | 214 | `git stash push -m my-name-stash` 215 | 216 | - Listing Stash 217 | 218 | `git stash list` 219 | 220 | - Recovering modifications 221 | 222 | `git stash apply` 223 | 224 | This will retrieve the most recent stash code. If you want to recover a stash 225 | oldest, just look at the number of the stash that appears when we list it and pass 226 | for the following command: 227 | 228 | `git stash apply stash @ {2}` 229 | 230 | - Removing Stashes 231 | 232 | When we retrieve changes from a stash, it remains saved. To delete it 233 | from the stack, run the drop command next to the name of the stash you want to remove 234 | 235 | `git stash drop stash @ {5}` 236 | 237 | --- 238 | -------------------------------------------------------------------------------- /README.pt.md: -------------------------------------------------------------------------------- 1 | Principais Comandos do Git 2 | 3 |

4 | Stars 5 | 6 | Forks 7 | 8 | Issues 9 | 10 | GitHub license 11 | 12 | 13 | Follow gabrieldejesus 14 | 15 |

16 | 17 |

18 | Principais Comandos do Git 19 |

20 | 21 |

22 | English   |    23 | Português 24 |

25 | 26 | - Iniciando um Repositório 27 | 28 | `git init` 29 | 30 | --- 31 | 32 | - Listando Arquivos Modificados 33 | 34 | `git status` 35 | 36 | --- 37 | 38 | - Desfazendo Alterações 39 | 40 | - Arquivos não monitorados 41 | 42 | `git checkout` 43 | 44 | - Para apagar novos arquivos que ainda não foram adicionados ao Stage 45 | 46 | `git clean -df` 47 | 48 | - Removendo arquivos do Stage 49 | 50 | `git reset` 51 | 52 | - Desfazendo o último commit 53 | 54 | `git revert HEAD` 55 | 56 | --- 57 | 58 | - Renomear Commit 59 | 60 | `git commit —amend` 61 | 62 | --- 63 | 64 | - Branches 65 | 66 | - Listando Branches locais 67 | 68 | `git branch` 69 | 70 | - Listar também as branches que estão no repositório remoto 71 | 72 | `git branch -a` 73 | 74 | - Indo para outra branch 75 | 76 | `git checkout minha-branch` 77 | 78 | - Se você adicionar -b uma nova branch será criada 79 | 80 | `git checkout -b minha-nova-branch` 81 | 82 | - Excluindo branches 83 | 84 | `git branch -d nome-da-branch // normal` 85 | 86 | `git branch -D nome-da-branch // forçando` 87 | 88 | - Renomeando branches 89 | 90 | `git branch -m novo-nome-da-branch` 91 | 92 | - Se você estiver em uma branch e quiser renomear outra, você deve passar primeiro o nome atual da branch que quer renomear: 93 | 94 | `git branch -m nome-atual novo-nome` 95 | 96 | - Branch Órfã 97 | Uma branch órfã tem esse nome porque ela não está ligada à branch principal, então 98 | seus históricos não são compartilhados. 99 | 100 | ```shell 101 | Exemplo: 102 | i---j---k <== branch 'minha branch' 103 | / 104 | a---b---c---d---h---l <== branch 'main' 105 | \ / 106 | e---f---g <== branch 'minha outra branch' 107 | 108 | 1---2---3---4 <== branch `órfã` 109 | ``` 110 | 111 | Isso pode ser útil quando você quer colocar mais de um projeto em um mesmo 112 | repositório. Um bom exemplo é quando você tem um projeto no Github e quer criar 113 | um site para divulgar o seu projeto. A aplicação e o site são coisas diferentes, 114 | portanto os códigos deles devem ser versionados separadamente. 115 | Ter ambos em um mesmo repositório simplifica o gerenciamento. 116 | 117 | Para criar uma branch órfã basta usar o comando: 118 | 119 | `git checkout --orphan minha-branch-orfa` 120 | 121 | --- 122 | 123 | - Visualizando o Histórico de Commits 124 | 125 | `git log` 126 | 127 | - Histórico de um ou mais arquivos 128 | 129 | `git log -p meus-arquivos` 130 | 131 | - Histórico de um autor 132 | 133 | `git log --author=name-author` 134 | 135 | - Histórico por data 136 | 137 | `git log --after="MMM DD YYYY"` 138 | 139 | `git log --before="MMM DD YYYY"` 140 | 141 | - Histórico Baseado em uma mensagem(commit) 142 | 143 | `git log --grep produtos` 144 | 145 | Com esse comando teremos o histórico de commits em que a mensagem do commit 146 | possua a palavra “produtos”. O que passamos pode ser uma expressão regular, 147 | e podemos passar mais de uma: 148 | 149 | Exemplos: 150 | 151 | Procurar por "produtos" OU "usuarios" 152 | 153 | `git log --grep produtos --grep usuarios` 154 | 155 | Procurar por "produtos" E "usuarios" 156 | 157 | `git log --grep produtos --and --grep usuarios` 158 | 159 | --- 160 | 161 | - Exibir branches em um modo mais legível 162 | 163 | É possível mandar imprimir o histórico exibindo as branches do repositório com algo 164 | mais legível e com cores com um comando. Teremos um resultado parecido com esse: 165 | 166 | ```shell 167 | * a102055 (HEAD -> master) commit 8 168 | | * 196d28e (branch-2) commit 7 169 | | * 07e073c commit 3 170 | | * 2b077ca new fie 171 | | | * c1369d8 (branch-3) commit 6 172 | | | * d11bdcd commit 5 173 | | |/ 174 | |/| 175 | * | 2b22b75 commit 2 176 | |/ 177 | * d5a12b0 .gitignore 178 | * 9535426 -- commit 1 179 | ``` 180 | 181 | O comando é um pouco comprido: 182 | 183 | `git log --all --decorate --oneline --graph` 184 | 185 | Para decorar tudo o que devemos escrever depois do log. 186 | 187 | ``` 188 | --all 189 | --decorate 190 | --oneline 191 | --graph 192 | ``` 193 | 194 | --- 195 | 196 | - Trabalhando em mais de uma coisa sem fazer commit 197 | 198 | Pode haver momentos em que você precisa parar o que está fazendo e começar a trabalhar 199 | em outra tarefa. Porém, pode não é bom fazer o commit de algo que ainda não foi 200 | finalizado para depois voltar nele, resultando em um commit que ficará no histórico 201 | mas que possui um código que não funciona. Nós podemos salvar essas alterações feitas 202 | mesmo sem precisar realizar um commit para depois voltar a trabalhar nela, o que é 203 | chamado de Stash (algo como “esconder” ou “acumular”). 204 | 205 | Ao fazer isso, seu repositório voltará ao estado do último commit, e as alterações 206 | feitas anteriormente estarão “escondidas”. 207 | 208 | - Salvando modificações em um Stash 209 | 210 | `git stash` 211 | 212 | - Você ainda pode colocar um nome nesse stash 213 | 214 | `git stash push -m meu-nome-stash` 215 | 216 | - Listando Stash 217 | 218 | `git stash list` 219 | 220 | - Recuperando modificações 221 | 222 | `git stash apply` 223 | 224 | Isso vai recuperar o código do stash mais recente. Se quiser recuperar um stash 225 | mais antigo, basta ver o número do stash que aparece quando o listamos e passar 226 | para o seguinte comando: 227 | 228 | `git stash apply stash@{2}` 229 | 230 | - Removendo Stashes 231 | 232 | Quando nós recuperamos alterações de um stash, ele continua guardado. Para apagá-lo 233 | da pilha, execute o comando drop junto ao nome do stash que você quer remover 234 | 235 | `git stash drop stash@{5}` 236 | 237 | --- 238 | --------------------------------------------------------------------------------