Skip to content

Latest commit

 

History

History
129 lines (98 loc) · 4 KB

File metadata and controls

129 lines (98 loc) · 4 KB

Git Basics

Welcome to the foundation of Git. These are the everyday commands you will use to track your code, stage changes, and create commits.

Initialize a repository

Creates a new, empty Git repository or reinitializes an existing one. #start #new #setup

git init

Stage all files

Stages every modified, deleted, and untracked file in the current directory. #add-all #track

git add .

Add modified or untracked file

Before a change can be committed, it must be staged. This interactive command queries your working directory for any modified or untracked files and allows you to select exactly which one you want to stage. #stage

git add $file

Commit staged changes (Interactive)

A commit records a snapshot of your staged changes. Good commit messages are critical for maintaining a readable history. This command also optionally signs your commit with GPG if you need verifiable history for security compliance. #save #snapshot

git commit $opts -m "$message"

Amend previous commit

Modify the most recent commit. This replaces the old commit with a new one. You can edit the commit message or use --no-edit to just add newly staged files to the previous commit. #rename-commit #edit-message #oops #fix

git commit --amend $no_edit

Unstage one file

git restore --staged $file_staged

Unstage everything

git restore --staged .

Discard unstaged changes to a file

If you've made a mistake and want to revert a file back to exactly how it looks in your last commit, use this command. Be careful: this is a destructive action and cannot be easily undone. #revert-file #checkout-file #undo-file

git restore $file_restore

Stash changes

Sometimes you need to switch contexts (like checking out a different branch) but you aren't ready to commit your current work. Stashing allows you to save your messy state on a stack so you can retrieve it later. #save-work #shelve #hide #pause

git stash push $include_untracked -m "$message"

Pop stash

Once you are ready to resume the work you stashed away, use stash pop. This will apply the changes from the stash to your current working directory and remove the stash from the stack. #restore-work #unshelve #resume

git stash pop $stash

Discard all unstaged changes

git restore .