DEV Community

Dev Maiqui 🇧🇷
Dev Maiqui 🇧🇷

Posted on • Edited on

2 2

🔶 Meu estudo de Git #1 - Utilitários Básicos

Estou Registrando o meu estudo de git.

Para este primeiro post, utilizei o seguinte vídeo:

Git #1 - Conceitos e principais comandos de versionamento]

Utilitários Básicos do Git

A Jornada do Autodidata em Inglês

Primeiros Passos: Config

$ git --version

$ git config --global user.name "Seu Nome"

$ git config --global user.email "seu_email@email.com"

$ git config --list
Enter fullscreen mode Exit fullscreen mode

Usando a documentação: Help

$ git help

$ git help add

$ git help commit
Enter fullscreen mode Exit fullscreen mode

Criando um diretório e iniciando o Git

$ mkdir -p ~/git/es2

$ cd ~/git/es2 

$ git init
Enter fullscreen mode Exit fullscreen mode

Criando um arquivo e verificando o status dele

$ touch file.txt
Enter fullscreen mode Exit fullscreen mode
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    file.txt

nothing added to commit but untracked files present (use "git add" to track)
Enter fullscreen mode Exit fullscreen mode

Adicionando o arquivo na Staging Area (Trackeando/Monitorando o arquivo)

$ git add file.txt
Enter fullscreen mode Exit fullscreen mode
$ git status      
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   file.txt
Enter fullscreen mode Exit fullscreen mode

Trackeando vários arquivos ao mesmo tempo

De forma interativa:

$ git add -i
Enter fullscreen mode Exit fullscreen mode

Ou adicionando tudo sem perguntar:

$ git add .
Enter fullscreen mode Exit fullscreen mode

Enviando para o repositório local usando o git commit -m para mensagem curta.

$ git commit -m "cria arquivo vazio de testes"
Enter fullscreen mode Exit fullscreen mode

Enviando para o repositório local usando o git commit para mensagem longa.

$ touch file2.txt

$ touch file3.txt
Enter fullscreen mode Exit fullscreen mode
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    file2.txt
    file3.txt

nothing added to commit but untracked files present (use "git add" to track)
Enter fullscreen mode Exit fullscreen mode
$ git add .
Enter fullscreen mode Exit fullscreen mode
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   file2.txt
    new file:   file3.txt
Enter fullscreen mode Exit fullscreen mode
$ git commit
Enter fullscreen mode Exit fullscreen mode
adiciona file2.txt e file3.txt vazios para testes de commit com mensagem longa.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Changes to be committed:
#       new file:   file2.txt
#       new file:   file3.txt
#
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
-- INSERT --
Enter fullscreen mode Exit fullscreen mode

Após escrever a mensagem, pressionar ESC e digitar :wq! e pressionar ENTER.

Arquivo .gitignore

$ touch .gitignore
Enter fullscreen mode Exit fullscreen mode
$ touch dep.out
Enter fullscreen mode Exit fullscreen mode
$ ls -la
total 8
drwxr-xr-x   8 maiquipirollitome  staff  256 Mar 12 11:55 .
drwxr-xr-x   3 maiquipirollitome  staff   96 Mar 12 10:26 ..
drwxr-xr-x  12 maiquipirollitome  staff  384 Mar 12 11:56 .git
-rw-r--r--   1 maiquipirollitome  staff   14 Mar 12 11:56 .gitignore
-rw-r--r--   1 maiquipirollitome  staff    0 Mar 12 11:55 dep.out
-rw-r--r--   1 maiquipirollitome  staff    0 Mar 12 10:35 file.txt
-rw-r--r--   1 maiquipirollitome  staff    0 Mar 12 11:27 file2.txt
-rw-r--r--   1 maiquipirollitome  staff    0 Mar 12 11:27 file3.txt
Enter fullscreen mode Exit fullscreen mode

Escrevendo no arquivo .gitignore:

$ echo "dep.out" > .gitignore
Enter fullscreen mode Exit fullscreen mode

ou todos os arquivos com final .out

$ echo "*.out" >> .gitignore
Enter fullscreen mode Exit fullscreen mode
$ cat .gitignore                  
dep.out
*.out
Enter fullscreen mode Exit fullscreen mode
$ git status    
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    .gitignore

nothing added to commit but untracked files present (use "git add" to track)
Enter fullscreen mode Exit fullscreen mode
$ git add .

$ git commit -m "adiciona gitignore"
Enter fullscreen mode Exit fullscreen mode

Observando as alterações dos arquivos

$ echo "file content" > file4.txt

$ git add .

$ git commit -m "adiciona o arquivo file4 para teste de diff"
Enter fullscreen mode Exit fullscreen mode

Adicionando uma nova linha no arquivo:

$ echo "New content" >> file4.txt
Enter fullscreen mode Exit fullscreen mode
$ cat file4.txt 
file content
New content
Enter fullscreen mode Exit fullscreen mode
$ git diff  
Enter fullscreen mode Exit fullscreen mode

Após o comando acima, o conteúdo abaixo é mostrado:

diff --git a/file4.txt b/file4.txt
index dd59d09..91aea86 100644
--- a/file4.txt
+++ b/file4.txt
@@ -1 +1,2 @@
 file content
+New content
(END)
Enter fullscreen mode Exit fullscreen mode

OBS: pressione a tecla q para sair.

Histórico dos commits realizados

$ git log
Enter fullscreen mode Exit fullscreen mode

Após o comando acima, o conteúdo abaixo é mostrado:

commit be2c14b0e37a455b6eb689f956949af33aef2068 (HEAD -> master)
Author: Maiqui Pirolli Tomé <maiquitome@gmail.com>
Date:   Sat Mar 12 13:12:39 2022 -0300

    adiciona o arquivo file4

commit 79091dabdcdfc48d860c796ee1d003755c78e76d
Author: Maiqui Pirolli Tomé <maiquitome@gmail.com>
Date:   Sat Mar 12 13:02:14 2022 -0300

    adiciona gitignore

commit 443f35bbccda20cb8fd52681e027f52a2e8c7eb5
Author: Maiqui Pirolli Tomé <maiquitome@gmail.com>
Date:   Sat Mar 12 11:33:22 2022 -0300

    adiciona file2.txt e file3.txt vazios para testes de commit com mensagem longa.

commit 051e33bae593fde6c9b9e45ee4d2c0e1f8d19ace
Author: Maiqui Pirolli Tomé <maiquitome@gmail.com>
Date:   Sat Mar 12 11:03:27 2022 -0300

:
Enter fullscreen mode Exit fullscreen mode

Histórico de commits de forma resumida

$ git log --oneline
Enter fullscreen mode Exit fullscreen mode

Após o comando acima, o conteúdo abaixo é mostrado:

be2c14b (HEAD -> master) adiciona o arquivo file4
79091da adiciona gitignore
443f35b adiciona file2.txt e file3.txt vazios para testes de commit com mensagem longa.
051e33b cria arquivo vazio de testes
(END)
Enter fullscreen mode Exit fullscreen mode

Editando commits

Verificando qual arquivo não foi efetuado o commit:

$ git status       
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   file4.txt

no changes added to commit (use "git add" and/or "git commit -a")
Enter fullscreen mode Exit fullscreen mode

Olhando o conteúdo do arquivo:

$ cat file4.txt
file content
New content
Enter fullscreen mode Exit fullscreen mode

Adicionando uma linha pra arrumar depois:

$ echo "New linne" >> file4.txt 
Enter fullscreen mode Exit fullscreen mode
$ cat file4.txt                
file content
New content
New linne
Enter fullscreen mode Exit fullscreen mode

Efetuando commit:

$ git add . 

$ git commit -m "adiciona nova linha"  
Enter fullscreen mode Exit fullscreen mode

Arrumando New linne

$ nano file4.txt    
Enter fullscreen mode Exit fullscreen mode

Trocando New linne para New line

  GNU nano 2.0.6              File: file4.txt                         Modified  

file content
New content
New line

                                [ Read 3 lines ]
^G Get Help  ^O WriteOut  ^R Read File ^Y Prev Page ^K Cut Text  ^C Cur Pos
^X Exit      ^J Justify   ^W Where Is  ^V Next Page ^U UnCut Text^T To Spell
Enter fullscreen mode Exit fullscreen mode

CRTL O pra salvar
ENTER pra confirmar
CTRL X pra sair

$ cat file4.txt                
file content
New content
New line
Enter fullscreen mode Exit fullscreen mode

Subscrevendo o commit anterior:

$ git add .

$ git commit -m "adiciona nova linha" --amend
Enter fullscreen mode Exit fullscreen mode
$ git log --oneline
Enter fullscreen mode Exit fullscreen mode

Após o comando acima, conseguimos ver que existe apenas um commit com a mensagem adiciona nova linha:

a80b808 (HEAD -> master) adiciona nova linha
be2c14b adiciona o arquivo file4
79091da adiciona gitignore
443f35b adiciona file2.txt e file3.txt vazios para testes de commit com mensagem longa.
051e33b cria arquivo vazio de testes
(END)
Enter fullscreen mode Exit fullscreen mode

Desfazendo o último commit (--soft)

$ git log
Enter fullscreen mode Exit fullscreen mode

Podemos ver que o último commit foi o adiciona nova linha e antes o commit adiciona o arquivo file4

commit a80b808bd012d16a4a9fc426207e201b7ac4987e (HEAD -> master)
Author: Maiqui Pirolli Tomé <maiquitome@gmail.com>
Date:   Sat Mar 12 13:56:10 2022 -0300

    adiciona nova linha

commit be2c14b0e37a455b6eb689f956949af33aef2068
Author: Maiqui Pirolli Tomé <maiquitome@gmail.com>
Date:   Sat Mar 12 13:12:39 2022 -0300

    adiciona o arquivo file4

commit 79091dabdcdfc48d860c796ee1d003755c78e76d
Author: Maiqui Pirolli Tomé <maiquitome@gmail.com>
Date:   Sat Mar 12 13:02:14 2022 -0300

    adiciona gitignore

commit 443f35bbccda20cb8fd52681e027f52a2e8c7eb5
Author: Maiqui Pirolli Tomé <maiquitome@gmail.com>
Date:   Sat Mar 12 11:33:22 2022 -0300

    adiciona file2.txt e file3.txt vazios para testes de commit com mensagem lon:
Enter fullscreen mode Exit fullscreen mode

Não temos nenhum arquivo para ser feito o commit.

$ git status
On branch master
nothing to commit, working tree clean
Enter fullscreen mode Exit fullscreen mode

Para desfazer o último commit:

$ git reset HEAD~1 --soft
Enter fullscreen mode Exit fullscreen mode

Após o ~ eu posso informar o número de commits que eu quero desfazer.

O comando --soft informa pra voltar o arquivo pra Staging Area pois eu ainda quero utilizá-lo.

Podemos ver que o commit mais recente foi desfeito, e o arquivo voltou para a Staging Area.

$ git log --oneline
Enter fullscreen mode Exit fullscreen mode
be2c14b (HEAD -> master) adiciona o arquivo file4
79091da adiciona gitignore
443f35b adiciona file2.txt e file3.txt vazios para testes de commit com mensagem longa.
051e33b cria arquivo vazio de testes
(END)
Enter fullscreen mode Exit fullscreen mode
$ git status             
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   file4.txt
Enter fullscreen mode Exit fullscreen mode

Desfazendo o último commit (--hard)

Fazendo o commit novamente do file4.txt:

$ git commit -m "aprimora file4"       
Enter fullscreen mode Exit fullscreen mode
$ git log --oneline
Enter fullscreen mode Exit fullscreen mode
f30b8c4 (HEAD -> master) aprimora file4
be2c14b adiciona o arquivo file4
79091da adiciona gitignore
443f35b adiciona file2.txt e file3.txt vazios para testes de commit com mensagem longa.
051e33b cria arquivo vazio de testes
(END)
Enter fullscreen mode Exit fullscreen mode
$ git reset HEAD~1 --hard
Enter fullscreen mode Exit fullscreen mode
$ git log --oneline
Enter fullscreen mode Exit fullscreen mode
be2c14b (HEAD -> master) adiciona o arquivo file4
79091da adiciona gitignore
443f35b adiciona file2.txt e file3.txt vazios para testes de commit com mensagem longa.
051e33b cria arquivo vazio de testes
(END)
Enter fullscreen mode Exit fullscreen mode

A opção --hard desfaz as mudanças.

$ cat file4.txt
file content
Enter fullscreen mode Exit fullscreen mode

O arquivo não volta para a Staging Area:

$ git status       
On branch master
nothing to commit, working tree clean
Enter fullscreen mode Exit fullscreen mode

Descartando alterações

$ echo "content" >> file4.txt
Enter fullscreen mode Exit fullscreen mode
$ git status                 
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   file4.txt

no changes added to commit (use "git add" and/or "git commit -a")
Enter fullscreen mode Exit fullscreen mode
$ cat file4.txt
file content
content
Enter fullscreen mode Exit fullscreen mode

Descartar essa alteração (removendo a linha 2):

$ git checkout file4.txt
Enter fullscreen mode Exit fullscreen mode
$ cat file4.txt         
file content
Enter fullscreen mode Exit fullscreen mode
$ git status            
On branch master
nothing to commit, working tree clean
Enter fullscreen mode Exit fullscreen mode

Para descartar alterações de todos os arquivos modificados:

$ git checkout .
Enter fullscreen mode Exit fullscreen mode

Adicionando arquivo deletado na Staging Area

Deletando o arquivo file.txt

$ rm -rf file.txt
Enter fullscreen mode Exit fullscreen mode
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    deleted:    file.txt

no changes added to commit (use "git add" and/or "git commit -a")
Enter fullscreen mode Exit fullscreen mode
$ git rm file.txt    
Enter fullscreen mode Exit fullscreen mode
$ git status     
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    file.txt
Enter fullscreen mode Exit fullscreen mode
$ git commit -m "remove arquivo file"
Enter fullscreen mode Exit fullscreen mode
$ git status                         
On branch master
nothing to commit, working tree clean
Enter fullscreen mode Exit fullscreen mode

Tag

Criando um rótulo:

$ git tag v1.0.0
Enter fullscreen mode Exit fullscreen mode
$ git log --oneline
Enter fullscreen mode Exit fullscreen mode
$ bd318ba (HEAD -> master, tag: v1.0.0) remove arquivo file
be2c14b adiciona o arquivo file4
79091da adiciona gitignore
443f35b adiciona file2.txt e file3.txt vazios para testes de commit com mensagem longa.
051e33b cria arquivo vazio de testes
(END)
Enter fullscreen mode Exit fullscreen mode

Criando mais arquivos:

$ touch file5.txt 

$ git add .

$ git commit -m "adiciona file5.txt"
Enter fullscreen mode Exit fullscreen mode
$ touch file6.txt 

$ git add .

$ git commit -m "adiciona file6.txt"
Enter fullscreen mode Exit fullscreen mode
$ git log --oneline
Enter fullscreen mode Exit fullscreen mode
1f353ef (HEAD -> master) adiciona file6.txt
eaf4c1a adiciona file5.txt
bd318ba (tag: v1.0.0) remove arquivo file
be2c14b adiciona o arquivo file4
79091da adiciona gitignore
443f35b adiciona file2.txt e file3.txt vazios para testes de commit com mensagem longa.
051e33b cria arquivo vazio de testes
(END)
Enter fullscreen mode Exit fullscreen mode

Voltando para a tag v1.0.0

$ git checkout v1.0.0
Enter fullscreen mode Exit fullscreen mode
$ git log --oneline
Enter fullscreen mode Exit fullscreen mode
bd318ba (HEAD, tag: v1.0.0) remove arquivo file
be2c14b adiciona o arquivo file4
79091da adiciona gitignore
443f35b adiciona file2.txt e file3.txt vazios para testes de commit com mensagem longa.
051e33b cria arquivo vazio de testes
(END)
Enter fullscreen mode Exit fullscreen mode

Voltando para a master:

$ git checkout master
Enter fullscreen mode Exit fullscreen mode
$ git log --oneline
Enter fullscreen mode Exit fullscreen mode
1f353ef (HEAD -> master) adiciona file6.txt
eaf4c1a adiciona file5.txt
bd318ba (tag: v1.0.0) remove arquivo file
be2c14b adiciona o arquivo file4
79091da adiciona gitignore
443f35b adiciona file2.txt e file3.txt vazios para testes de commit com mensagem longa.
051e33b cria arquivo vazio de testes
(END)
Enter fullscreen mode Exit fullscreen mode

Detalhando um commit: Show

Detalhando o primeiro commit:

$ git show 051e33b
Enter fullscreen mode Exit fullscreen mode
commit 051e33bae593fde6c9b9e45ee4d2c0e1f8d19ace
Author: Maiqui Pirolli Tomé <maiquitome@gmail.com>
Date:   Sat Mar 12 11:03:27 2022 -0300

    cria arquivo vazio de testes

diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..e69de29
(END)
Enter fullscreen mode Exit fullscreen mode

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more