-
Notifications
You must be signed in to change notification settings - Fork 3
/
git_basic_workflow.qmd
143 lines (75 loc) · 5.47 KB
/
git_basic_workflow.qmd
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
# Git basic workflow {#sec-git_basic_workflow}
## Overview
In this exercise, we will practice basic Linux command lines, including `ls`, `cd`, `mkdir`, `touch`and basic git commands including `git status`, `git add`, `git commit`, and `git log`.
Once we are able to move around in the shell, create files and directories, then we are ready for the basic git workflow, which is:
1. edit a file
2. save those edits
3. `git add`
4. `git commit -m "commit message"`
> edit → save → add → commit
## Working with files and the commandline in Rstudio
Goal of this step: get familiar with basic Linux command lines: `ls`, `cd`, `mkdir`and `touch`.
Log in to the DataCamp interface. Go to the Workspaces section. We will be using the terminal in DataCamp.
![](git_basic_workflow_files/datacamp_createworkspace.png)
You get to the terminal through the *Run* menu:
![](git_basic_workflow_files/datacamp_run_menu.png)
Some of the images below are of a terminal in a different application (Rstudio). The git commands and output are the same.
Type `ls` in the terminal. It will list the files and directories in a specified directory or the current working directory if no directory is specified.
![](images/s1_0.png)
Type `mkdir air_plane_exercise` in the terminal. It will create a new directory with a specified name in the file system.
![](images/s1_1.png)
Type `ls` in the terminal to check the files and directories in current working directory.
![](images/s1_2.png)
Type `cd air_plane_exercise` in the terminal to change the current working directory to `air_plane_exercise` folder.
![](images/s1_3.png)
Type `touch instructions.txt` in the terminal to create a new text file named "instructions" inside the air_plane_exercise folder. You can type `ls` to check the updated files and folders.
![](images/s1_4.png)
## Creating a git repo,
Goal of this step: learn git init.
Type `git init` in the terminal. The `git init` command is typically used to start a new repository or to convert an existing project into a Git repository.
![](images/s2_1.png)
## Git status, add, commit
Goal of this step: learn git status, git add, and git commit
Make some changes in the "instructions.txt" and save it.
Type `git status` in the terminal to show the state of the working directory and the staged changes. You will see `untracked files`.
![](images/s2_2.png)
Type `git add` in the terminal to stage changes for a commit. It allows you to select which changes in the working directory will be included in the next commit.
Type `git status` in the terminal, you will see "changes to be committed"![](images/s2_3.png)
Type `git commit -m "paper plane2"`to save changes to the local repository. It takes all of the changes staged with `git add` and stores them as a new commit with a message describing the changes.
![](images/s2_4.png)
Type `git status` to check status again. It should show "nothing to commit, working tree clean".
![](images/s2_5.png)
Make some changes:
- edit the "instructions" text file, save it, git add it, git commit it
- create another text file named "license", save it, git add it, git commit it
## Using git log to understand what is in the repo
Goal of this step: learn git log
Type git log in the terminal to display the commit history. It will show a list of all commits in the current branch, along with information about the author, date, and commit message for each one.
The latest message will show at the top and the oldest will show at the bottom.
![](images/s3_1.png)
You can also use `git log --oneline --abbrev-commit --all --graph` to show git log in short. ![](images/s3_2.png)
In class we will mostly add `--color` to this as well:
``` sh
git log --oneline --abbrev-commit --all --graph --decorate --color
```
You can learn more about reading the output of that command here: https://stackoverflow.com/questions/22313343/git-graph-what-do-the-lines-and-asteriks-denote
The * characters show a single commit, the lines help us understand the branches.
Here are the review of Linux command lines and Git command lines
## Linux Command Lines
1. cd → change directory
`cd` () is to change the current working directory to a specified directory.
2. ls → list files
`ls` (list directory contents) is to list the files and directories in a specified directory or the current working directory if no directory is specified.
3. pwd → present working directory
`pwd` shows the full path to the current directory. It can help understand how the files look. `tree ..` is another useful option (this goes up a directory using the two dots: `..` and `tree` shows the files in a hierarchical diagram.)
4. mkdir → make new directory
`mkdir` (make directory) is to create a new directory with a specified name in the file system.
## Git
1. Git add
`git add` is a command used in Git to stage changes for a commit. It allows you to select which changes in the working directory will be included in the next commit.
2. Git commit -m "message"
`git commit` saves changes to the local repository. It takes all of the changes staged with `git add` and stores them as a new commit with a message describing the changes.
3. Git status
`git status` shows the state of the working directory and the staged changes, and it indicates which branch you are currently on.
4. Git log
`git log` is to display the commit history of a repository. It shows a list of all commits in the current branch, along with information about the author, date, and commit message for each one.