๐Ÿ’ก๐Ÿ“– Essential Git Commands: Your Go-To Cheat Sheet for Efficient Version Control

Git is essential for software development, allowing version control, effective collaboration among developers, and secure management of code changes. It facilitates tracking changes, reverting to previous versions, and integrating work from different t…


This content originally appeared on DEV Community and was authored by Joรฃo Victor

Git is essential for software development, allowing version control, effective collaboration among developers, and secure management of code changes. It facilitates tracking changes, reverting to previous versions, and integrating work from different team members, making the development process more organized and efficient. For more insights and to explore my other repositories or access this post in Portuguese, be sure to visit my GitHub profile at my GitHub.

๐Ÿ› ๏ธ Commands

โš™๏ธ Configuration and Setup

๐Ÿ”น git config

Configures the user name, email, editor, and more.

Exemplo: `git config --global user.name "Your Name"` sets the user name for all repositories.

๐Ÿ”น git init

Initializes a new local Git repository.

Exemplo: `git init` creates a new Git repository in the current directory.

๐Ÿ—ƒ๏ธ Stage & Snapshot

๐Ÿ”น git status

Shows the status of files (modified, untracked, etc.).

Exemplo: `git status` to see the current state of files.

๐Ÿ”น git add

Adds files to the stage for committing.

Exemplo: `git add .` adds all modified files to the stage.

๐Ÿ”น git commit

Commits the files from the stage and saves a snapshot of the project.

Exemplo: `git commit -m "message"` commits with a message.

๐ŸŒณ Branch & Merge

๐Ÿ”น git branch

Lists, creates, or deletes branches.

Exemplo: `git branch new-branch` creates a new branch.

๐Ÿ”น git checkout

Switches to another branch or restores files.

Exemplo: `git checkout another-branch` switches to the specified branch.

๐Ÿ”น git merge

Merges histories of two branches.

Exemplo: `git merge another-branch` merges another-branch into the current branch.

๐Ÿ” Inspection & Comparison

๐Ÿ”น git log

Shows the commit history.

Exemplo: `git log` to see the commit history.

๐Ÿ”น git diff

Shows differences between commits, branches, etc.

Exemplo: `git diff` shows unstaged differences.

๐Ÿ“ค Share & Update

๐Ÿ”น git remote

Manages a set of tracked repositories.

Exemplo: `git remote add origin URL` adds a new remote.

๐Ÿ”น git fetch

Downloads objects and refs from another repository.

Exemplo: `git fetch origin` updates the remote origin information.

๐Ÿ”น git push

Updates the remote repository with local commits.

Exemplo: `git push origin main` sends local commits to the main branch in the remote origin.

๐Ÿ”น git pull

Updates the local repository with the latest version from the remote.

Exemplo: `git pull origin main` updates the local with the remote.

๐Ÿšซ Undo

๐Ÿ”น git revert

Reverts changes from a specific commit.

Exemplo: `git revert <commit-hash>` reverts the changes from the specified commit.

๐Ÿ”น git reset

Resets the HEAD to a previous state.

Exemplo: `git reset --hard HEAD~1` undoes the last commit and changes.

๐Ÿ”น git rm

Removes files from the index (stage) and working directory.

Exemplo: `git rm file.txt` removes the file from the working directory and the stage.

๐Ÿ”น git restore

Restores files from the stage or commit history.

Exemplo: `git restore file.txt` undoes changes in the file.

๐Ÿ”น git clean

Removes untracked files by Git.

Exemplo: `git clean -fd` removes untracked directories and files.

๐ŸŒ Working with Remotes

๐Ÿ”น git clone

Copies an existing Git repository.

Exemplo: `git clone <url>` clones the repository locally.

๐Ÿ”น git push (review)

Sends changes to the remote repository.

Exemplo: `git push origin main` sends local changes to the main branch in the remote.

๐Ÿ”น git pull (review)

Updates your local repository with the remote repository version.

Exemplo: `git pull origin main` pulls updates from main in the origin to the local.

๐Ÿš€ Advanced Management

๐Ÿ”น git rebase

Reapplies commits on top of another base.

Exemplo: `git rebase main` reapplies the commits from the current branch on top of main.

๐Ÿ”น git blame

Shows who modified each line of a file.

Exemplo: `git blame file.txt` shows the line-by-line authorship.

๐Ÿ”น git show

Shows information about objects in Git.

Exemplo: `git show <commit-hash>` shows information about the commit.

๐Ÿ”น git log --graph

Displays the commit history as an ASCII graph.

Exemplo: `git log --graph` shows the structure of branches and merges.

๐Ÿ”น git stash

Temporarily saves local changes in a clean area.

Exemplo: `git stash push -m "message"` saves the current work with a message.

๐Ÿ”น git stash pop

Applies changes saved with git stash.

Exemplo: `git stash pop` applies the last stashed change.

๐Ÿ”น git cherry-pick

Applies a commit from another branch to the current branch.

Exemplo: `git cherry-pick <commit-hash>` applies the specified commit.

๐Ÿท๏ธ Tags

๐Ÿ”น git tag

Lists, creates, or deletes tags.

Exemplo: `git tag v1.0.0` creates a tag to mark a version.

๐Ÿงฉ Git Hooks

๐Ÿ”น git hook

Scripts hooks triggered by important events.

Exemplo: Customize `.git/hooks/pre-commit` to run tests before each commit.

๐Ÿ“ Reflog

๐Ÿ”น git reflog

Shows a log of changes in the HEAD reference.

Exemplo: `git reflog` helps to find lost commits.

๐Ÿ”— Submodules

๐Ÿ”น git submodule

Manages another repository within a repository as a submodule.

Exemplo: `git submodule add URL` adds a new submodule.

๐Ÿ› ๏ธ Debugging Tools

๐Ÿ”น git bisect

Uses binary search to find the commit that introduced a bug.

Exemplo: `git bisect start` to start the bisect.

๐Ÿ”ง Merge Tools

๐Ÿ”น git mergetool

Opens a graphical tool to resolve merge conflicts.

Exemplo: `git mergetool` after a merge conflict.

๐ŸŒ Working with Remotes

๐Ÿ”น git remote show

Shows information about the remote repository.

Exemplo: `git remote show origin` shows details of the remote origin.

๐Ÿ”น git remote prune

Removes local references to deleted remote branches.

Exemplo: `git remote prune origin` cleans old references.

๐Ÿ—‚๏ธ Git Archive

๐Ÿ”น git archive

Creates an archive (like .tar or .zip) of commit trees.

Exemplo: `git archive --format zip --output /tmp/file.zip HEAD` creates a zip file of the current state.

๐ŸŒณ Git Worktree

๐Ÿ”น git worktree

Manages multiple worktrees linked to a repository.

Exemplo: `git worktree add ../new-directory branch` creates a new worktree.

๐Ÿ› ๏ธ Other Useful Commands

๐Ÿ”น git ls-tree

Lists the contents of a commit tree.

Exemplo: `git ls-tree HEAD` shows the tree of the HEAD.

๐Ÿ”น git mv

Moves or renames a file, directory, or symlink.

Exemplo: `git mv old_file.txt new_file.txt`.

๐Ÿ”น git gc

Cleans unnecessary files and optimizes the local repository.

Exemplo: `git gc` to optimize the repository.

๐Ÿ”น git fsck

Checks the integrity of the Git filesystem.

Exemplo: `git fsck` to check for errors.

๐Ÿ”น git filter-branch

Rewrites branches.

Exemplo: `git filter-branch --tree-filter 'rm -f password.txt' HEAD` removes a file from the entire history.

โ„น๏ธ Information and Help

๐Ÿ”น git help

Shows help for Git commands.

Exemplo: `git help commit` shows the help for the commit command.

๐Ÿ”น git version

Shows the installed Git version.

Exemplo: `git version` to see the current version.

โŒ Git Ignore

๐Ÿ”น .gitignore

Specifies intentionally untracked files to ignore.

Exemplo: Add `*.log` to .gitignore to ignore log files.

๐Ÿ“ Git Attributes

๐Ÿ”น .gitattributes

Allows defining attributes for specific paths.

Exemplo: Add `*.txt linguist-detectable=true` to .gitattributes.

๐Ÿ—ƒ๏ธ LFS (Large File Storage) Management

๐Ÿ”น git lfs track

Tracks large files with Git LFS.

Exemplo: `git lfs track "*.psd"` to track Photoshop files.

๐Ÿ”น git lfs ls-files

Lists all files tracked by Git LFS.

Exemplo: `git lfs ls-files` to see LFS files.

๐Ÿš€ Performance

๐Ÿ”น git count-objects

Shows information about objects in the Git database.

Exemplo: `git count-objects` to see repository statistics.

๐ŸŒ Networking

๐Ÿ”น git daemon

Allows Git to be served as a daemon for stateless protocols.

Exemplo: `git daemon --reuseaddr --base-path=/path/to/repo --export-all --verbose` to serve a repository.

๐ŸŒณ Subtree

๐Ÿ”น git subtree

Tool for subprojects, allows repositories within another.

Exemplo: `git subtree add --prefix=subproject subproject_repo master --squash` adds a subproject.


This content originally appeared on DEV Community and was authored by Joรฃo Victor


Print Share Comment Cite Upload Translate Updates
APA

Joรฃo Victor | Sciencx (2024-08-19T21:51:23+00:00) ๐Ÿ’ก๐Ÿ“– Essential Git Commands: Your Go-To Cheat Sheet for Efficient Version Control. Retrieved from https://www.scien.cx/2024/08/19/%f0%9f%92%a1%f0%9f%93%96-essential-git-commands-your-go-to-cheat-sheet-for-efficient-version-control/

MLA
" » ๐Ÿ’ก๐Ÿ“– Essential Git Commands: Your Go-To Cheat Sheet for Efficient Version Control." Joรฃo Victor | Sciencx - Monday August 19, 2024, https://www.scien.cx/2024/08/19/%f0%9f%92%a1%f0%9f%93%96-essential-git-commands-your-go-to-cheat-sheet-for-efficient-version-control/
HARVARD
Joรฃo Victor | Sciencx Monday August 19, 2024 » ๐Ÿ’ก๐Ÿ“– Essential Git Commands: Your Go-To Cheat Sheet for Efficient Version Control., viewed ,<https://www.scien.cx/2024/08/19/%f0%9f%92%a1%f0%9f%93%96-essential-git-commands-your-go-to-cheat-sheet-for-efficient-version-control/>
VANCOUVER
Joรฃo Victor | Sciencx - » ๐Ÿ’ก๐Ÿ“– Essential Git Commands: Your Go-To Cheat Sheet for Efficient Version Control. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/19/%f0%9f%92%a1%f0%9f%93%96-essential-git-commands-your-go-to-cheat-sheet-for-efficient-version-control/
CHICAGO
" » ๐Ÿ’ก๐Ÿ“– Essential Git Commands: Your Go-To Cheat Sheet for Efficient Version Control." Joรฃo Victor | Sciencx - Accessed . https://www.scien.cx/2024/08/19/%f0%9f%92%a1%f0%9f%93%96-essential-git-commands-your-go-to-cheat-sheet-for-efficient-version-control/
IEEE
" » ๐Ÿ’ก๐Ÿ“– Essential Git Commands: Your Go-To Cheat Sheet for Efficient Version Control." Joรฃo Victor | Sciencx [Online]. Available: https://www.scien.cx/2024/08/19/%f0%9f%92%a1%f0%9f%93%96-essential-git-commands-your-go-to-cheat-sheet-for-efficient-version-control/. [Accessed: ]
rf:citation
» ๐Ÿ’ก๐Ÿ“– Essential Git Commands: Your Go-To Cheat Sheet for Efficient Version Control | Joรฃo Victor | Sciencx | https://www.scien.cx/2024/08/19/%f0%9f%92%a1%f0%9f%93%96-essential-git-commands-your-go-to-cheat-sheet-for-efficient-version-control/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.