Ramificación y fusión básicas#

  • Última modificación: Mayo 14, 2022

Preparación del proyecto#

[1]:
!rm -rf git-demo
!mkdir git-demo
%cd git-demo
/workspace/git/git-demo
[2]:
!git init

!git config user.email "you@example.com"
!git config user.name "john doe"
Initialized empty Git repository in /workspace/git/git-demo/.git/

Rama master#

[3]:
!touch file_1.txt
!git add file_1.txt
!git commit -m 'create file_1.txt in master branch'
[master (root-commit) 74c1416] create file_1.txt in master branch
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file_1.txt

Creación de rama testing y modificación del archivo#

[4]:
!git checkout -b testing
!echo "line 1 in file_1.txt in testing branch" >> file_1.txt
!git add file_1.txt
!git commit -m 'add line 1 to file_1.txt in testing branch'
Switched to a new branch 'testing'
[testing 0542075] add line 1 to file_1.txt in testing branch
 1 file changed, 1 insertion(+)
[5]:
!git branch
  master
* testing

Retorno a la rama master#

[6]:
!git checkout master
Switched to branch 'master'

Creación de la rama hotfix y modificación del archivo#

[7]:
!git checkout -b hotfix
!echo "line 1 in file_1.txt in hotfix branch" >> file_1.txt
!git add file_1.txt
!git commit -m 'add line 1 to file_1.txt in hotfix branch'
Switched to a new branch 'hotfix'
[hotfix aa12308] add line 1 to file_1.txt in hotfix branch
 1 file changed, 1 insertion(+)
[8]:
!git branch
* hotfix
  master
  testing

Retorno a master y fusión de hotfix#

[9]:
!git checkout master
!git merge hotfix
Switched to branch 'master'
Updating 74c1416..aa12308
Fast-forward
 file_1.txt | 1 +
 1 file changed, 1 insertion(+)

Borrado de la rama hotfix#

[10]:
!git branch -d hotfix
Deleted branch hotfix (was aa12308).
[11]:
!git branch
* master
  testing

Retorno a la rama testing#

[12]:
!git checkout testing
!echo "line 2 in file_1.txt in testing branch" >> file_1.txt
!git add file_1.txt
!git commit -m 'add line 2 to file_1.txt in testing branch'
Switched to branch 'testing'
[testing a2a1ba3] add line 2 to file_1.txt in testing branch
 1 file changed, 1 insertion(+)

Retorno a la rama master y fusión#

[13]:
!git checkout master
!git merge testing
Switched to branch 'master'
Auto-merging file_1.txt
CONFLICT (content): Merge conflict in file_1.txt
Automatic merge failed; fix conflicts and then commit the result.

Revisión manual del conflicto#

[14]:
!cat file_1.txt
<<<<<<< HEAD
line 1 in file_1.txt in hotfix branch
=======
line 1 in file_1.txt in testing branch
line 2 in file_1.txt in testing branch
>>>>>>> testing

Corrección#

[15]:
%%writefile file_1.txt
line 1 in file_1.txt in hotfix branch
line 1 in file_1.txt in testing branch
line 2 in file_1.txt in testing branch
Overwriting file_1.txt
[16]:
!git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   file_1.txt

no changes added to commit (use "git add" and/or "git commit -a")
[17]:
!git add file_1.txt
!git commit -m "merge testing branch"
[master 6339475] merge testing branch
[18]:
!git status
On branch master
nothing to commit, working tree clean
[19]:
!git log --oneline --decorate --graph --all
*   6339475 (HEAD -> master) merge testing branch
|\
| * a2a1ba3 (testing) add line 2 to file_1.txt in testing branch
| * 0542075 add line 1 to file_1.txt in testing branch
* | aa12308 add line 1 to file_1.txt in hotfix branch
|/
* 74c1416 create file_1.txt in master branch
[20]:
!git branch
* master
  testing

Borrado de la rama testing#

[21]:
!git branch -d testing
Deleted branch testing (was a2a1ba3).
[22]:
!git branch
* master
[23]:
# --< Limpieza del área de trabajo >-------------------------------------------
%cd ..
!rm -rf git-demo
/workspace/git