notes/IT/The basics of Git.md
2023-10-09 09:07:46 -06:00

3.2 KiB

#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.

git commit -am "The Epoch"

  • 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

  • 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

git pull

  • Take the most recent commits from github, and copy them locally.

Merge local changes with remote changes

git stash

  • Take any code that hasn't been committed, and save, before reverting back to the latest commit. git pull -r
  • Fetch remote changes and apply them locally git stash pop
  • Take uncommitted changes, and apply them over the changes just added, using merge conflict markers. 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]

  • Create a new local branch, with [new-branch-name], from the current branch's latest commit

Switch to an existing branch

git checkout [branch-name]

  • Update the directory to match the code stored in [branch-name]

Delete a branch

git branch -d [branch-name]

  • Delete the specified branch and all stored info

Pull a remote branch

git switch [branch-name]

List all branches (including remote)

git branch --list -a

Prune branches that aren't on remote

https://stackoverflow.com/questions/7726949/remove-tracking-branches-no-longer-on-remote
gu