Configuration
git config --global [option] [value]
git config --local [option] [value]
Options can be set globally your the file system or locally to the repo folder. Local options override global options.
git config list to list --global or --local configurations
Common Configuration Options
- user.name: Set your name for commits user.name [first last]
- user.email: Set your email for commits user.email [email]
- core.editor: Define the default text editor core.editor [editor]
- color.ui: Enable colored output color.ui auto
All Config options: https://git-scm.com/docs/git-config
Repo Setup
git init
git remote add [name] [url]
git clone [url]
git init creates a brand new repo
git clone creates a local branch of an existing repository
Saving Changes
git status
git diff
git diff --staged
git add [file] [file2]
git add .
git commit
git commit -m "[message]"
git diff shows changes not yet staged or commited
git add . will stage all files for the next commit.
git commit --amend modifies the last commit (don't use on already merged/pushed commits)
git reset [file] is the "undo" to add
Logging & History
git log
git log --oneline
See commit history
git show [commit]
Show details about a commit by specifying a commit hash
Undo Commits
git revert [commit]
Create a new commit to undo a previous
git reset HEAD~1
git retore .
git reset --hard HEAD~1
Permanatly deletes a previous commit
Useful Files
.gitignore choose files to be ignored by git such as .env, logs/, dist/, or a pattern*/
Gitignore Documentation:
https://git-scm.com/docs/gitignoreUse a .gitkeep file to track an empty directory.
Check https://git-scm.com/doc or run git help [command] for more information about commands
Switching Branches
git branch to view all branches
git switch [branch]
git checkout [branch]
git switch -c [branch]
git checkout -b [branch]
Use -c or -b to create a new branch before moving.
Merging/Rebasing Branches
*Commit Changes in from-branch
git diff [from-branch]..[to-branch]
git switch [to-branch]
git checkout [to-branch]
git merge [from-branch]
git rebase [from-branch]
git branch -D [from-branch] optionally to delete branch after merge
Resolving Conflicts
While rebasing/merging, use the --continue to finish merging after resolving conflicts
While rebasing/merging, use the --abort to give up after failing to resolve conflicts
Git Remotes
git remote -v
git remote add [name] [url]
git remote remove [name] [url]
git remote set-url [name] [url]
Common remote names are origin and upstream
Pushing Changes to Remote Branch
git push
git push [remote] [branch]
git push -u [remote] [branch] Sets the upstream for future pushes so the command can be run in the future with only git push
git push --force overwrites the remote branch
git config --global push.autoSetupRemote true configures an option to never have to set upstream for branches
Pulling Changes from Remote Branch
git pull
git fetch
git merge
git pull --rebase to avoid merge commits
Store Changes Temporarily
git stash
git stash push -m "[message]"
git stash pop
git stash pop stash@{#}
git stash -u to also stash away untracked files
Times when stashing is useful:
- To switch a branch without commiting
- Move changes after forgetting to branch from main
- Testing different sets of changes
git stash apply applys a stash without deleteing it like pop
git stash list shows the stash stack
git stash pop will run git stash pop stash@{0} by defualt
git stash drop deletes the latest stash
git stash clear deletes all stashes pop
Deleting Files
git clean -fd remove all untracked files or specify a pattern
Use the -n flag to preview files to be deleted
-X removes ignored files and -x removes ignored and untracked files