Git -

Tips and Tricks


Introduction

Mastering Git involves learning not only the basics but also the tips and tricks that can enhance your productivity and make your workflow smoother. This tutorial covers various Git tips and tricks, from shortcuts to advanced features, that can help you become a more efficient developer.


1. Undoing the Last Commit

Sometimes you may need to undo the last commit. You can do this without losing your changes using the `git reset` command.

        
            # Example of undoing the last commit:
# Undo the last commit but keep the changes:
git reset --soft HEAD~1
# Undo the last commit and discard the changes:
git reset --hard HEAD~1
        
    

This example demonstrates how to undo the last commit while keeping your changes.


2. Stashing Changes

Git stash allows you to temporarily save changes that you are not ready to commit. This is useful when you need to switch branches or work on something else.

        
            # Stashing changes:
# Stash changes in the working directory:
git stash
# List all stashes:
git stash list
# Apply the most recent stash:
git stash apply
# Apply a specific stash:
git stash apply stash@{2}
# Drop a specific stash:
git stash drop stash@{2}
        
    

This example shows how to stash and apply changes using Git.


3. Aliases for Common Commands

Git aliases can save you time by shortening commonly used commands. You can set up aliases in your Git configuration file.

        
            # Example of creating Git aliases:
# Create an alias for the git status command:
git config --global alias.st status
# Create an alias for the git log command with pretty formatting:
git config --global alias.lg "log --oneline --graph --decorate --all"
        
    

This example demonstrates how to create and use Git aliases.


4. Interactive Rebase

Interactive rebase allows you to edit, reorder, and squash commits. This can be useful for cleaning up your commit history before merging.

        
            # Example of interactive rebase:
# Start an interactive rebase for the last 3 commits:
git rebase -i HEAD~3
# In the interactive editor, choose the action for each commit (e.g., pick, squash, edit).
# Save and close the editor to complete the rebase.
        
    

This example shows how to perform an interactive rebase.


5. Viewing Commit History Graphically

Git provides several tools to view commit history graphically, such as `gitk` and `git log --graph`.

        
            # Example of viewing commit history graphically:
# Use gitk to view the commit history:
gitk
# Use git log with graph option:
git log --graph --oneline --decorate --all
        
    

This example demonstrates how to view the commit history graphically.


6. Checking Out Previous Commits

You can check out previous commits to see the project state at a specific point in time. This can be useful for debugging or understanding the history of changes.

        
            # Example of checking out previous commits:
# Checkout a previous commit by its hash:
git checkout <commit_hash>
# Create a new branch from the checked out commit:
git checkout -b new-branch-name
        
    

This example shows how to check out previous commits in Git.


7. Cleaning Up Untracked Files

The `git clean` command can be used to remove untracked files from your working directory. This helps keep your workspace clean.

        
            # Example of cleaning up untracked files:
# Show what would be removed:
git clean -n
# Remove untracked files:
git clean -f
# Remove untracked directories:
git clean -fd
        
    

This example demonstrates how to clean up untracked files in Git.


8. Using Bisect to Find Bugs

The `git bisect` command helps you find the commit that introduced a bug by performing a binary search through your commit history.

        
            # Example of using git bisect to find a bug:
# Start bisecting:
git bisect start
# Mark the current commit as bad:
git bisect bad
# Mark an earlier commit as good:
git bisect good <commit_hash>
# Git will checkout a commit for you to test. Mark it as good or bad.
# Continue marking commits as good or bad until you find the problematic commit.
# Once done, end the bisect session:
git bisect reset
        
    

This example shows how to use `git bisect` to find a buggy commit.


9. Amending Commits

You can amend the most recent commit to include changes that you forgot to add or to update the commit message.

        
            # Example of amending the most recent commit:
# Amend the most recent commit with new changes:
git add .
git commit --amend
# Amend the most recent commit message:
git commit --amend -m "New commit message"
        
    

This example demonstrates how to amend the most recent commit in Git.


10. Cherry-Picking Commits

The `git cherry-pick` command allows you to apply changes from specific commits to your current branch. This is useful for incorporating individual bug fixes or features.

        
            # Example of cherry-picking commits:
# Cherry-pick a commit by its hash:
git cherry-pick <commit_hash>
        
    

This example shows how to cherry-pick commits in Git.


11. Comparing Branches and Commits

Git provides several commands to compare branches and commits, helping you understand the differences and changes between them.

        
            # Example of comparing branches and commits:
# Compare two branches:
git diff branch1..branch2
# Compare a branch with the current branch:
git diff branch-name
# Compare two commits:
git diff commit1 commit2
        
    

This example demonstrates how to compare branches and commits in Git.


12. Viewing Commit Details

The `git show` command displays detailed information about a specific commit, including the changes made and the commit message.

        
            # Example of viewing commit details:
# Show details of a specific commit:
git show <commit_hash>
        
    

This example shows how to view detailed information about a commit in Git.


13. Managing Large Repositories

Managing large repositories efficiently involves using tools like Git LFS (Large File Storage) to handle large files and optimize performance.

        
            # Example of managing large repositories with Git LFS:
# Install Git LFS:
git lfs install
# Track a specific file type with Git LFS:
git lfs track "*.psd"
# Add and commit changes as usual:
git add .gitattributes
git add <file_name>.psd
git commit -m "Add PSD file with Git LFS"
        
    

This example demonstrates how to use Git LFS for managing large repositories.


14. Best Practices for Using Git

Follow these best practices to make the most of Git:



Conclusion

Mastering Git involves not only understanding the basics but also learning various tips and tricks that can enhance your productivity. By incorporating these tips and tricks into your workflow, you can become a more efficient developer and make the most of Git's powerful features.