|
| 1 | +# git cheatsheet |
| 2 | + |
| 3 | +Check whether you have git: `git version` |
| 4 | + |
| 5 | +If not, download git from https://git-scm.com/downloads |
| 6 | + |
| 7 | + |
| 8 | +## Creating local repo |
| 9 | + |
| 10 | +| Instruction | Command | |
| 11 | +|-----------|-----------| |
| 12 | +| create a directory | `mkdir test` | |
| 13 | +| go into directory | `cd test` | |
| 14 | +| initialize git | `git init` | |
| 15 | +| create readme.md | `touch readme.md` | |
| 16 | +| edit readme.md | `nano readme.md` - then make some edits | |
| 17 | +| stage changes | `git add *` | |
| 18 | +| check which files are staged, unstaged, and untracked | `git status` | |
| 19 | +| show unstaged changes between your index and working directory | `git diff` | |
| 20 | +| commit changes | `git commit -m "create readme"` | |
| 21 | + |
| 22 | +## Working with branches and reverting changes |
| 23 | + |
| 24 | +First, make some changes and create several commits. Then: |
| 25 | + |
| 26 | +| Instruction | Command | |
| 27 | +|---------|-------------| |
| 28 | +| check all local branches | `git branch` | |
| 29 | +| if your main branch is named “master”, do this | `git branch -m main` | |
| 30 | +| see commit history (including hashes) | `git log --oneline --all` | |
| 31 | +| go to some commit | `git checkout <commit hash>` | |
| 32 | +| create and go to new branch | `git checkout -b <branch-name>` or `git switch -c <branch-name>`| |
| 33 | +| OR: go to specific commit & make new branch in one step | `git checkout -b <branch name> <commit hash>` | |
| 34 | +| go back to main branch | `git checkout main` or `git switch main`| |
| 35 | +| merge changes from named commit with current branch | `git merge <branch_name>` | |
| 36 | +| create new commit that undoes all of the changes made in <commit> & apply it to the current branch | `git revert <commit>` | |
| 37 | +| remove <file> from the staging area but leave working directory unchanged | `git reset <file>` | |
| 38 | + |
| 39 | +## Connecting local to remote repo |
| 40 | + |
| 41 | +Go to [GitHub.com](https://github.com/) and create new repo. Then follow the instructions under **"…or push an existing repository from the command line"**: |
| 42 | + |
| 43 | +| Instruction | Command | |
| 44 | +|---------|-------------| |
| 45 | +| add origin | `git remote add origin https://github.com/<your_username>/<your_repo_name>.git` | |
| 46 | +| push changes | `git push -u origin main` |
| 47 | + |
| 48 | +## Working with branches on GitHub |
| 49 | + |
| 50 | +Go to [GitHub.com](https://github.com/) and create a new branch. Then: |
| 51 | + |
| 52 | +| Instruction | Command | |
| 53 | +|---------|-------------| |
| 54 | +| fetch the repo (doesn't merge) | `git fetch remote` | |
| 55 | +| check status | `git status` | |
| 56 | +| pull the repo (merge) | `git pull` | |
| 57 | +| see both remote and local branches | `git branch -a` | |
| 58 | +| create and go to new branch | `git checkout <branch-name>` | |
| 59 | +| push and create new remote branch | `git push --set-upstream origin <branch-name>` | |
| 60 | + |
| 61 | + |
| 62 | +## Forking, branching, and pull requests (using our repo as an example) |
| 63 | + |
| 64 | +Go to our class repo and create a fork. This creates your own server-side copy. Then: |
| 65 | + |
| 66 | +| Instruction | Command | |
| 67 | +|---------|-------------| |
| 68 | +| clone the repo | `git clone <URL>` | |
| 69 | +| create new branch | `git checkout -b <branch-name>` or `git switch -c <branch-name>`| |
| 70 | +| stage changes | `git add *` | |
| 71 | +| create commit | `git commit -m “<some message>”` | |
| 72 | +| push commit | `git push --set-upstream origin <branch-name>` | |
| 73 | + |
| 74 | +On [GitHub.com](https://github.com/), you can now create a pull request from your new branch to the 'official' repository. This needs to be approved by a repository maintainer. |
0 commit comments