Git

Advanced Git Concepts – Gitignore, Git Hooks, Git Stash, and Git Rebase

The following article explains Advanced Git Concepts like Gitignore, Git Hooks, Git Stash, and Git Rebase.

gitignore

  1. Create a new Git repository or navigate to an existing one.
  2. Create a file named “.gitignore” in the root directory of your project.
  3. Open the “.gitignore” file in a text editor.
  4. 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.
  5. Save and close the “.gitignore” file.
  6. Run git add .gitignore to stage the “.gitignore” file.
  7. Commit the changes using git commit -m "Add .gitignore file".
  8. Git will now ignore the files or directories specified in the “.gitignore” file during subsequent operations like adding, committing, or pushing.

Git Hooks

  1. Navigate to a Git repository where you want to set up a hook.
  2. Inside the “.git” directory, navigate to the “hooks” directory.
  3. Choose a hook you want to set up (e.g., “pre-commit”, “post-commit”, etc.).
  4. Create a shell script file with the same name as the chosen hook (e.g., “pre-commit.sh”).
  5. 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.
  6. Save and close the shell script file.
  7. Make the shell script file executable by running chmod +x <hook_name>.sh. Replace <hook_name> with the actual name of the hook file.
  8. Perform the action that triggers the hook (e.g., make a commit) to execute the custom script.

Git Stash

  1. Make changes to your code or modify files in your Git repository.
  2. Run git stash to stash your changes. Git will save the changes and revert your working directory to the last committed state.
  3. Make additional changes or switch branches to work on something else.
  4. To retrieve the stashed changes, use git stash apply or git stash pop. The former applies the stash without removing it, while the latter applies the stash and removes it from the stash list.
  5. Git may encounter conflicts while applying the stash. Resolve any conflicts manually by editing the files.
  6. After resolving conflicts, stage the changes using git add . and commit them using git commit.

Git Rebase

  1. Create a new branch or switch to an existing branch in your Git repository.
  2. Make some changes and commit them to the branch.
  3. Switch to the branch you want to rebase onto (e.g., main branch) using git checkout <branch_name>.
  4. Run git pull to ensure you have the latest changes from the branch you’re rebasing onto.
  5. Run git rebase <branch_to_rebase> to initiate the rebase process. Replace <branch_to_rebase> with the name of the branch containing your commits.
  6. Git may encounter conflicts during the rebase process. Resolve any conflicts manually by editing the files.
  7. After resolving conflicts, stage the changes using git add ..
  8. Use git rebase --continue to proceed with the rebase after resolving conflicts.
  9. Repeat steps 6-8 until the rebase is complete.
  10. Review and verify that the rebased branch has the desired changes.
  11. 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?

Applications of Terraform

Innovative Project Ideas in Terraform

20+ Interview Questions on Chaos Engineering

Examples of Array Functions in PHP

Git Best Practices

10 Basic Hands-On Exercises on Git and GitHub

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *