The following article explains Advanced Git Concepts like Gitignore, Git Hooks, Git Stash, and Git Rebase.
gitignore
- Create a new Git repository or navigate to an existing one.
- Create a file named “.gitignore” in the root directory of your project.
- Open the “.gitignore” file in a text editor.
- Add patterns to the file to specify which files or directories should be ignored by Git. For example, you can add “*.log” to ignore all log files or “build/” to ignore the “build” directory.
- Save and close the “.gitignore” file.
- Run
git add .gitignoreto stage the “.gitignore” file. - Commit the changes using
git commit -m "Add .gitignore file". - Git will now ignore the files or directories specified in the “.gitignore” file during subsequent operations like adding, committing, or pushing.
Git Hooks
- Navigate to a Git repository where you want to set up a hook.
- Inside the “.git” directory, navigate to the “hooks” directory.
- Choose a hook you want to set up (e.g., “pre-commit”, “post-commit”, etc.).
- Create a shell script file with the same name as the chosen hook (e.g., “pre-commit.sh”).
- Open the shell script file in a text editor and add your desired commands or actions. For example, you can run linting or formatting checks before committing code in the “pre-commit” hook.
- Save and close the shell script file.
- Make the shell script file executable by running
chmod +x <hook_name>.sh. Replace<hook_name>with the actual name of the hook file. - Perform the action that triggers the hook (e.g., make a commit) to execute the custom script.
Git Stash
- Make changes to your code or modify files in your Git repository.
- Run
git stashto stash your changes. Git will save the changes and revert your working directory to the last committed state. - Make additional changes or switch branches to work on something else.
- To retrieve the stashed changes, use
git stash applyorgit stash pop. The former applies the stash without removing it, while the latter applies the stash and removes it from the stash list. - Git may encounter conflicts while applying the stash. Resolve any conflicts manually by editing the files.
- After resolving conflicts, stage the changes using
git add .and commit them usinggit commit.
Git Rebase
- Create a new branch or switch to an existing branch in your Git repository.
- Make some changes and commit them to the branch.
- Switch to the branch you want to rebase onto (e.g., main branch) using
git checkout <branch_name>. - Run
git pullto ensure you have the latest changes from the branch you’re rebasing onto. - Run
git rebase <branch_to_rebase>to initiate the rebase process. Replace<branch_to_rebase>with the name of the branch containing your commits. - Git may encounter conflicts during the rebase process. Resolve any conflicts manually by editing the files.
- After resolving conflicts, stage the changes using
git add .. - Use
git rebase --continueto proceed with the rebase after resolving conflicts. - Repeat steps 6-8 until the rebase is complete.
- Review and verify that the rebased branch has the desired changes.
- Finally, push the rebased branch using
git push origin <branch_name>.
Further Reading
How Git Transforms Your Development Process?
Innovative Project Ideas on Cloud Resource Provisioning
Tools for Performing Cloud Resource Provisioning
How to Organize a Contest on Git and GitHub?
10 Unique Themes for a Git and GitHub Contest
When should we prefer to React over PHP?
Innovative Project Ideas in Terraform
20+ Interview Questions on Chaos Engineering
Examples of Array Functions in PHP
10 Basic Hands-On Exercises on Git and GitHub
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
