diff --git a/Code-Management/Git/main.md b/Code-Management/Git/main.md index 544fc23..c9b2326 100644 --- a/Code-Management/Git/main.md +++ b/Code-Management/Git/main.md @@ -1,215 +1,523 @@ -# Git Commands Guide +# Git Commands Guide (DevOps-Oriented) ## 1. Installation and Setup ### Install Git -Download from [git-scm.com](https://git-scm.com/). + +Download and install Git from: +[https://git-scm.com/](https://git-scm.com/) + +Linux (Debian/Ubuntu): + +```bash +sudo apt update && sudo apt install git -y +``` + +RHEL/CentOS: + +```bash +sudo yum install git -y +``` + +macOS (Homebrew): + +```bash +brew install git +``` ### Verify Installation + ```bash git --version ``` -### Configure User Info -Set your name and email for commits: +### Configure User Identity + +Git uses this information for commits: + ```bash git config --global user.name "Your Name" git config --global user.email "your.email@example.com" ``` +Check configuration: + +```bash +git config --list +``` + +Configuration scopes: + +* `--system`: All users +* `--global`: Current user +* `--local`: Repository only + +--- + ## 2. SSH Key Configuration -Use a custom SSH key: +### Generate SSH Key + ```bash -git config --add --local core.sshCommand 'ssh -i ' +ssh-keygen -t ed25519 -C "your.email@example.com" ``` -Clone with custom key: +Start SSH agent and add key: + ```bash -git -c core.sshCommand="ssh -i " clone host:repo +eval "$(ssh-agent -s)" +ssh-add ~/.ssh/id_ed25519 ``` +### Use Custom SSH Key (Per Repository) + +```bash +git config --local core.sshCommand "ssh -i " +``` + +Clone with custom SSH key: + +```bash +git -c core.sshCommand="ssh -i " clone git@host:repo.git +``` + +--- + ## 3. Initialize Repository -Create a new Git repo: +Create a new Git repository: + ```bash git init -b main ``` -- `-b main`: Sets default branch name to `main`. + +Existing repository: + +```bash +git init +``` + +--- ## 4. Basic Workflow -### Stage and Commit +### Stage and Commit Changes + Stage all changes: + ```bash git add -A ``` + Commit changes: + ```bash -git commit -m "Initial Commit" +git commit -m "Initial commit" ``` -### Connect to Remote -Link local repo to remote: +### Connect Local Repository to Remote + ```bash -git remote add origin +git remote add origin +``` + +Verify: + +```bash +git remote -v ``` ### Push to Remote + +First push: + ```bash -git push origin main +git push -u origin main ``` +Subsequent pushes: + +```bash +git push +``` + +--- + ## 5. Repository Status and History -### Check Status +### Check Repository Status + ```bash git status ``` -Shows staged, unstaged, and untracked files. -### View History +### View Commit History + ```bash git log ``` -- `git log -p`: Shows diffs per commit. -- `git log -3`: Shows last 3 commits. -- `git log --graph`: Shows history as a graph. -- `git log --oneline`: Shows compact log. -### View Changes +Common options: + +```bash +git log --oneline +git log --graph --oneline --all +git log -p +git log -3 +``` + +### View File Changes + +Unstaged changes: + ```bash git diff ``` -Shows unstaged changes. + +Staged changes: ```bash git diff --staged ``` -Shows staged changes. + +Compare branches: + +```bash +git diff main..dev +``` + +--- ## 6. File Operations ### Stage Specific Files + ```bash -git add +git add ``` ### Unstage Files + ```bash -git reset +git reset ``` -### Discard Changes +### Discard Local Changes + ```bash -git checkout -- +git checkout -- +``` + +Restore using modern command: + +```bash +git restore ``` -Reverts file to last committed state. ### Rename File + ```bash -git mv +git mv old-name new-name ``` ### Remove File + ```bash -git rm +git rm ``` +Remove but keep locally: + +```bash +git rm --cached +``` + +--- + ## 7. Branch Management -### Create and Switch to Branch +### Create and Switch Branch + ```bash git checkout -b ``` +Modern alternative: + +```bash +git switch -c +``` + ### List Branches + ```bash git branch -``` -List with details: -```bash +git branch -a git branch -v ``` ### Delete Branch + ```bash git branch -d ``` -### Switch Branch +Force delete: + ```bash -git checkout +git branch -D ``` -## 8. Merging +### Rename Branch + +```bash +git branch -m old-name new-name +``` + +--- + +## 8. Merging and Rebasing ### Merge Branch + ```bash git merge ``` -**Merge Types:** -- **Fast-forward:** No conflicts; branch is ahead of target. -- **Three-way:** Conflicts possible; creates new merge commit. +Merge types: + +* Fast-forward +* Three-way merge (creates merge commit) + +### Rebase (Linear History) + +```bash +git rebase main +``` + +Abort rebase: + +```bash +git rebase --abort +``` + +Continue rebase: + +```bash +git rebase --continue +``` + +--- ## 9. Remote Operations ### List Remotes + ```bash git remote -``` - -### Show Remote Info -```bash git remote -v ``` + +### Show Remote Details + ```bash git remote show origin ``` ### Fetch Changes -```bash -git fetch -``` -Get updates without merging. ```bash +git fetch git fetch --all ``` -Fetch from all remotes. + +### Pull Changes + +Fetch + merge: + +```bash +git pull +``` + +Rebase instead of merge: + +```bash +git pull --rebase +``` + +--- ## 10. Commit Management -### Edit Last Commit Message +### Amend Last Commit + ```bash git commit --amend ``` -### Show Commit Changes +### Show Commit Details + ```bash git show ``` -### Revert Commit -```bash -git revert HEAD -``` -Revert last commit. +### Revert Commit (Safe for Shared Branches) ```bash git revert ``` -Revert specific commit. -## 11. Miscellaneous +### Reset Commit (Use with Caution) + +Soft reset: + +```bash +git reset --soft HEAD~1 +``` + +Mixed reset: + +```bash +git reset HEAD~1 +``` + +Hard reset: + +```bash +git reset --hard HEAD~1 +``` + +--- + +## 11. Stash (Temporary Changes) + +Save work without committing: + +```bash +git stash +``` + +List stashes: + +```bash +git stash list +``` + +Apply stash: + +```bash +git stash apply +``` + +Pop stash: + +```bash +git stash pop +``` + +--- + +## 12. Tags (Releases) + +Create tag: + +```bash +git tag v1.0.0 +``` + +Annotated tag: + +```bash +git tag -a v1.0.0 -m "Release v1.0.0" +``` + +Push tags: + +```bash +git push origin --tags +``` + +--- + +## 13. .gitignore + +Create `.gitignore`: + +```bash +touch .gitignore +``` + +Example: + +``` +.env +node_modules/ +*.log +``` + +Apply after commit: + +```bash +git rm -r --cached . +git add . +git commit -m "Apply gitignore" +``` + +--- + +## 14. Useful Configuration and Aliases + +Change default editor: -### Change Default Editor ```bash git config --global core.editor "vim" ``` -### Clone Repository +Create aliases: + ```bash -git clone +git config --global alias.st status +git config --global alias.co checkout +git config --global alias.cm commit +git config --global alias.br branch ``` + +--- + +## 15. Troubleshooting and Recovery + +Undo last commit but keep changes: + +```bash +git reset --soft HEAD~1 +``` + +Recover deleted branch: + +```bash +git reflog +git checkout -b +``` + +Fix detached HEAD: + +```bash +git checkout main +``` + +--- + +## 16. Clone Repository + +Clone via SSH: + +```bash +git clone git@github.com:user/repo.git +``` + +Clone specific branch: + +```bash +git clone -b +``` +