KandZ – Tuts

We like to help…!

Git 7 🧩 Git Cheatsheet

7. Git Cheatsheet

a. Part 1: Basic & Everyday Commands

CommandDescription
git initInitializes a new local Git repository.
git clone [url]Creates a local copy of a remote repository.
git statusShows the status of changes as untracked, modified, or staged.
git add .Stages all changes in the current directory for the next commit.
git add [file]Stages a specific file for the next commit.
git commit -m "[message]"Records staged changes to the repository with a descriptive message.
git pushPushes committed changes to a remote repository.
git pullFetches and merges changes from the remote repository.

b. Part 2: Branching & Merging

CommandDescription
git branchLists all local branches.
git branch [name]Creates a new branch.
git checkout [name]Switches to a different branch.
git checkout -b [name]Creates and switches to a new branch.
git merge [branch]Merges the specified branch’s history into the current branch.
git branch -d [name]Deletes a local branch.
git fetchDownloads objects and refs from another repository. It does not merge changes, leaving them for you to inspect.
git rebase [target]Reapplies commits from the current branch on top of another branch, creating a cleaner, linear history.

c. Part 3: Undoing & Rewriting History

CommandDescription
git reset [file]Unstages a file without changing its content.
git reset --hardDiscards all changes in the working directory and staging area, and moves HEAD to the specified commit. Caution: This is destructive.
git checkout -- [file]Discards changes in a working directory. Caution: This is destructive.
git revert [commit]Creates a new commit that undoes the changes from a previous commit. This is a safer way to “undo” a commit as it doesn’t rewrite history.
git commit --amendAmends the last commit, useful for adding forgotten files or correcting the commit message.
git rebase -i [commit]Opens an interactive rebase editor, allowing you to squash, reorder, or drop commits. Caution: Do not use this on public branches.

d. Part 4: Remote Repositories & Collaboration

CommandDescription
git remote -vLists the remote repositories you are connected to.
git remote add [name] [url]Adds a new remote repository.
git remote rm [name]Removes a remote repository.
git fetch [remote]Fetches all branches from the remote repository.
git pull --rebasePulls changes from the remote and rebases them on top of your local commits, creating a cleaner history.

e. Part 5: Tagging & Inspection

CommandDescription
git logShows the commit history.
git log --onelineShows a condensed, one-line view of the commit history.
git show [commit]Shows the changes for a specific commit.
git diffShows changes in the working directory that are not yet staged.
git diff --stagedShows changes in the staging area that are not yet committed.
git tag [name]Creates a lightweight tag at the current commit.
git tag -a [name] -m "[message]"Creates an annotated tag with a message.

Leave a Reply