Advanced Git Commands

Advanced Git Commands

Β·

3 min read

Mastering Merge, Conflicts, Pull Requests, Reset, Revert, Rebase, Stashing, and Cherry-Picking.

Git is a powerful version control system used by developers worldwide to manage code changes efficiently. While most of us are familiar with basic commands like git add, git commit, and git push, mastering advanced Git commands can elevate your skills and streamline your workflow. In this blog, we'll explore some advanced Git commands, including how to handle merge conflicts, work with pull requests, and more.


1. Git Merge and Resolving Conflicts

What is Git Merge?

git merge is used to combine changes from two branches into a single branch. Typically, it merges the feature branch back into the main branch.

Command Syntax:

git checkout main
git merge feature-branch

Handling Merge Conflicts

Conflicts occur when changes in the two branches affect the same lines of code. Git marks the conflicting sections in the files.

Steps to Resolve Conflicts:

  1. Open the conflicting file(s) in your text editor.

  2. Look for conflict markers:

     <<<<<<< HEAD
     Your changes
     =======
     Changes from feature-branch
     >>>>>>> feature-branch
    
  3. Decide which changes to keep and edit the file accordingly.

  4. After resolving conflicts, mark the file as resolved:

     git add <file_name>
    
  5. Complete the merge process:

     git commit
    

2. GitHub Pull Request (PR)

Pull requests are used to review and merge code changes in a collaborative environment, especially when working on GitHub.

Steps to Create a Pull Request:

  1. Push your changes to the remote repository:

     git push origin feature-branch
    
  2. Navigate to your repository on GitHub.

  3. Click on the "Pull Requests" tab and select "New Pull Request."

  4. Compare your feature branch with the target branch (e.g., main) and create the pull request.

Once submitted, team members can review, discuss, and approve your changes before merging.


3. Reset and Revert in Git

Git Reset

git reset undoes changes in the repository history.

Command Syntax:

  • Soft reset (keeps changes staged):

      git reset --soft HEAD~1
    
  • Mixed reset (unstages changes):

      git reset --mixed HEAD~1
    
  • Hard reset (deletes changes):

      git reset --hard HEAD~1
    

Git Revert

git revert creates a new commit that undoes changes from a previous commit without altering the commit history.

Command Syntax:

git revert <commit-hash>

4. Git Rebase

git rebase is used to move or combine commits from one branch to another, resulting in a cleaner commit history.

Command Syntax:

git checkout feature-branch
git rebase main

Interactive Rebase:

git rebase -i HEAD~<number_of_commits>

This opens an editor where you can squash, edit, or drop commits as needed.


5. Stashing in Git

Stashing temporarily shelves your changes, allowing you to switch branches or work on something else without committing incomplete changes.

Command Syntax:

  • Save changes to stash:

      git stash
    
  • Apply stashed changes:

      git stash apply
    
  • List stashed changes:

      git stash list
    
  • Drop a specific stash:

      git stash drop stash@{0}
    

6. Cherry-Picking in Git

Cherry-picking allows you to apply a specific commit from one branch to another.

Command Syntax:

  1. Find the commit hash:

     git log
    
  2. Apply the commit to the current branch:

     git cherry-pick <commit-hash>
    

This is particularly useful for bug fixes or small features you want to move to another branch without merging all changes.


Conclusion

Mastering advanced Git commands like merge, rebase, stashing, and cherry-picking empowers you to manage complex workflows efficiently. Whether resolving conflicts, collaborating via pull requests, or cleaning up commit history, these commands are essential tools for every developer. Start practicing today and elevate your Git expertise!

Thank you for reading :)
Ramya R

Β