Basic Git Commands Flashcards
Create a new git repository
git init
Add a file to the git repo staging area
git add <filename></filename>
Commit the changes from the staging area to the git repo
git commit -m “Commit Message”
Show the current status of staged and committed files for the git repo
git status
Configure a global default username for git
git config –add –global user.name “SuperCoolGuy”
Configure a global default email address for git
git config –add –global user.email “coolguy@gmail.com”
Configure a local default username for git
git config –add user.name “SuperCoolGuy”
View git settings (user, email, etc)
git config –list –local
git config –list –global
View a single key from the git configuration settings
git config –get <key></key>
example: git config –get user.email
Edit the git config file directly
nano .git/config
(from the root of the project folder)
Remove a key from the git configuration
git config –unset <key></key>
Remove all instances of keys with a given value
git config –unset-all <key></key>
example: git config –unset-all user.name
(assuming user.name has been set more than once)
Remove an entire section of keys from the config
git config –remove-section <section name>
example: git config –remove-section user
Where are the git config files stored for the system, global, local, and worktree levels?
System: /etc/gitconfig
Global: ~/.gitconfig (hidden file)
Local: .git/config (in the project folder)
Worktree: .git/config.worktree (in the project folder)
View the past commits and hashes
git log
Show the git log, but only with the last commit and turn off paging
git –no-pager log -n 1
Show the git log with full reference IDs and branches
git log –decorate=full
Show a compact view of the git log (one line for each commit)
git log –oneline
Using plumbing commands, view the contents of a blob file.
1.) git log (to see the commit hashes)
2.) git cat-file -p <hash-of-commit> (to see the tree hash)
3.) git cat-file -p <hash-of-tree> (to get blob hash)
4.) git cat-file -p <hash-of-blob> (to view blob file contents)</hash-of-blob></hash-of-tree></hash-of-commit>
Check the active branch of the git project
git branch
Change the name of a branch from “main” to “default”
git branch -m main default
Change the global default branch from “master” to “main”
git config –global init.defaultbranch main
Remove the branch named “main”
git branch -d main
OR
git branch -D main (force delete)
Create a new branch named “feature_one”
git branch feature_one