Git | Tips and Tricks

hbolajraf

Hassan BOLAJRAF

Posted on July 23, 2024

Git | Tips and Tricks
Note
You can check other posts on my personal website: https://hbolajraf.net

Git Tips and Tricks

Git is a powerful version control system that can make your development workflow more efficient. Here are some tips and tricks to help you get the most out of Git.

Configure Git

Before you start using Git, it's a good idea to configure it with your name and email address. This information will be associated with your commits.

git config --global user.name "hbolajraf"
git config --global user.email "hassan.bolajraf@gmail.com"
Enter fullscreen mode Exit fullscreen mode

You can also set other configurations, such as your preferred text editor and default branch.

Basic Commands

1. Initialize a Repository: To start a new Git repository, use git init in your project directory.

2. Clone a Repository: To clone a repository from a remote URL, use git clone <URL>.

3. Commit Changes: After making changes, use git commit -m "Your commit message" to save them.

4. Check the Status: Use git status to see the status of your working directory.

Branching

Branches are essential for managing different lines of development.

1. Create a Branch: Use git branch <branch_name> to create a new branch.

2. Switch Branches: To switch to a different branch, use git checkout <branch_name>.

3. Merge Branches: Merge changes from one branch into another with git merge <branch_name>.

4. Delete Branch: Use git branch -d <branch_name> to delete a branch.

Stashing

Stashing is useful when you need to save your changes temporarily.

1. Stash Changes: Use git stash to save your changes.

2. Apply Stash: To reapply your changes, use git stash apply.

3. List Stashes: See a list of stashes with git stash list.

Interactive Rebase

Interactive rebase allows you to modify commit history.

1. Rebase Interactive: Use git rebase -i HEAD~n to interactively rebase the last n commits.

2. Edit Commits: Change "pick" to "edit" to modify a commit.

3. Amend Commits: Use git commit --amend to edit the current commit.

Git Aliases

Git aliases let you create shortcuts for Git commands.

1. Create an Alias: Add an alias to your global Git configuration.

git config --global alias.co checkout
Enter fullscreen mode Exit fullscreen mode

2. Usage: Now, you can use git co as a shorthand for git checkout.

Git Hooks

Git hooks are scripts that run automatically on certain Git events.

1. Pre-Commit Hook: Create a .git/hooks/pre-commit script to run actions before a commit.

2. Post-Receive Hook: In a server's Git repository, create a hooks/post-receive script to perform actions after receiving a push.

Ignoring Files

You can specify files or patterns to ignore using a .gitignore file.

1. Create .gitignore: Create a file named .gitignore and list the files, directories, or patterns you want to ignore.

2. Example .gitignore:

# Ignore build artifacts
bin/
obj/

# Ignore log files
*.log

# Ignore a specific directory
docs/
Enter fullscreen mode Exit fullscreen mode

What Next?

These tips and tricks will help you become more proficient with Git, making your version control tasks more efficient and your development process smoother.

💖 💪 🙅 🚩
hbolajraf
Hassan BOLAJRAF

Posted on July 23, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Git | Tips and Tricks
git Git | Tips and Tricks

July 23, 2024