-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranching.html
100 lines (74 loc) · 5.6 KB
/
branching.html
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
<!DOCTYPE html>
<html lang="en">
<!--
Website: Intro to Git
Page: branching.html
Author: Kevin Robell
Purpose: This page teaches the user how make branches, revert commits, and merge branches together.
-->
<head>
<meta charset="utf-8" />
<title> Intro to Git </title>
<link href="css/sta.css" rel="stylesheet" type="text/css" />
<link href="css/styles.css" rel="stylesheet" type="text/css" />
</head>
<body class="body">
<header class="header">
<h1 class="title-txt"> What is </h1>
<img id="logo" src="img/Git-Logo-Black.png" alt="git logo" />
<h1 class="title-txt"> ? </h1>
</header>
<nav class="nav">
<a href="index.html">Home</a>
<a href="starting.html">Starting Out</a>
<a id="current-page" href="branching.html">Branching</a>
<a href="remote-server.html">Remote Server</a>
</nav>
<div class="box w4">
<h2> Branching </h2>
<hr>
Branching allows you to make a copy of your project that you can change without interfering with the state of your original project. You are encouraged to make a new branch before making changes to a codebase.
</div>
<div class="box w3">
In this case, we currently have an empty file called mammals.txt. Type <code>git checkout -b <branch name></code>. This will create a new branch called add-text and checkout or move you to that new branch. You can check the current branch by typing <code>git branch</code>. Move to another branch by typing <code>git checkout <branch name></code>.
</div>
<img src="img/branching_page-console-1.png" alt="Console window showing how to create a new branch using the git checkout -b <branch name> command." />
<div class="box w3">
Now let’s add some text to mammals.txt in the add-text branch. Add “alligators, wasps, caterpillars” and make a commit.
</div>
<img src="img/branching_page-console-2.png" alt="Console window showing how to make a commit to the new branch." />
<div class="box w3">
Of course, those aren’t names of mammals. We can revert the branch to the previous commit by typing <code>git revert HEAD</code>. The default text editor will pop up to allow you to type the commit message. Continue with the default commit message. Revert actually makes a new commit which reverses the changes made in the previous commit instead of deleting the previous commit.
</div>
<img src="img/branching_page-console-3.png" alt="Console window showing how to revert a commit using the git revert HEAD command." />
<div class="box w3">
There is another more precise way to reverse changes at the file level instead of the commit level. In the add-text branch, create and add a file named another.txt. Now add “alligators, wasps, caterpillars” to the file mammals.txt one more time. If we reverted the commit like last time, the file another.txt would be lost. We don’t want that. Instead, we can revert the contents of mammals.txt to the previous commit by typing <code>git restore <file path></code>. Once again we have removed the non-mammals from the mammals.txt file while the another.txt file remains intact.
</div>
<img src="img/branching_page-console-4.png" alt="Console window showing how to restore a file using the git restore <file path> command." />
<div class="box w3">
We’re back where we started. Let’s add “tiger, lion, bear” to mammals.txt and make a commit.
</div>
<img src="img/branching_page-console-5.png" alt="Console window showing how to make another commit." />
<div class="box w3">
To add these changes to the main branch we move to that branch and type <code>git merge <branch name></code>. That will merge changes into the checked out branch. In some cases, a merge conflict will appear and this will force you decide on which code to keep based upon the diff. Once you correct the merge conflict you will be able to merge. In this case, there are no conflicts so we can move on.
</div>
<img src="img/branching_page-console-6.png" alt="Console window showing how to merge branches using the git merger <branch name> command." />
<div class="box w3">
You can see that the merge worked because the mammals.txt file was blank before the merge and has a list of mammals after the merge.
<hr>
Since we have merged in the changes from the add-text branch into the main branch, we can delete the add-text branch by typing <code>git branch -d <branch name></code>.
<br><br>
<b>Note:</b> This command only works when you are NOT using the branch that is being deleted.
</div>
<img src="img/branching_page-console-7.png" alt="Console window showing how to delete a branch by using the git branch -d <branch name> command." />
<div class="box w4">
Now that you know how to branch and merge we will move on to using git with remote servers.
</div>
<a class="btn lines" href="remote-server.html">Continue learning →</a>
<footer class="footer">
CST336 Internet Programming. 2020© Robell<br>
<a href="https://git-scm.com/downloads/logos" target="_blank">Git Logo</a> by <a href="https://twitter.com/jasonlong" target="_blank">Jason Long</a>.
Built using <a href="https://apainintheneck.github.io/sta.css/" target="_blank">sta.css</a>.
</footer>
</body>
</html>