-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommandsSheet.txt
249 lines (179 loc) · 13 KB
/
CommandsSheet.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
Author: Felipe de Padua
----------------- RESUMO DE TRAINING:
> Git: Created by Linus Torvalds (mesmo que criou o Linux)
> Git hooks: onde estao os triggers (p.ex. o git Lint que vai verificar qualidade do codigo quando feito o commit; Jenkins, etc.)
> .gitconfig (arquivo config global onde estao as configuracoes de name, email, editor, etc)
Git Pull: https://www.atlassian.com/git/tutorials/syncing (Go to the 'Git Pull' part)
- It shows 'Git pull --rebase'(which is a common practice)
Pull from other Branch: If you, say, created a branch off master.
-command: git pull (brings the changes from the repository with same branch name you are)
- Ex: if branch is called my_branch, git pull = git pull origin my_branch
-command: git pull origin master (brings the changes from the repository from MASTER and add to the local my_branch)
- Ex: You may need that when somebody pushed recent changes to master (and you want my_branch to get those changes as well)
LINK: https://git-scm.com/docs/git-pull
command: git rm --cached <file> to unstage
command: git checkout -- <file> OR git checkout . (remove all modifications. It returns to the way it was after the last COMMIT)
PS: > git checkout <file> (tambem funciona. Desfaz as modificacoes)
command: git clean -f (apaga todos arquivos untracked)
command: git config user.name ; git config user.email (to show email or name)
command: git shortlog ; git log --author="Felipe"
command: git log --graph (shows graph so that you can check if there was a merge, rebase, etc.)
command: git show <hash do commit> (p.ex: b8f24c6fcc429e30245ac3fd23f8997c1f5d4045)
- mostra as modificacoes que foram feitas naqule commit (similar ao DIFF)
command: git diff (show the modifcations)
- You can also do: git diff [filename] (file to compare)
git diff HEAD // shows the list of changes after your last commit.
git diff --cached // shows the diff between your last commit and changes to be committed next.
- You can also do "git log" to see the commits, and copy the commit hash and:
> git diff <hash> (ex: 5ac2dc....)
Check: https://veerasundar.com/blog/2011/06/git-tutorial-comparing-files-with-diff/
command: git diff --name-only (show only what files have been modified)
command: git reset HEAD <file> (to unstage)
commaend: git reset HEAD~ (to unstage all files)
command: git checkout HEAD~2 (goes back to commits, to return to the head commit, just do checkout on the branch)
- To come back to the branch head you just need to checkout to your master branch again
command: git commit -am "<message>" (ja faz "add ." e commit -m ) [VI: RECOMENDA-SE FAZER PASSO A PASSO]
command: git reset --soft <commit hash> (undo commit AND it will keep the modifications from this commit AND stage them)
command: git reset --mixed <commit hash> (undo commit AND it will keep the modifications from this commit AND leave unstaged)
command: git reset --hard <commit hash> (undo commit AND it will ignore all changes (it wont keep any modification))
- OBS: o hash que voce passa eh o commmit no qual voce quer retornar. O valor default do 'reset' é '--mixed'
EX: command: git reset HEAD~1 (reseta para o ultimo Commit)
(*) Undoing Commits & Changes: https://www.atlassian.com/git/tutorials/undoing-changes
> Commandos para linkar a um repositorio remoto (Ver a aba 'Code' em https://github.com/shunnaidder/gitTraining):
command 1): git remote add origin [email protected]:shunnaidder/gitTraining.git
command 2): git push -u origin master ('origin' é para onde vai e 'master' é o branch de onde vem [a fazer push])
-PS: precisa ter a chave ssh:
- https://gist.github.com/developius/c81f021eb5c5916013dc
1) ssh-keygen -t rsa -C "[email protected]"
2) Copy the contents of the file ~/.ssh/id_rsa.pub to your SSH keys in your GitHub account settings
3) Go to GitHub on 'Settings' > 'SSH and GPG Keys' > 'New SSH key' and add the key
command: git remote (lista os repositorios remotos ligados)
command: git push origin master [to push MASTER to ORIGIN repository on Github] (the -u is useful only th first time)
- git push <REMOTENAME> <BRANCHNAME>
SOURCE: https://help.github.com/articles/pushing-to-a-remote/
- Push to a different branch name:
- Normally when I do a push in git I do something like git push origin master, which really means push from
the local branch named master to the remote branch named master. If you want to push to a remote branch with
a different name than your local branch, separate the local and remote names with a colon:
> git push origin local-name:remote-name
SOURCE: https://penandpants.com/2013/02/07/git-pushing-to-a-remote-branch-with-a-different-name/
> Fork: you can use FORK to get a copy of a repository that you do not have permission to push changes.
You should fork the repository, clone the forked repo to your machine, make changes and push them. After that, you can
do a 'pull request' so that the owner of that real repository will analyze your changes. If they like, they can accept/merge
your changes.
command: git checkout -b <branch_name> (create new branch)
-(*) When pushing the branch do: 'git push origin <new_remote_branch_name>' (it is good to put the same name as the local)
command: git checkout <branch_name> (move to another branch)
command: git branch -D <branch_name> (delete branch - PS: checkout to other branch before deleting it)
Delete remote branches: https://www.git-tower.com/learn/git/faq/delete-remote-branch
command: git push origin --delete <branch> (to delete remote branch.
(You can run the command above to delete the local branch and then this one to remove remote branch)
SOURCE: https://www.git-tower.com/learn/git/faq/delete-remote-branch
command: git branch (to show local branches and which branch you are)
OR git branch -r (show all remore branches)
OR git branch -a (show all local and remote branches)
MERGE: cria um commit novo (porem mantem a ordem cronologica dos commits)
REBASE: joga as mudancas para o inicio/topo da fila de commits (estrutura linear, porem perde a ordem cronologica dos commits),
nao cria commmits extras
Atlassian: https://www.atlassian.com/git/tutorials/merging-vs-rebasing
REBASE:
> git checkout feature
> gir rebase master
: This moves the entire feature branch to begin on the tip of the master branch, effectively
incorporating all of the new commits in master
MERGE:
> git checkout feature
> gir merge master
Or, you can condense this to a one-liner:
> git merge master feature
command: git pull --rebase [aconselhado quando feito o pull]
command: on master do: 1) git merge <branch_name> (You will merge the branch on the master)
2) Now do 'git push origin master' to push changes to the remote repository
command: on master do: git rebase <branch_name> (i will rebase the branch on the master)
command: git rm <filename> (to remove a file and automatically stage it)
OR delete manually and then: git add [filename]
command: git stash (to save unstaged changes)
- É quase como um "git checkout .", ou seja, ele remove as modificaçoes, porem ele guarda essas modificacoes para uso futuro
- Exemplo Pratico: se voce sair da branch, voce vai perder as modificacoes. Entao, voce quer guardar as modificacoes para
quando voltar, voce poder recupera-las
- * Exemplo Pratico 2: quando voce precisar dar um PULL, Git nao deixa fazer se tiver modificacoes. No entanto se voce der
um "git checkout .", voce vai perder todas suas modificacoes.
Entao, voce pode dar "git stash", da o "git pull" e, entao, da "git stash apply"
> Para recuperar, command: git stash apply
> You can also use 'git stash pop' (Ver diferenca: https://git-scm.com/docs/git-stash)
- GET A SPECIFIC STASH: git stash apply stash@{2} (https://git-scm.com/book/en/v1/Git-Tools-Stashing)
> command: git stash list (ver a lista de stash, pois posso usar stash varias vezes)
> command: git stash clear (limpo o stash list)
// Dei git stash para esconder essa linha. Agora, depois do 'git stash apply ela vai aparecer antes das modoficacoes vindas do PULL
// Modifiquei pelo Github.com e dei commmit para depois dar pull na maquina local. Porem, tambem tinha modificacao local.
// Entao, eu fiz o 'git stash' antes de dar pull. Depois do pull, fiz 'git stash apply'. Portanto, agora tenho as
// modificacoes do PULL e STASH aqui
command: git revert <commit hash>
- faz um COMMIT revertendo as modificações daquele commit
- EX: se voce fez um COMIIT a123... que adicionou uma linha X, o 'git revert <a123...>' faz um novo
COMMIT que apaga essa linha X (Ele nao some com o COMMIT anterior que adicionou a linha X)
Depois voce pode da um CHECKOUT para esse commit novamente p/ continuar trabalhando nele
-> Voce pode verificar isso dando um 'git show <commit_hash_gerado_revert>'
PS: não é tao utilizado.
-> Com comandos como o 'RESET --soft' e 'git stash' voce consegue fazer algo similir.
command: git push origin :<branch_name> (to delete remote branch)
- git branch -d <branch_name> so apaga localmente
-> * Rebasing & Merging: https://www.youtube.com/watch?v=a_msiOrYLgM
command: git commit --amend -m "message that will substitute the current commit"
- you can also add more code before doing the command above and it will also be included in the commit
PS: you might not notice, but the commit hash will change (thats how gerrit creates the concept o patchset)
command: git log --stat (to see summary of files changed)
> you can COMMIT things and PUSH to respostory
- After that, you can still make modifications and then do 'git commit --amend -m "<message>"'
PS: however, you get a CONFLICT which you will need to solve
- SOURCE: https://www.youtube.com/watch?v=FdZecVxzJbk (The beggining ofthe video)
> Cherry-pick:
- create a commit from a branch to another branch
EX. CASE: Lets say you commited something in the wrong branch (say the master) that was supposed to be in the 'dvelop' branch.
What you can do to fix this issue:
1) 'git log' and Copy the commit hash that you want to "paste" in the develop branch.
2) Switch to the develop branch ('git checkout develop')
3) Then, you can do 'git cherry-pick <commit_hash>'. (The commit will be added to the top of this branch)
4) Lastly, you switch back to the master branch and reset to the previous commit to undo the mistaken commit.
- SOURCE: https://www.youtube.com/watch?v=FdZecVxzJbk (Starts at 07:37)
> Reset local master branch to be just like remote repository HEAD:
1) First make sure you have all the latest code from remote:
command: git fetch origin
2) Now run this to undo any changes (commits) you have and reset it to be equal to remote's master branch:
command: git reset --hard origin/master
----- VERY IMPORTANT:
command: git pull --rebase
EX.1: Voce faz uma modificacao e faz um commit. Logo após, voce roda o 'git pull --rebase' que traz modificacoes que
foram 'pushed' para o repositorio remoto mas ainda nao esta no local.
Entao, diferente de somente dar 'git pull', ele vai colocar o commit local no topo.
PS: no entanto, vai dar CONFLICT, voce deve resolver o conflito (ex: git mergetool -t kdiff3) e entao dar
o comando 'git rebase --continue'
- When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
FINALLY: Após isso, voce vai ter pegado todas as mudancas e se der 'git status', vai ver que seu commit vai
estar no topo (1 commit ahead of origin/master) pronto para ser 'pushed'.
SOURCE: https://help.github.com/articles/resolving-merge-conflicts-after-a-git-rebase/
-----
Git Fetch (Megan):
one way to get the new dev branch is to first delete your local dev branch
- first make sure you are NOT on dev branch, so switch to a different branch
- then run the command: git branch -d dev (this will delete your local dev)
- then do: git fetch origin dev
- git checkout dev (if you want to switch to dev branch)
----
Force MASTER branch to update to be just like 'develop' branch (this will rewrite commit history):
1) > git checkout master
2) > git reset --hard develop
3) > git push --force
SOURCE: https://superuser.com/questions/716818/how-to-overwrite-a-branch-in-git-with-master
----
command: git remote -v (To see where you are pushing to/fetching from)
-> "git remote prune origin" pra corrigir as referências de branches (problema ao dar PULL)
> Merge Conflict? Use: "git mergetool -t kdiff3" (using kdiff3)
IMPORTANT!!!! Never forget the command: gitk
EXTRA: gerrithub.io (Repo that combines Gerrit and Github)
--- (EXTRA) Github vs GitLab [git repositories]:
- https://usersnap.com/blog/gitlab-github/
- * https://www.upwork.com/hiring/development/gitlab-vs-github-how-are-they-different/
- (*) Atlassian's GIT Tutorial: https://www.atlassian.com/git/tutorials/syncing