Git: Basics: set up, stage, commit, sync
23 Git commands: Basics: set up, stage, commit, sync. Click a command to see what each part does.
Basics: set up, stage, commit, sync23
git config --global user.name "<name>"; git config --global user.email "<email>"Set your name and email (once per machine)What each part does
git configread/write Git settings--globalfor your user (~/.gitconfig), all reposuser.name / user.emailrecorded as the author of your commits
Tip Git 2.46+ also has: git config set --global user.name "<name>". Check with git config list --show-origin.
git init -b mainStart a new repoWhat each part does
initcreate a .git folder here-b main--initial-branch: name the first branch main
git clone <url>Copy a remote repoWhat each part does
clone <url>download the repo with full history into a new folder
git clone --depth 1 <url>Shallow clone (fast, latest commit only)What each part does
--depth 1only the most recent commit, no history
git statusWhat changed?What each part does
statusbranch, staged, not-staged and untracked files
git add <file>; git add -AStage one file / everythingWhat each part does
add <file>stage that file for the next commitadd -A--all: stage every change in the repo, including deletions (git add . = only under the current folder)
git add -pStage only parts of a fileWhat each part does
-p--patch: go through each change and choose y/n
git commit -m "<message>"Commit staged changesWhat each part does
commitrecord the staged changes-m "<message>"commit message (without -m an editor opens)
git commit -am "<message>"Stage tracked files and commit in one stepWhat each part does
-a--all: auto-stage modified and deleted tracked files (NOT new files)-mmessage
git commit --amend --no-editFix the last commit (not pushed yet)CautionWhat each part does
--amendreplace the last commit with one that includes what is staged now--no-editkeep the same message
git log --oneline -n 10Recent historyWhat each part does
--onelineone line per commit-n 10only the last 10
git diff; git diff --stagedSee the changesWhat each part does
git diffchanges not staged yet--stagedwhat is staged (alias --cached)
git show <commit>Show one commitWhat each part does
showmessage, author and diff of that commit (HEAD if omitted)
git branch -aList branchesWhat each part does
-a--all: local and remote-tracking branches; * marks the current one
git checkout -b <branch>Create and switch to a branchWhat each part does
checkout -bolder equivalent of git switch -c
git branch -d <branch>; git branch -D <branch>Delete a branchCautionWhat each part does
-ddelete if it is fully merged-Dforce delete even if not merged
git merge --no-ff <branch>Merge a branch into the current oneWhat each part does
merge <branch>bring <branch>'s commits into the current branch--no-ffalways create a merge commit, even when a fast-forward is possible
Tip Conflict? Fix the files, git add them, git commit, or give up with git merge --abort.
git remote -vWhich remotes are set up?What each part does
-vshow fetch and push URLs
git remote add origin <url>Add a remoteWhat each part does
remote add origin <url>name the remote "origin" (convention)
git pullGet the latest from the remoteWhat each part does
pullfetch + merge the upstream branch (git pull --rebase to rebase instead)
git pushUpload your commitsWhat each part does
pushsend commits to the upstream branch (first time: git push -u origin <branch>)
git rm --cached <file>Stop tracking a file but keep it on diskWhat each part does
rmremove from Git--cachedonly from the index; the file stays (then add it to .gitignore)
git mv <old> <new>Rename a tracked fileWhat each part does
git mvrename and stage the rename in one step
Nothing matches.
My DevOps Playbook by Angelo John Calleja