Files
the_information_nexus/tech_docs/git_cheat_sheet.md

4.3 KiB
Raw Blame History

Certainly! Heres a concise and highly technical reference guide based on our discussion about handling file deletions and commits in Git:

1. Reviewing Changes

Before making any changes to your Git repository, always check the current status to see untracked or modified files.

git status

2. Staging Changes

To stage changes, including file deletions or modifications:

  • Stage a specific file:

    git add <file_path>
    
  • Stage a deleted file:

    git rm <file_path>
    
  • Stage recursive deletion for directories:

    git rm -r <directory_path>
    

3. Committing Changes

After staging the changes, commit them with a clear and descriptive message. The message should concisely explain the what and why of the changes.

git commit -m "<commit_message>"

4. Pushing Changes

To synchronize your local changes with the remote repository:

git push origin <branch_name>

5. Handling Divergent Branches

If you encounter a message about divergent branches during a pull, you have several options to reconcile them:

  • Merge (creates a merge commit):

    git pull --rebase=false
    
  • Rebase (reapplies local commits on top of fetched commits):

    git pull --rebase=true
    
  • Fast-forward only (rejects non-fast-forward pulls):

    git pull --ff-only
    

6. Force-Pushing Changes

To overwrite remote changes with your local branch (use with caution):

git push origin <branch_name> --force

7. Best Practices for Commit Messages

  • Keep the subject line around 50 characters.
  • Use the body to explain "what" and "why", not "how".
  • Use imperative mood in the subject line.

8. Ignoring Files

To prevent tracking of unnecessary files like .DS_Store, create a .gitignore file:

  • Example .gitignore content:
    .DS_Store
    

9. Git Configuration for Handling Pulls

Set a global preference for how pulls are handled across all repositories:

  • For merging:

    git config --global pull.rebase false
    
  • For rebasing:

    git config --global pull.rebase true
    
  • For fast-forward only:

    git config --global pull.ff only
    

This guide consolidates the key actions and commands in Git related to staging, committing, and pushing changes, along with best practices and configuration tips. Use it as a technical reference to manage your projects effectively in Git.


Git Cheatsheet

1. Remote Repository Commands

  • Clone a repository:
    git clone git@github.com:USER-NAME/REPOSITORY-NAME.git
  • Push changes to a specific remote branch:
    git push origin BRANCH-NAME
  • Pull changes from a specific remote branch:
    git pull origin BRANCH-NAME

2. Workflow Commands

  • Add all changes to the staging area:
    git add .

  • Commit changes with a message:
    git commit -m "Your descriptive commit message"

3. Checking Status & Log History

  • Check the current state and changes:
    git status

  • View the commit history:
    git log

4. Branching

  • Create and switch to a new branch:
    git checkout -b BRANCH_NAME

  • Switch to an existing branch:
    git checkout BRANCH_NAME

  • List all branches:
    git branch

  • Delete a branch:
    git branch -d BRANCH_NAME

5. Additional Commands

  • Show changes between the working directory and index:
    git diff

  • Revert changes from a specified commit:
    git revert COMMIT

  • Reset the current branch head to a specified commit:
    git reset COMMIT

  • Temporarily save changes:
    git stash

6. Tips & Best Practices

  • Use branches for development.
  • Commit regularly with descriptive messages.
  • Use pull requests to merge changes.
  • Resolve conflicts promptly.

7. Basic Git Syntax (Simplified Model)

The basic Git syntax is program | action | destination.

For example:

  • git add . is read as git | add | ., where the period represents everything in the current directory.
  • git commit -m "message" is read as git | commit -m | "message".
  • git status is read as git | status | (no destination).
  • git push origin main is read as git | push | origin main.

Remember: Practice makes you better at Git! Keep this cheatsheet handy.