-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.sh
131 lines (97 loc) · 4.72 KB
/
git.sh
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
#----
#---- Information about a repo
#----
git remote -v #Show remote repositories, including remote urls
#--- clone existing git repo ---#
cd ~/projects
git clone git@github.com:benscarlson/repo.git
git clone git@github.com:repo.git myrepo #instead of cloning to folder 'repo' this will clone to folder 'myrepo'
#To clone a branch
#First clone the main repo, as above
git branch -a #This shows remote branches (e.g. remotes/origin/mybranch)
git checkout mybranch #This sets up a local branch to track origin's mybranch, and switches to that branch
#Execute a git command when not in the directory containing the .git repo
git -C myrepodir
#--- add non-repo code to a new github repo ---#
#first, go to github.com and create the repo "anno" on github, don't initialize
cd ~/projects/anno/src
git init
git status
#set up .gitignore. See below
git add .
git remote add origin git@github.com:benscarlson/anno.git
git commit -am 'initial commit'
git push -u origin master
#--- add existing local repo to github ---#
#first, go to github.com and create the repo "myrepo" on github. don't initialize
#then, do this on the command line
#Commit changes
git add .
git commit -am 'initial commit'
#Set up remote tracking and push to github
git remote add origin git@github.com:benscarlson/myrepo.git
git branch -M main
git push -u origin main
#--- change a repo name ---#
#go to github and change the name
#the old name will still forward to the new name
git remote set-url origin git@github.com:/benscarlson/movedb.git #update the target repo on local repositories. don't have to but recommended.
# Untrack a file from the repo but leave on disk
git rm --cached your_filename
#get the last commit
git rev-parse HEAD #5e152f31b4e0d4fe45403d822cd785da888e0431
git log -1 --format='%H' #5e152f31b4e0d4fe45403d822cd785da888e0431
git log --pretty --format='%h' -n 1 #5e152f3 (short version?)
#To see a repo using a specific hash in github, go to upper right above the file list (e.g "25 commits"). Click on this to see the history.
#--- Submodules ---#
git submodule add [submodule-repository-url] [path]
git submodule add git@github.com:bblonder/hypervolume.git
#---- SSH Keys ----#
#How to generate an ssh key and add it to github
#https://gist.github.com/adamjohnson/5682757
cd ~/.ssh
ssh-keygen -t rsa -C "your_email@example.com"
# add ssh key to github
# https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account
ls -al ~/.ssh #check ssh director for ssh key
cat ~/.ssh/id_rsa.pub #print out contents of rsa key and copy the text
# go to github and add a key by pasting the contents
#---- Branches ----#
#https://thenewstack.io/dont-mess-with-the-master-working-with-branches-in-git-and-github/
git branch -a #Shows all branches and which is the current branch
git checkout -b mybranch #Creates the branch and switches to that branch
git checkout mybranch #switch to mybranch
#to merge back to main
git checkout main
git merge -–no-ff mybranch
git branch --merged #see which branches have been merged into the current branch
git branch -d mybranch #delete my branch
#If the current branch does not exist on origin you set an upstream branch and then push
git push --set-upstream origin mybranch
#---- how to stage a pull request ----
#note if master branch is selected and you make changes, and you want to commit changes to a local branch
# you can still follow the steps below. The key is just don't commit your changes to the master branch. Create
# your own branch and then commit. Overall, it's probably best practice to make a branch first and switch to that
# branch before making any changes.
git branch mybranch # create new branch
git checkout mybranch # switch to new branch
git branch #shows you current branch
git commit -am 'my message' # make changes and commit to local branch
git push -u origin mybranch # set up a branch on github that will track your local branch.
# go to github and make a pull request
#---- What to do to your local repo after pull request has been merged
# maintainer will merge branch into master, and delete remote branch
git pull # get the latest (assuming you are on master branch)
git branch -d mybranch # delete local branch
#---- Display logs ----#
#Some pretty ways to show commit history
git log --graph --oneline --decorate --all #This prints out the commits
git log --topo-order --all --graph --date=local --pretty=format:'%C(green)%h%C(reset) %><(55,trunc)%s%C(red)%d%C(reset) %C(blue)[%an]%C(reset) %C(yellow)%ad%C(reset)%n'
#can make alias for a command
git config --global alias.mycmd 'git command'
git mycmd #is now aliased to 'git command'
#---- .gitignore
echo ".DS_Store" >> .gitignore
echo ".RData" >> .gitignore
echo ".RHistory" >> .gitignore
echo ".Rproj.user" >> .gitignore