Advance Git & GitHub for DevOps Engineers: Part-2
We are exploring more deep in GIT. So these GIT commands and concepts which we will be discussing here are very important for every IT professionals. Many topic which we will be discussing today are important interview questions which you will face so let begin to explore.
Git Stash:
Git stash is a powerful feature in Git that allows you to temporarily save changes in your working directory that you aren't ready to commit yet. This is particularly useful when you need to switch branches or work on something else without committing unfinished work.
When you stash your changes, Git takes your modified tracked files, stages changes, and saves them on a stack of unfinished changes that you can reapply at any time.
Common Git Stash Commands
Stash Changes:- git stash
This command saves your local modifications away and reverts the working directory to match the HEAD commit.
List Stashed Changes:- git stash list
This shows a list of stashed changes.
Pop Stashed Changes: - git stash pop
This reapplies the most recent stash and removes it from the stash list.
Git stash is a versatile tool for temporarily shelving your work and managing your workflow without committing incomplete changes. It helps maintain a clean working directory and smooth transitions between tasks.
Git Squash
Git squash is a technique used to combine multiple commits into a single commit. This is particularly useful for cleaning up a commit history before merging a feature branch into the main branch. Squashing commits can help maintain a more readable and concise project history.
When to Use Git Squash
How to Squash Commits
Interactive Rebase
Interactive rebase is the most common way to squash commits in Git.
Start an Interactive Rebase:- git rebase -i HEAD~n
Replace n with the number of commits you want to squash. For example, if you want to squash the last 3 commits, use HEAD~3.
Modify the Rebase Todo List An editor will open with a list of commits. The default action is pick. Change the action of the commits you want to squash to squash (or s).
pick e1e3f5c Commit message 1
领英推荐
squash d4f3c3e Commit message 2
squash b5c4f2e Commit message 3
Save and Close the Editor Save the file and close the editor. Git will squash the commits and open another editor window for you to edit the commit message of the squashed commit.
Edit the Commit Message Modify the commit message as needed, then save and close the editor.
Squashing During Merge
You can also squash commits during a merge operation using the --squash option.
Git squash is a useful technique for cleaning up commit history by combining multiple commits into a single commit. It can be done through interactive rebase or during a merge operation, making it easier to maintain a concise and understandable project history.
Cherry-pick:
Git cherry-pick is a powerful command that allows you to apply a specific commit from one branch to another. This is useful when you want to incorporate a particular change without merging entire branches.
When to Use Git Cherry-Pick
How to Use Git Cherry-Pick
Identify the Commit to Cherry-Pick Find the commit hash (SHA) of the commit you want to cherry-pick. You can use git log to view the commit history and identify the commit hash.
git log
Switch to the Target Branch Checkout the branch where you want to apply the commit.
git checkout target-branch
Cherry-Pick the Commit Use the git cherry-pick command followed by the commit hash.
git cherry-pick <commit-hash>
Git cherry-pick is a valuable tool for applying specific changes from one branch to another without merging entire branches. It allows for selective incorporation of commits, making it easier to manage project history and apply targeted fixes or features.