Grabación de cambios en repositorios#

  • Última modificación: Mayo 14, 2022

Creación de un proyecto nuevo#

[1]:
!mkdir git-demo
%cd git-demo
/workspace/git/git-demo
[2]:
!pwd
/workspace/git/git-demo

Inicialización del repositorio#

[3]:
!git init
Initialized empty Git repository in /workspace/git/git-demo/.git/
[4]:
!git config user.email "you@example.com"
!git config user.name "john doe"

Estado del repositorio#

[5]:
!git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
Working      Staging    .git
Tree         Area       directory
-------------------------------------------------------------------------------------

Creación de un archivo#

[6]:
!touch file_1.txt
!git status
On branch master

No commits yet

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

nothing added to commit but untracked files present (use "git add" to track)
Working       Staging    .git
Tree          Area       directory
-------------------------------------------------------------------------------------
file_1.txt
  (empty)

Tracking del nuevo archivo#

[7]:
!git add file_1.txt
[8]:
!git status
On branch master

No commits yet

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

Working       Staging       .git
Tree          Area          directory
-------------------------------------------------------------------------------------
file_1.txt    file_1.txt
  (empty)       (empty)

Creación de un nuevo archivo#

[9]:
!touch file_2.txt
!git status
On branch master

No commits yet

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

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

Working       Staging       .git
Tree          Area          directory
-------------------------------------------------------------------------------------
file_1.txt    file_1.txt
  (empty)       (empty)
file_2.txt
  (empty)

Tracking de file_2.txt#

[10]:
!git add file_2.txt
!git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   file_1.txt
        new file:   file_2.txt

Working       Staging       .git
Tree          Area          directory
-------------------------------------------------------------------------------------
file_1.txt    file_1.txt
  (empty)       (empty)
file_2.txt    file_2.txt
  (empty)       (empty)

Modificación de file_2.txt#

[11]:
!echo 'hello world' > file_2.txt
!git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   file_1.txt
        new file:   file_2.txt

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:   file_2.txt

Working          Staging       .git
Tree             Area          directory
-------------------------------------------------------------------------------------
file_1.txt       file_1.txt
  (empty)          (empty)
file_2.txt       file_2.txt
  hello world      (empty)

Adición de los cambios#

[12]:
!git add file_2.txt
!git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   file_1.txt
        new file:   file_2.txt

Working          Staging          .git
Tree             Area             directory
-------------------------------------------------------------------------------------
file_1.txt       file_1.txt
  (empty)          (empty)
file_2.txt       file_2.txt
  hello world      hello world

Commit de los cambios#

[13]:
!git commit -m 'create file_1.txt file_2.txt files'
[master (root-commit) 2874d34] create file_1.txt file_2.txt files
 2 files changed, 1 insertion(+)
 create mode 100644 file_1.txt
 create mode 100644 file_2.txt
Working          Staging    .git
Tree             Area       directory
-------------------------------------------------------------------------------------
file_1.txt                  ### create file_1.txt file_2.txt files
  (empty)                   file_1.txt
file_2.txt                    (empty)
  hello world               file_2.txt
                              hello world

Visualización de cambios#

[14]:
!echo 'Hola mundo cruel!' >> file_2.txt
!git diff
diff --git a/file_2.txt b/file_2.txt
index 3b18e51..2688e04 100644
--- a/file_2.txt
+++ b/file_2.txt
@@ -1 +1,2 @@
 hello world
+Hola mundo cruel!
Working                Staging    .git
Tree                   Area       directory
-------------------------------------------------------------------------------------
file_1.txt                        ### create file_1.txt file_2.txt files
  (empty)                         file_1.txt
file_2.txt                          (empty)
  hello world                     file_2.txt
  Hola mundo cruel!                 hello world

Almacenamiento de cambios#

[15]:
!git add file_2.txt
!git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   file_2.txt

Working                Staging                .git
Tree                   Area                   directory
-------------------------------------------------------------------------------------
file_1.txt             file_2.txt             ### create file_1.txt file_2.txt files
  (empty)                hello world          file_1.txt
file_2.txt               Hola mundo cruel!      (empty)
  hello world                                 file_2.txt
  Hola mundo cruel!                             hello world

Visualización de cambios en staged#

[16]:
# Note que el siguiente comando no devuelve nada
!git diff
[17]:
!git diff --staged
diff --git a/file_2.txt b/file_2.txt
index 3b18e51..2688e04 100644
--- a/file_2.txt
+++ b/file_2.txt
@@ -1 +1,2 @@
 hello world
+Hola mundo cruel!
[18]:
!git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   file_2.txt

Archivos a ignorar#

[19]:
!touch file_to_ignore.txt
!git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   file_2.txt

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

Working                Staging                .git
Tree                   Area                   directory
-------------------------------------------------------------------------------------
file_1.txt             file_2.txt             ### create file_1.txt file_2.txt files
  (empty)                hello world          file_1.txt
file_2.txt               Hola mundo cruel!      (empty)
  hello world                                 file_2.txt
  Hola mundo cruel!                             hello world
file_to_ignore.txt
  (empty)
[20]:
!echo "file_to_ignore.txt" > .gitignore
Working                 Staging                .git
Tree                    Area                   directory
-------------------------------------------------------------------------------------
file_1.txt              file_2.txt             ### create file_1.txt file_2.txt files
  (empty)                 hello world          file_1.txt
file_2.txt                Hola mundo cruel!      (empty)
  hello world                                  file_2.txt
  Hola mundo cruel!                              hello world
file_to_ignore.txt
  (empty)
.gitignore
  file_to_ignore.txt
[21]:
#
# Note que ya no aparece file_to_ignore.txt
#
!git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   file_2.txt

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

Working                 staging                .git
Tree                    Area                   directory
-------------------------------------------------------------------------------------
file_1.txt              file_2.txt             ### create file_1.txt file_2.txt files
  (empty)                 hello world          file_1.txt
file_2.txt                Hola mundo cruel!      (empty)
  hello world                                  file_2.txt
  Hola mundo cruel!                              hello world
file_to_ignore.txt
  (empty)
.gitignore
  file_to_ignore.txt
[22]:
!git add .gitignore
!git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   .gitignore
        modified:   file_2.txt

Working                 Staging                   .git
Tree                    Area                      directory
-------------------------------------------------------------------------------------
file_1.txt              file_2.txt                ### create file_1.txt file_2.txt files
  (empty)                 hello world             file_1.txt
file_2.txt                Hola mundo cruel!         (empty)
  hello world           .gitignore                file_2.txt
  Hola mundo cruel!       file_to_ignore.txt         hello world
file_to_ignore.txt
  (empty)
.gitignore
  file_to_ignore.txt

Reversión de cambios#

[23]:
!echo "nueva linea" >> file_2.txt
!cat file_2.txt
hello world
Hola mundo cruel!
nueva linea
Working                 Staging                 .git
Tree                    Area                    directory
-------------------------------------------------------------------------------------
file_1.txt              file_2.txt              ### create file_1.txt file_2.txt files
  (empty)                 hello world           file_1.txt
file_2.txt                Hola mundo cruel!       (empty)
  hello world           .gitignore              file_2.txt
  Hola mundo cruel!       file_to_ignore.txt      hello world
  nueva linea
file_to_ignore.txt
  (empty)
.gitignore
  file_to_ignore.txt
[24]:
!git restore file_2.txt
[25]:
!cat file_2.txt
hello world
Hola mundo cruel!
Working                 Staging                 .git
Tree                    Area                    directory
-------------------------------------------------------------------------------------
file_1.txt              file_2.txt              ### create file_1.txt file_2.txt files
  (empty)                 hello world           file_1.txt
file_2.txt                Hola mundo cruel!       (empty)
  hello world           .gitignore              file_2.txt
  Hola mundo cruel!       file_to_ignore.txt      hello world
file_to_ignore.txt
  (empty)
.gitignore
  file_to_ignore.txt
[26]:
!git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   .gitignore
        modified:   file_2.txt

Commit#

[27]:
!git commit  -m 'create .gitignore file'
[master 4218caf] create .gitignore file
 2 files changed, 2 insertions(+)
 create mode 100644 .gitignore
Working                 Staging    .git
Tree                    Area       directory
-------------------------------------------------------------------------------------
file_1.txt                         ### create file_1.txt file_2.txt files
  (empty)                          file_1.txt
file_2.txt                           (empty)
  hello world                      file_2.txt
  Hola mundo cruel!                  hello world
file_to_ignore.txt                 ### create .gitignore file
  (empty)                          file_2.txt
.gitignore                           hello world
  file_to_ignore.txt                 Hola mundo cruel!
                                   .gitignore
                                     file_to_ignore.txt
[28]:
!git status
On branch master
nothing to commit, working tree clean

Borrado de archivos de la carpeta actual#

[29]:
!ls -1
file_1.txt
file_2.txt
file_to_ignore.txt
[30]:
!rm file_2.txt
!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_2.txt

no changes added to commit (use "git add" and/or "git commit -a")
Working                 Staging                 .git
Tree                    Area                    directory
-------------------------------------------------------------------------------------
file_1.txt              file_2.txt (deleted)    ### create file_1.txt file_2.txt files
  (empty)                                       file_1.txt
file_to_ignore.txt                                (empty)
  (empty)                                       file_2.txt
.gitignore                                        hello world
  file_to_ignore.txt                            ### create .gitignore file
                                                file_2.txt
                                                  hello world
                                                  Hola mundo cruel!
                                                .gitignore
                                                  file_to_ignore.txt

Dejar de seguir los cambios de un archivo#

[31]:
!git rm file_2.txt
rm 'file_2.txt'
[32]:
!git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    file_2.txt

Working                 Staging                 .git
Tree                    Area                    directory
-------------------------------------------------------------------------------------
file_1.txt              file_2.txt (deleted)    ### create file_1.txt file_2.txt files
  (empty)                                       file_1.txt
file_to_ignore.txt                                (empty)
  (empty)                                       file_2.txt
.gitignore                                        hello world
  file_to_ignore.txt                            ### create .gitignore file
                                                file_2.txt
                                                  hello world
                                                  Hola mundo cruel!
                                                .gitignore
                                                  file_to_ignore.txt
[33]:
!git commit -m "delete file_2.txt"
[master b6326fe] delete file_2.txt
 1 file changed, 2 deletions(-)
 delete mode 100644 file_2.txt
Working                 Staging    .git
Tree                    Area       directory
-------------------------------------------------------------------------------------
file_1.txt                         ### create file_1.txt file_2.txt files
  (empty)                          file_1.txt
file_to_ignore.txt                   (empty)
  (empty)                          file_2.txt
.gitignore                           hello world
  file_to_ignore.txt               ### create .gitignore file
                                   file_2.txt
                                     hello world
                                     Hola mundo cruel!
                                   .gitignore
                                     file_to_ignore.txt
                                   ### delete file_2.txt
[34]:
!git status
On branch master
nothing to commit, working tree clean

Movimiento de archivos#

[35]:
!git mv file_1.txt my_new_file.txt
!git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    file_1.txt -> my_new_file.txt

Working                 Staging                          .git
Tree                    Area                             directory
-------------------------------------------------------------------------------------------
my_new_file_1.txt       file_1.txt -> my_new_file.txt    ### create file_1.txt file_2.txt files
  (empty)                                                file_1.txt
file_to_ignore.txt                                         (empty)
  (empty)                                                file_2.txt
.gitignore                                                 hello world
  file_to_ignore.txt                                     ### create .gitignore file
                                                         file_2.txt
                                                           hello world
                                                           Hola mundo cruel!
                                                         .gitignore
                                                           file_to_ignore.txt
                                                         ### delete file_2.txt
[36]:
!git commit -m "rename file_1.txt -> my_new_file.txt"
[master cab78ab] rename file_1.txt -> my_new_file.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename file_1.txt => my_new_file.txt (100%)
Working                 Staging    .git
Tree                    Area       directory
-------------------------------------------------------------------------------------------
my_new_file_1.txt                  ### create file_1.txt file_2.txt files
  (empty)                          file_1.txt
file_to_ignore.txt                   (empty)
  (empty)                          file_2.txt
.gitignore                           hello world
  file_to_ignore.txt               ### create .gitignore file
                                   file_2.txt
                                     hello world
                                     Hola mundo cruel!
                                   .gitignore
                                     file_to_ignore.txt
                                   ### delete file_2.txt
                                   ### rename file_1.txt -> my_new_file.txt
                                   my_new_file.txt
                                     (empty)
[37]:
!git status
On branch master
nothing to commit, working tree clean
[38]:
!ls -1
file_to_ignore.txt
my_new_file.txt
[39]:
# --< Limpieza del área de trabajo >-------------------------------------------
%cd ..
!rm -rf git-demo
/workspace/git