Skip to content

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.

Index
23 items
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

    1. git configread/write Git settings
    2. --globalfor your user (~/.gitconfig), all repos
    3. user.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 repo

    What each part does

    1. initcreate a .git folder here
    2. -b main--initial-branch: name the first branch main
  • git clone <url>Copy a remote repo

    What each part does

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

    1. --depth 1only the most recent commit, no history
  • git statusWhat changed?

    What each part does

    1. statusbranch, staged, not-staged and untracked files
  • git add <file>; git add -AStage one file / everything

    What each part does

    1. add <file>stage that file for the next commit
    2. add -A--all: stage every change in the repo, including deletions (git add . = only under the current folder)
  • git add -pStage only parts of a file

    What each part does

    1. -p--patch: go through each change and choose y/n
  • git commit -m "<message>"Commit staged changes

    What each part does

    1. commitrecord the staged changes
    2. -m "<message>"commit message (without -m an editor opens)
  • git commit -am "<message>"Stage tracked files and commit in one step

    What each part does

    1. -a--all: auto-stage modified and deleted tracked files (NOT new files)
    2. -mmessage
  • git commit --amend --no-editFix the last commit (not pushed yet)Caution

    What each part does

    1. --amendreplace the last commit with one that includes what is staged now
    2. --no-editkeep the same message
  • git log --oneline -n 10Recent history

    What each part does

    1. --onelineone line per commit
    2. -n 10only the last 10
  • git diff; git diff --stagedSee the changes

    What each part does

    1. git diffchanges not staged yet
    2. --stagedwhat is staged (alias --cached)
  • git show <commit>Show one commit

    What each part does

    1. showmessage, author and diff of that commit (HEAD if omitted)
  • git branch -aList branches

    What each part does

    1. -a--all: local and remote-tracking branches; * marks the current one
  • git checkout -b <branch>Create and switch to a branch

    What each part does

    1. checkout -bolder equivalent of git switch -c
  • git branch -d <branch>; git branch -D <branch>Delete a branchCaution

    What each part does

    1. -ddelete if it is fully merged
    2. -Dforce delete even if not merged
  • git merge --no-ff <branch>Merge a branch into the current one

    What each part does

    1. merge <branch>bring <branch>'s commits into the current branch
    2. --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

    1. -vshow fetch and push URLs
  • git remote add origin <url>Add a remote

    What each part does

    1. remote add origin <url>name the remote "origin" (convention)
  • git pullGet the latest from the remote

    What each part does

    1. pullfetch + merge the upstream branch (git pull --rebase to rebase instead)
  • git pushUpload your commits

    What each part does

    1. 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 disk

    What each part does

    1. rmremove from Git
    2. --cachedonly from the index; the file stays (then add it to .gitignore)
  • git mv <old> <new>Rename a tracked file

    What each part does

    1. git mvrename and stage the rename in one step

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…