Code-Memo

Git is a distributed version control system that tracks changes in your files, allowing you to revert to previous versions, collaborate with others, and maintain a history of your project.

  1. Basic Concepts:
    • Repository (Repo): A directory containing your project files and the .git folder, which contains the version history.
    • Commit: A snapshot of your project at a point in time.
    • Branch: A parallel version of your project where you can make changes without affecting the main version.
    • Merge: Combining changes from different branches.
    • Clone: A copy of a remote repository on your local machine.
    • Pull: Fetching changes from a remote repository and merging them into your local branch.
    • Push: Sending your local changes to a remote repository.

Git Basics

  1. Configuration
    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
    git config --list
    
  2. Initialization
    git init
    
  3. Cloning a Repository
    git clone <repository-url>
    
  4. Adding Files
    git add <file-name>
    git add .
    git add *.txt
    
  5. Committing Changes
    git commit -m "Commit message"
    git commit -a -m "Commit message"
    
  6. Status and Diff
    git status
    git diff
    
  7. Log
    git log
    git log --oneline
    git log --stat
    

Branching and Merging

  1. Branching
    git branch
    git branch <branch-name>
    git checkout <branch-name>
    git checkout -b <branch-name>
    
  2. Merging
    git merge <branch-name>
    git merge --no-ff <branch-name>
    
  3. Rebasing
    git rebase <branch-name>
    git rebase -i <commit-hash>
    
  4. Deleting a Branch
    git branch -d <branch-name>
    git branch -D <branch-name>
    

Remote Repositories

  1. Adding a Remote
    git remote add origin <remote-url>
    git remote -v
    
  2. Fetching and Pulling
    git fetch
    git pull
    
  3. Pushing Changes
    git push origin <branch-name>
    git push -u origin <branch-name>
    
  4. Deleting Remote Branch
    git push origin --delete <branch-name>
    

Stashing Changes

  1. Stashing
    git stash
    git stash save "Stash message"
    
  2. Listing Stashes
    git stash list
    
  3. Applying Stashes
    git stash apply
    git stash apply stash@{index}
    
  4. Dropping Stashes
    git stash drop
    git stash drop stash@{index}
    
  5. Popping Stashes
    git stash pop
    

Undoing Changes

  1. Amending Commits
    git commit --amend -m "New commit message"
    
  2. Resetting Changes
    git reset --soft <commit-hash>
    git reset --hard <commit-hash>
    
  3. Reverting Commits
    git revert <commit-hash>
    

Workflow Examples

  1. Feature Branch Workflow
    git checkout -b feature-branch
    # make changes
    git add .
    git commit -m "Add feature"
    git checkout main
    git merge feature-branch
    git push origin main
    
  2. Forking Workflow
    git clone <forked-repo-url>
    git remote add upstream <original-repo-url>
    git fetch upstream
    git checkout main
    git merge upstream/main
    git push origin main
    
  3. Rebase Workflow
    git checkout feature-branch
    git rebase main
    git checkout main
    git merge feature-branch
    git push origin main