8000 GitHub - mike-rambil/Advanced-Git: Collaborative cheatsheets for GIT
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

mike-rambil/Advanced-Git

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

banner

Advanced Git Commands

For Dummies & Try hards

Important

If you find a useful GIT command - be sure to sent a PR here as well :)

Table of Contents

Advanced Git Commands

πŸ”₯Latest Finds

πŸ“Œ git push --force-with-lease

git push --force-with-lease ensures you don't accidentally overwrite someone else's work when force-pushing. It checks if the remote branch still matches what you last pulled. If someone else has pushed changes in the meantime, your push is rejected, preventing unintended data loss.

πŸ”Ή Problem

When users are within the same branch and they want to push to remote but not sure if there is a commit pushed from the other dev that might block you

πŸ”Ή Solution

use the ---force with lease flag while pushing to check it there is a push. If there is a push from the other person - your push to the same branch is cancelled.

Checkout Tutorial Here

πŸ“Œ git worktree

πŸ”Ή Problem

Switching branches in Git can be slow and disruptive to make changes to different branches can be tedious and problematic if you need to work at different branches simultaneously.

πŸ”Ή Solution

Git Worktree allows you to work on multiple branches simultaneously without the overhead of constant switching and reconfiguration.

πŸ”Ή Flags

  • git worktree add <path> <branch>: Creates a new worktree for an existing branch.
  • git worktree add -b <branch> <path>: Creates a new worktree with a new branch.
  • git worktree list: Lists all active worktrees.
  • git worktree remove <path>: Detaches a worktree from the repo without deleting files.
  • git worktree prune: Cleans up stale worktree references after manual deletion.
  • git worktree move <old-path> <new-path>: Moves a worktree to a different location.

πŸ”Ή Tutorial

Checkout Tutorial Here

πŸ“Œ git clean

git clean removes untracked files and directories from your Git repository, helping to keep your workspace clean by deleting unnecessary files not tracked by version control.

πŸ”Ή Problem

Over time, untracked files (e.g., logs, build artifacts, temporary files) can clutter your repository, making it harder to manage. Manually deleting them is inefficient and error-prone.

πŸ”Ή Solution

Use git clean to safely remove untracked files and directories.

πŸ”Ή Flags

  • -n β†’ Shows what will be deleted without actually deleting anything.
  • -f β†’ Forces deletion of untracked files.
  • -d β†’ Deletes untracked directories.
  • -i β†’ Interactive mode, allowing selective deletion.
  • -x β†’ Removes ignored and untracked files.
  • -X β†’ Removes only ignored files.

Checkout Tutorial Here

git log --oneline -- filename.txt

git log --oneline -- filename.txt shows all past commits that changed a specific file (filename.txt) in a short and easy-to-read format.

πŸ”Ή Problem

When users want to see past commits of a specific file to either revert back a commit/cherrypick/review etc.

πŸ”Ή Solution

use the git log --oneline -- filename.txt to review the past commits and view the differences. Users can then subsequently revert, reset, review and do whatever makes sense with that commit of the specific file.

Tutorial

Checkout Tutorial Here

Repository Management

πŸ“Œ git init --bare

Short Description: Initialize a bare repository, typically used for remote repositories.

Problem: You need a central repository for collaboration, not a working directory.

Solution: Use git init --bare to create a repository that only stores Git data, suitable for remotes.

Example:

git init --bare my-repo.git

πŸ“Œ git clone --mirror <repository>

Short Description: Clone a repository in mirror mode, including all refs and branches.

Problem: You want a full backup or migration of a repository, including all refs.

Solution: Use git clone --mirror for a complete copy.

Example:

git clone --mirror https://github.com/example/repo.git

For more repository management commands, see contents/command-reference.md.

Branching and Merging

πŸ“Œ git branch -m <old-branch> <new-branch>

Short Description: Rename a branch.

Problem: You want to change a branch name for clarity or convention.

Solution: Use git branch -m to rename.

Example:

git branch -m old-feature new-feature

For more branching and merging commands, see contents/command-reference.md.

History and Inspection

  • git log --graph --oneline --decorate --all: Visualize the commit history in a graphical format.
  • git log -p: Show patches (differences) introduced in each commit.
  • git show <commit>: Show various types of objects (e.g., commits, tags, trees).
  • git reflog: Show the history of changes to the repository's refs.
  • git diff <commit1> <commit2>: Show changes between two commits.
  • git blame <file>: Show what revision and author last modified each line of a file.
  • git bisect start: Begin a binary search to find the commit that introduced a bug.
  • git bisect bad <commit>: Mark a commit as bad in the bisect process.
  • git bisect good <commit>: Mark a commit as good in the bisect process.

Stashing and Cleaning

  • git stash save "description": Save changes to the stash with a description.
  • git stash pop: Apply the last stashed changes and remove them from the stash.
  • git stash apply stash@{n}: Apply a specific stash.
  • git stash list: List all stashes.
  • git stash drop stash@{n}: Remove a specific stash.
  • git clean -fd: Remove untracked files and directories.
  • git clean -n: Show what would be removed by git clean.

Submodules

  • git submodule add <repository> <path>: Add a submodule.
  • git submodule init: Initialize the submodules.
  • git submodule update: Update the submodules to the latest commit.
  • git submodule foreach <command>: Run a command in each submodule.
  • git submodule sync: Synchronize the submodule URLs.

Advanced Configuration

  • git config --global alias.<alias-name> '<git-command>': Create a Git command alias.
  • git config --global core.editor "<editor>": Set the default text editor for Git.
  • git config --global user.name "<name>": Set the user name for Git commits.
  • git config --global user.email "<email>": Set the user email for Git commits.
  • git config --global --edit: Edit the global configuration file.

Rewriting History

  • git commit --amend: Modify the most recent commit.
  • git rebase -i <base>: Start an interactive rebase session.
  • git filter-branch --tree-filter <command> -- --all: Rewrite branches to modify the working tree.
  • git reset --soft <commit>: Move the HEAD to a commit, staging all changes.
  • git reset --hard <commit>: Move the HEAD to a commit, discarding all changes.
  • git revert <commit>: Create a new commit that undoes the changes from a previous commit.

Collaboration and Review

  • git remote add <name> <url>: Add a new remote repository.
  • git fetch <remote>: Download objects and refs from another repository.
  • git pull --rebase <remote> <branch>: Fetch the branch and rebase onto the current branch.
  • git push <remote> <branch>: Push changes to a remote repository.
  • git push --force-with-lease <remote> <branch>: Force push with a safety check to ensure you're not overwriting someone else's work.
  • git push --tags: Push all tags to the remote repository.
  • git request-pull <start> <url> <end>: Generate a request to pull changes into a repository.

πŸ›  View and Clean Up Local Git Branches

🐧 Bash

List Local Branches Without a Remote Connection. git branch -vv | grep -E '^\s*\S+\s+[^\[]+$'
Automatically Delete Local Branches Without Remote Tracking.

git branch -vv | grep -E '^\s*\S+\s+[^\[]+$' | awk '{print $1}' | xargs git branch -D

View Local Branches That Had Their Remote Deleted (Ex: PR Merged & Deleted in remote but still exist in local).**

git branch -vv | grep ': gone]'

Delete Stale Local Branches That Had Their Remote Deleted.

git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D

πŸ–₯ PowerShell

List Local Branches Without a Remote Connection.

git branch -vv | Select-String -NotMatch "origin/"

Automatically Delete Local Branches Without Remote Tracking

git branch -vv | Select-String -NotMatch "origin/" | ForEach-Object {
   $branch = ($_ -split "\s+")[1]
   git branch -D $branch
}

View Local Branches That Had Their Remote Deleted (Ex: PR Merged & Deleted in remote but still exist in local).**

git branch -vv | Select-String ": gone\]"

Delete Stale Local Branches That Had Their Remote Deleted.**

git fetch -p && git branch -vv | Select-String ': gone]' | ForEach-Object { $_.ToString().Trim().Split()[0] } | ForEach-Object { git branch -D $_ }

Miscellaneous

πŸ“Œ git archive --format=zip --output=<file.zip> <tree-ish>

Short Description: Create an archive of files from a named tree.

Problem: You want to export a snapshot of your repository as a zip file.

Solution: Use git archive to create a zip archive.

Example:

git archive --format=zip --output=repo.zip main

For more miscellaneous commands, see contents/command-reference.md.

Useful Rare Git Commands You Never Heard Of

Here are some lesser-known but powerful Git commands:

  • git replace <old-commit> <new-commit>: Temporarily substitute one commit for another, useful for testing or patching history without rewriting it.
  • git notes add -m "message" <commit>: Attach notes to commits without changing commit history.
  • git rerere: Enable reuse of recorded conflict resolutions to auto-resolve repeated merge conflicts.

For more rare and advanced commands, check out contents/rare-git-commands.md.

Contributors & Credits

Note

Add yourself, your links you have used, or even your blogs if you have some or is an author

A list of individuals who have contributed to this project.

πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ View Contributors

Acknowledgment of the original authors.

πŸ§‘β€πŸ’» View Credits & Authors

A collection of blogs, articles, and materials used to learn more about Git.

πŸ“– View References & Resources

About

Collaborative cheatsheets for GIT

Topics

Resources

Stars

Watchers

33F8

Forks

Releases

No releases published

Packages

No packages published
0