notes/IT/The basics of Git.md

77 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

2022-10-23 20:32:43 +00:00
#notes #documentation
### Create a git repository
`git init`
- This defines the folder as a git repository, and creates `.git`, a folder that contains metadata about the repo, like where the *remote* (repository hosted somewhere else) is, and other useful stuff like version control. You should not need to directly interact with this folder.
`git add .`
- This makes all files and folders in the current repository(can also be more precise by replacing `.` with specific files) tracked.
2022-11-15 00:03:45 +00:00
`git commit -am "The Epoch"`
2022-10-23 20:32:43 +00:00
- Syntax breakdown:
- `-a` All, commit all changes
- `-m` Have a message, takes a string argument, in this example, "The Epoch"
#### If adding a remote repository, GitHub or otherwise.
`git remote add origin https://urltorepo.com/path/to/repo`
- Telling git that the files should come from and go to that remote repository, where `https://urltorepo.com/path/to/repo` is the destination repo.
- If the url is wrong, it can be removed with `git remote remove origin`
`git branch -M main`
- Syntax breakdown:
- `-M`, shortcut for `--move --force`, used to move/rename a branch.
- Telling git that we want the current branch to be `main`.
`git push -u origin main`
- Synatx breakdown:
- `-u` is a shortcut for `--set-upstream`
- Telling git that we want the main host of the repository(`origin`, or `https://urltorepo.com/path/to/repo`) to be the host. After this is run one time, git remembers, and you can shorten it to `git push`, to take commits(saved changes) from your local code, and send them to remote(GitHub or other Git server).
### Update code on github/remote
`git commit -am "changes"`
- Tell git to keep track of all the changes you made
`git push`
- Take code here and put it on the server
### Get code from github
`git clone https://remotehost.com/repo/to/clone`
2023-08-23 21:14:26 +00:00
- Download a folder containing all of the code and versions to a project, where `https://remotehost.com/repo/to/clone` is the url of the repo in question.
### Fetch recent changes from github
2023-08-23 21:15:26 +00:00
`git pull`
2023-08-23 21:16:26 +00:00
- Take the most recent commits from github, and copy them locally.
### Merge local changes with remote changes
`git stash`
2023-08-23 21:20:26 +00:00
- Take any code that hasn't been committed, and save, before reverting back to the latest commit.
2023-08-23 21:26:26 +00:00
`git pull -r`
2023-08-23 21:23:26 +00:00
- Fetch remote changes and apply them locally
2023-08-23 21:26:26 +00:00
`git stash pop`
2023-08-23 21:27:26 +00:00
- Take uncommitted changes, and apply them over the changes just added, using merge conflict markers.
2023-08-23 21:28:26 +00:00
Then open the project in any merge conflict resolver, and manually resolve the commit. Most IDEs have one of some form, I like VScode's.
## Working with branches
### Create a new branch
`git checkout -b [new-branch-name]`
2023-08-23 21:31:26 +00:00
- Create a new local branch, with `[new-branch-name]`, from the current branch's latest commit
2023-08-23 21:28:26 +00:00
### Switch to an existing branch
2023-08-23 21:29:26 +00:00
`git checkout [branch-name]`
2023-08-23 21:32:26 +00:00
- Update the directory to match the code stored in `[branch-name]`
2023-08-23 21:31:26 +00:00
2023-08-23 21:30:26 +00:00
### Delete a branch
2023-08-23 21:31:26 +00:00
`git branch -d [branch-name]`
- Delete the specified branch and all stored info
### Pull a remote branch
2023-08-23 21:33:26 +00:00
`git switch [branch-name]`
2023-08-23 21:34:26 +00:00
### List all branches (including remote)
2023-10-09 15:07:46 +00:00
`git branch --list -a`
2023-10-09 15:10:46 +00:00
### Prune branches that aren't on remote
2023-10-09 15:07:46 +00:00
<https://stackoverflow.com/questions/7726949/remove-tracking-branches-no-longer-on-remote><br>
2023-10-09 15:09:46 +00:00
`gut remote prune origin`