generated from jtr13/cctemplate
-
Notifications
You must be signed in to change notification settings - Fork 139
/
common_github_commands.Rmd
141 lines (93 loc) · 3.49 KB
/
common_github_commands.Rmd
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
# Common git command lines tutorial when working on studio
Boquan Sun
When many people work on a project, it is very efficient to use github to synchronize everyone's process. And git command lines can make us do very thing on github conveniently. Not only in RStudio, but also in VisualStudio or other development tools can we use same command lines to do what we want. Therefore, it is very significant to be familiar with git common lines.
Firstly set up user's information
```{r,eval = FALSE}
git config --global user.name "[first_name last_name]"
```
Then clone a github repository
```{r,eval = FALSE}
#initialize a directory as git directory
git init
#retrieve a git repository from github.com
git clone [url]
```
Update and retrieve from remote repository
```{r,eval = FALSE}
# synchronize your local repository with remote
git pull
#Download objects and refs from another repositorym
git fetch [alias or url]
```
Git checkout to get branch for different work.
```{r,eval = FALSE}
# start a new branch and then checked out.
git checkout -b [newbranch]
# start a new branch and then checked out. If it already exists, initialize it as a new branch.
git checkout -B [newbranch]
# switch to a existed branch
git checkout [branch]
# list all existed branches
git branch
# delete a branch
git branch -d [branch]
# create a new branch
git branch [branch]
```
After writing new codes, commit them to a remote repository.
```{r,eval = FALSE}
# add modified files to stage prepared to be commited
git add [file list]
# add everying
git add -A
#merges the specified branch’s history into the current branch
git merge [branch name]
#commit your staged files
git commit -m '[describe your commit content]'
# update remote repository
git push
#set a new remote repo and update these commit to it
git push --set-upstream origin [repo name]
```
Sometimes we want to record the current state of the working directory and the index, but want to go back to a clean working directory
```{r,eval = FALSE}
# saves local modifications away and reverts the working directory to match the HEAD commit.
git stash
#same as git stash
git stash push
# list stash entries we have
git stash list
# restore a recent stash modification
git stash apply
```
When you enter an old branch, you may forget what you have done. So there are commands help you to recall what has been modified.
```{r, eval=FALSE}
# show modified files in working directory, staged for next commit
git status
# get difference of modified parts but not staged yet
git diff
# get diiference of staged part but not committed yet
git diff --staged
#unstage files to retain changes in working directory
git reset [file_list]
```
Sometimes we may see our commit history.
```{r, eval=FALSE}
# for current branch to see the commit history
git log
# show commit in branchA but not in branchB
git log branchB..branchA
# see the commits that changed file
git log --follow [file]
#show all commit logs with indication of paths that moved
git log --stat -M
```
At last, usually we find an edition with bug, then we need to revert it.
```{r, eval=FALSE}
#check commit history
git log -- oneline
# specify the hash code next to our commit
git revert [hash code]
```
The advantage of using git revert is that it doesn't touch the commit history. This means that we can still see all of the commits in our history, even the reverted ones.
Another safety measure here is that everything happens in our local system unless we push them to the remote repo