logo
eng-flag

Git Cheat Sheet

Table of Contents

  1. Getting Started
  2. Basic Commands
  3. Branching and Merging
  4. Remote Repositories
  5. Undoing Changes
  6. Advanced Commands
  7. Git Configuration
  8. Practical Scenario
  9. Git Workflow Best Practices

Getting Started

Initialize a new Git repository

git init

Clone an existing repository

git clone <repository-url>

Example:

git clone https://github.com/username/repo-name.git

Basic Commands

Check the status of your repository

git status

Add files to the staging area

git add <file-name>

Add all files:

git add .

Commit changes

git commit -m "Your commit message"

View commit history

git log

For a more concise view:

git log --oneline

Show changes between commits, commit and working tree, etc.

git diff

Branching and Merging

List all branches

git branch

Create a new branch

git branch <branch-name>

Switch to a branch

git checkout <branch-name>

Create and switch to a new branch

git checkout -b <branch-name>

Merge a branch into the current branch

git merge <branch-name>

Delete a branch

git branch -d <branch-name>

Remote Repositories

Add a remote repository

git remote add <remote-name> <repository-url>

Example:

git remote add origin https://github.com/username/repo-name.git

List remote repositories

git remote -v

Fetch changes from a remote repository

git fetch <remote-name>

Pull changes from a remote repository

git pull <remote-name> <branch-name>

Push changes to a remote repository

git push <remote-name> <branch-name>

Undoing Changes

Discard changes in working directory

git checkout -- <file-name>

Unstage a file

git reset HEAD <file-name>

Amend the last commit

git commit --amend

Reset to a specific commit

git reset --hard <commit-hash>

Revert a commit

git revert <commit-hash>

Advanced Commands

Interactive rebase

git rebase -i <commit-hash>

Cherry-pick a commit

git cherry-pick <commit-hash>

Create a tag

git tag <tag-name>

Stash changes

git stash

Apply stashed changes

git stash apply

Clean untracked files

git clean -f

Git Configuration

Set global user name

git config --global user.name "Your Name"

Set global user email

git config --global user.email "your.email@example.com"

Set default editor

git config --global core.editor "vim"

List all configurations

git config --list

Learning Git: A Practical Scenario

Setting Up the Project

Imagine you're a developer starting a new project called "SuperApp". You decide to use Git for version control. Let's go through the process of setting up and managing this project using Git.

Initialize the Repository

First, create a new directory for your project and initialize a Git repository:

mkdir SuperApp
cd SuperApp
git init

Configure Git

Set up your Git configuration:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Creating the Initial Commit

Create Some Files

Create a few initial files for your project:

echo "# SuperApp" > README.md
echo "console.log('Hello, SuperApp!');" > app.js
echo "body { font-family: Arial, sans-serif; }" > styles.css

Check the Status

Check the status of your repository:

git status

You'll see that these files are untracked.

Add Files to Staging Area

Add these files to the staging area:

git add README.md app.js styles.css

Commit the Changes

Make your first commit:

git commit -m "Initial commit: Add README, app.js, and styles.css"

Branching and Feature Development

Create a New Branch

You want to add a new feature for user authentication. Create a new branch for this feature:

git checkout -b feature/user-auth

Make Changes

Add a new file for user authentication:

echo "function login() { /* TODO: Implement login logic */ }" > auth.js

Modify app.js to include the new auth.js file:

echo "import './auth.js';" >> app.js
echo "// TODO: Implement user authentication" >> app.js

Stage and Commit Changes

Stage and commit these changes:

git add auth.js app.js
git commit -m "Add basic structure for user authentication"

Switch Back to Main Branch

Switch back to the main branch:

git checkout main

Create Another Branch

Create another branch for a different feature:

git checkout -b feature/data-visualization

Add a new file for this feature:

echo "function createChart() { /* TODO: Implement chart creation */ }" > chart.js

Stage and commit:

git add chart.js
git commit -m "Add basic structure for data visualization"

Merging and Resolving Conflicts

Merge User Auth Branch

Switch back to main and merge the user authentication feature:

git checkout main
git merge feature/user-auth

Attempt to Merge Data Visualization

Now try to merge the data visualization feature:

git merge feature/data-visualization

This might create a merge conflict if both branches modified app.js.

Resolve the Conflict

Open app.js and resolve the conflict manually. Then:

git add app.js
git commit -m "Merge feature/data-visualization and resolve conflicts"

Working with Remote Repositories

Add a Remote Repository

Add a remote repository (assuming you've created one on GitHub):

git remote add origin https://github.com/yourusername/SuperApp.git

Push to Remote

Push your changes to the remote repository:

git push -u origin main

Create a Pull Request

For the data visualization feature, push the branch and create a pull request:

git checkout feature/data-visualization
git push -u origin feature/data-visualization

Then go to GitHub and create a pull request for this branch.

Collaboration and Syncing

Clone the Repository (for a new team member)

A new team member would clone the repository:

git clone https://github.com/yourusername/SuperApp.git
cd SuperApp

Fetch and Pull Changes

Before starting work each day, fetch and pull changes:

git fetch origin
git pull origin main

Create a New Feature Branch

Create a new branch for a bug fix:

git checkout -b bugfix/login-error

Make changes, commit, and push:

# Make changes to auth.js
git add auth.js
git commit -m "Fix login error handling"
git push -u origin bugfix/login-error

Advanced Git Usage

Amending a Commit

Realize you forgot to update the README. Amend the last commit:

echo "## User Authentication" >> README.md
git add README.md
git commit --amend

Interactive Rebase

Suppose you want to squash the last 3 commits on your feature branch:

git checkout feature/user-auth
git rebase -i HEAD~3

In the interactive rebase editor, mark commits to squash.

Creating a Tag for Release

After merging all features and fixing bugs, create a release tag:

git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0

Undoing Changes

Discard Changes in Working Directory

If you make a mistake and want to discard changes:

git checkout -- auth.js

Unstage a File

If you accidentally stage a file:

git reset HEAD styles.css

Revert a Commit

If you need to undo a commit:

git revert <commit-hash>

Cleaning Up

Delete Merged Branches

After merging feature branches, delete them:

git branch -d feature/user-auth
git branch -d feature/data-visualization
git push origin --delete feature/user-auth
git push origin --delete feature/data-visualization

Clean Untracked Files

Remove untracked files (be careful with this!):

git clean -n  # Dry run
git clean -f  # Actually remove files

Git Workflow Best Practices

  1. Commit often: Make small, focused commits that address a single issue or feature.

  2. Write meaningful commit messages: Use clear and descriptive commit messages that explain what changes were made and why.

  3. Use branches: Create a new branch for each feature or bug fix to keep your work organized and isolated.

  4. Pull before you push: Always pull the latest changes from the remote repository before pushing your own changes to avoid conflicts.

  5. Review your changes: Use git diff and git status to review your changes before committing.

  6. Use .gitignore: Create a .gitignore file to exclude files and directories that shouldn't be tracked by Git (e.g., build artifacts, temporary files).

  7. Keep your repository clean: Regularly delete merged branches and use git clean to remove untracked files.

  8. Use meaningful branch names: Name your branches descriptively, e.g., "feature/user-authentication" or "bugfix/login-error".

  9. Rebase feature branches: Use git rebase to keep your feature branches up-to-date with the main branch and maintain a clean history.

  10. Use tags for releases: Tag important commits, especially for releases, to easily reference them later.

2024 © All rights reserved - buraxta.com