Git
- NOTE: older repos will have
masterrather thanmain
(note: see command_line notes to find info on VIM)
- version control system
HEAD- currently checked out --- the files editable on my machine/working copy/working space
Help
git command --help- to get into help menu within git- Detailed Reference/Docs
- Interactive git examples
- GitHub cheat sheet
- GitLab cheat sheet
- Interactive visual cheat sheet
General
- when creating new github repository include
.gitignoredirectory - this allows us to avoid committing unnecessary files that would commit every time - manually add.ideaand anything else you want ignored to the.gitignoredirectory cat .git/config` look at remote to see where it is pointedgit config [option]- to change your config (can also do it in an editor to the .git/config file)- new github/gitlab account requires 1 time config setup of email and username
Status
git status- shows which files need to be to be staged, committed, etc (git status aliasgst)
Log
git log- shows changesgit log --graph- shows pathsgit log --name-status- only shows names of files changed in a commit- will have
A,M, orDat the beginning of lines to show if the file was added, modified, or deleted
- will have
git log -p- shows all changesq- to quit loggit reflog- shows history of all the refs(places your head has changed to)
Making Repositories
git init- initializes empty git repository (easier to make new repository on github/gitlab first and then clone it)git remote add origin https:/...- connect repository to a blank github repository at the address "https:/..."git push --set-upstream git@gitlab.com:kristinbrooks/new-project-name.git main- makes a new project on gitlab and pushes my local repository to maingit clone https://...- clone new github/gitlab repository with address "https://..."
Checkout
git checkout branch_name- checkout the "branch_name" branch from github/gitlab (checkout aliasco)git checkout .- reverts committed files that have been modified (checkout aliasco)
Remote Tracking Branches
git remote -v- shows a list of the local remote tracking branches/clones
Diffs (differences)
git diff- shows changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes between two blob objects, or changes between two files on disk- this much easier to follow in the IDE
git diff --name-status- show only the names and status of the changed files
Branches
git branch new_branch_name- creates a new branch, but does not switch to itgit sw name_of_branch_want_to_switch_to- switch branches (old way isgit co branch_name)git sw -c new_branch_name- create new branch and switch to it (switch aliassw)git cherry-pick commit_sha- puts the sha on the tip of a new branch- if doing this make sure remaining commit/s on original branch are still intact
git cherry-pick <current-branch>..<other-branch>- cherry-pick from one branch to another branch- example:
git checkout Branch1 git cherry-pick Branch1..Branch2 - start with this:
a -- b -- c <-- Master \ \ \ d -- e <-- Branch1 \ f -- g <-- Branch2 - get this:
a -- b -- c <-- Master \ \ \ d -- e -- f -- g <-- Branch1 (Cherry-Pick f & g) \ f -- g <-- Branch2
- example:
Pulling
git pull- runsgit fetchand callsgit mergeto merge the retrieved branch heads into the current branchgit pull --rebase- fetches and then rebases instead of merging
Fetching
git fetch- gets the updated versions of the remote tracking branch/es- if no remote is specified the default
originremote will be used
- if no remote is specified the default
git fetch --all- gets all remote tracking branches for the repo
Stashing
git stash -u- stashes local changesgit stash pop- pops the changes from the stash
Adding
git add filename- stages file "filename"git add .- stages current directory
Committing
git commit- commits and opens VIM (commit aliasci)git commit -m "notes about commit here"- commits without opening VIMgit commit --amend- edit last commit, VIM will open to edit commit messagegit commit --amend --no-edit- edit last commit without changing commit message- Writing good commit messages
Rebasing and Merging
git rebase- sets aside your changes, updates your local branch to be up to date, then applies your changes on top- if rebase, must force push afterwards
merge branch_name --no-ff- this is a merge commit, it only holds meta data not the actual changes- rebasing blows away any merge commits that happened after you branched off
Pushing
git push- pushes changes to github/gitlaboriginis the default push location if no location is provided
git push --force-with-lease- push amended/rewritten history (using just--forcewill blow away any changes if you're not up to date, so always use--force-with-leaseto be safe)git push -u- makes a new remote tracking branchgit push --set-upstream- does the same thing asgit push -u
Cleaning
git clean -df- get rid of unstaged files that have never been committedgit clean -dfx- also clean gitignored files
Resetting
git reset --hard <upstream/main, origin/main>- Resets the index and working tree. Any changes to tracked files in the working tree sinceare discarded reset docs