|
| 1 | + |
| 2 | +# git |
| 3 | + |
| 4 | +a way to keep track of changes you make to files. a very |
| 5 | +commonly-used *version control system* |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +# why use git? |
| 10 | + |
| 11 | +it solves the problem of having "draft1.txt", "draft2.txt", |
| 12 | +etc. |
| 13 | + |
| 14 | +limitations of copying files and numbering them: |
| 15 | +- when you forget what was in which version, no easy way to |
| 16 | + see what was in which file. |
| 17 | +- no easy way to compare between versions. |
| 18 | +- no easy way to go back to a previous version. |
| 19 | +- clogs up the folder |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +# repos |
| 24 | + |
| 25 | +git repositories ("repos") are used to keep track of the |
| 26 | +files associated with one project. |
| 27 | + |
| 28 | +to keep things orderly, within your programming directory, |
| 29 | +make a new directory for each project you start. |
| 30 | + |
| 31 | +in each of your project directories, start a git repo to |
| 32 | +keep track of your work. do this with `git init` |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +# key git commands |
| 37 | + |
| 38 | +https://www.youtube.com/watch?v=u2rZLVWOWvs |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +# sample workflow |
| 43 | + |
| 44 | +- `mkdir` for your new project |
| 45 | +- create some files in there, get started, etc. |
| 46 | +- `git init` |
| 47 | +- `git status` (should be empty) |
| 48 | +- `git add doge.png wow.html` |
| 49 | +- `git status` (now git is watching these files for changes!) |
| 50 | +- `git commit -am "first commit"` (now you've saved a snapshot |
| 51 | + of the directory. note: it will only commit changes in the files |
| 52 | + you're watching.) |
| 53 | +- `git status` (should be clean) |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +# good practices |
| 58 | + |
| 59 | +make a commit for each bit of progress you make. |
| 60 | + |
| 61 | +commit messages help track progress - make nice ones! |
| 62 | +it sucks to break something and then have a hard time |
| 63 | +finding the last working version! |
| 64 | + |
| 65 | +don't do `git add .` or `git add *` - you will add files |
| 66 | +that you didn't mean to add! |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +# finding past versions |
| 71 | + |
| 72 | +`git log` |
| 73 | + |
| 74 | +each commit has a unique identifier called a hash. it'll look |
| 75 | +something like this in the log: a5815fb05810bc0ebf53faaa4aba370055bf70d5 |
| 76 | + |
| 77 | +for a more compact log, do `git log --oneline`. you can also use |
| 78 | +the short hashes in that list to look at & revert to |
| 79 | +previous versions. |
| 80 | + |
| 81 | +`git show 8d9c7f45ca1f1af59061fa64608666abef6cafd6` shows what changed |
| 82 | + |
| 83 | +to see what the entirety of a file looked like at a |
| 84 | +particular commit, do: |
| 85 | + |
| 86 | +``` |
| 87 | +git show 8d9c7f45ca1f1af59061fa64608666abef6cafd6:file.js |
| 88 | +``` |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +# going back to a previous commit |
| 93 | + |
| 94 | +`git revert` |
| 95 | + |
| 96 | +more on going back to previous commits: |
| 97 | +https://www.atlassian.com/git/tutorials/undoing-changes |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +# github |
| 102 | + |
| 103 | +https://www.youtube.com/watch?v=3cMP4oBKO34 |
| 104 | + |
| 105 | +(addendum: i prefer `git push origin master`) |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +# starting a new github repo |
| 110 | + |
| 111 | +github repos live online, on github.com. you can have a |
| 112 | +million projects in local git repos and zero activity on |
| 113 | +github. to take your local repo and put it online (where it |
| 114 | +will live on forever regardless of what happens to your |
| 115 | +computer and where others can see your work & possibly even |
| 116 | +help you with it!), you first have to go to github.com. |
| 117 | + |
| 118 | +- on github: click the big green "new repository" button and |
| 119 | + follow the steps. NOTE: do not make a README. |
| 120 | +- on github: once your new github repository is ready, the |
| 121 | + site will show your new repo & display its git url. it |
| 122 | + will look something like this: |
| 123 | + `[email protected]:cyberwizardinstitute/course-map.git` |
| 124 | +- in terminal do: `git remote add origin |
| 125 | + [email protected]:cyberwizardinstitute/course-map.git` |
| 126 | +- in terminal do: `git push origin master` |
| 127 | + |
| 128 | +now, when you go to your repo on github, it should have the |
| 129 | +contents of your last commit. |
| 130 | + |
| 131 | +`git remote -v` |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +# collaborating on existing github projects (w/push access) |
| 136 | + |
| 137 | +here's a workflow for existing github projects that you have |
| 138 | +push access to and where it will be fine if you push |
| 139 | +directly to them. |
| 140 | + |
| 141 | +- on github, find the git url to clone the repo. |
| 142 | +- in terminal: `git clone xyz.git` |
| 143 | +- check that everything has copied with `ls` |
| 144 | +- make whatever changes you want |
| 145 | +- `git status` |
| 146 | +- `git commit -am "adding xyz"` (this makes a commit on your |
| 147 | + local machine) |
| 148 | +- `git push origin master` (this pushes your changes back up |
| 149 | + to github) |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +# collaborating on existing github projects (w/o push access) |
| 154 | + |
| 155 | +for existing github projects that you'd like to work on but |
| 156 | +that you don't have push access to, or where you'd like your |
| 157 | +own github repo to play around in before pushing to the main |
| 158 | +branch. |
| 159 | + |
| 160 | +- on github, fork the repo you want to work with. (this will |
| 161 | + give you your own copy of the repo on github, but not on |
| 162 | + your local machine). |
| 163 | +- on github, go to your own fork of the repo and find the git url to clone the repo. |
| 164 | +- in terminal: `git clone xyz.git` (this pulls your forked |
| 165 | + copy to your local machine) |
| 166 | +- check that everything has copied with `ls` |
| 167 | +- make whatever changes you want |
| 168 | +- `git status` |
| 169 | +- `git commit -am "adding xyz"` (this makes a commit on your |
| 170 | + local machine) |
| 171 | +- `git push origin master` (this pushes your changes back up |
| 172 | + to your fork on github) |
| 173 | +- to ask that your changes get merged back into the main |
| 174 | + branch, go to the main repo (not your fork) on github and |
| 175 | + click "make a pull request" |
| 176 | + |
| 177 | +--- |
| 178 | + |
| 179 | +# good practices when working on collaborative github repos |
| 180 | + |
| 181 | +each time you return to working on your local copy of a |
| 182 | +shared github repo, do `git pull origin master` to make sure |
| 183 | +that you have the most up to date version of the repo. |
| 184 | + |
| 185 | +many existing open source projects have code review before |
| 186 | +your pull request can be merged. this is why it's a |
| 187 | +good idea to fork and work on your own branch of an existing |
| 188 | +project. |
0 commit comments