DEV Community

Cover image for Git Secrets Unveiled: 20 Advanced Techniques for Efficient Coding
Usman Awan
Usman Awan

Posted on

Git Secrets Unveiled: 20 Advanced Techniques for Efficient Coding

Beyond the Basics: 20 Essential Git Techniques Every Developer Should Master

Version control is an essential aspect of modern software development, allowing teams to collaborate efficiently and manage code changes systematically. Among the various tools available, Git stands out as a powerful and widely adopted version control system, enabling developers to track modifications, experiment with new features, and revert to previous states effortlessly. While many developers are familiar with basic Git commands, mastering advanced techniques can significantly enhance productivity and streamline workflows. In this article, we’ll explore 20 advanced Git techniques that every developer should know, equipping you with the skills to navigate complex version control scenarios with confidence and efficiency.

1. Add & Commit

You can streamline your workflow by using the -am flag to add and commit changes in a single command. Instead of running separate commands:

$ git add .
$ git commit -m "new project"
Enter fullscreen mode Exit fullscreen mode

Use:

$ git commit -am "new project"
Enter fullscreen mode Exit fullscreen mode

This command stages modified files and commits them with the specified message.

2. Amend Commits

To rename your last commit message or include additional changes, use:

$ git commit --amend -m "New Message"
Enter fullscreen mode Exit fullscreen mode

To add changes while retaining the original message, stage changes first:

$ git add .
$ git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

3. Override Remote History

To push local commits and override the remote repository’s history, use the --force flag. Caution: This action rewrites the remote history:

$ git push origin master --force

4. Revert Commits

To undo a commit without removing it from the history, use the git revert command. First, view your commit history with:

$ git log --oneline

Enter fullscreen mode Exit fullscreen mode

Then revert a commit using its ID:

$ git revert <commit-id>
Enter fullscreen mode Exit fullscreen mode

5. Codespaces

GitHub Codespaces allows you to edit and run code directly in your browser. Access this feature by pressing the period key (".") in your preferred repository for a full VSCode interface.

6. Stash Changes

To save your current progress without committing, use the stash command:

$ git stash save "work in progress"
Enter fullscreen mode Exit fullscreen mode

View your stash list:

$ git stash list
Enter fullscreen mode Exit fullscreen mode

Retrieve stashed changes with:

$ git stash apply stash@{0}
Enter fullscreen mode Exit fullscreen mode

7. Rename Branches

Rename your current branch to a more appropriate name:

$ git branch -M new-branch-name
Enter fullscreen mode Exit fullscreen mode

8. Decorate Logs

For a clearer view of your commit history, use:

$ git log --graph --decorate --oneline

Enter fullscreen mode Exit fullscreen mode

This visually represents your commit history, including branch merges.

9. Switch Back to Previous Branch

Return to the last branch you were on with:

$ git checkout -

Enter fullscreen mode Exit fullscreen mode

10. Sync with Remote Repository

Synchronize your local repository with the remote and discard local changes:

$ git fetch origin
$ git reset --hard origin/master
Enter fullscreen mode Exit fullscreen mode

Remove untracked files:

$ git clean -df
Enter fullscreen mode Exit fullscreen mode

11. Create a New Branch and Switch

You can create and switch to a new branch in a single command:

$ git checkout -b new-branch-name
Enter fullscreen mode Exit fullscreen mode

12. Cherry-Pick Commits

To apply specific commits from one branch to another without merging, use the cherry-pick command:

$ git cherry-pick <commit-id>

Enter fullscreen mode Exit fullscreen mode

13. Use Interactive Rebase

Rearrange, edit, or squash commits with an interactive rebase. Start the process with:

$ git rebase -i HEAD~n
Enter fullscreen mode Exit fullscreen mode

Replace n with the number of commits you want to modify. This opens an editor where you can choose your actions.

14. Track Remote Branches

To set a local branch to track a remote branch, use:

$ git branch --set-upstream-to=origin/remote-branch local-branch
Enter fullscreen mode Exit fullscreen mode

15. View Differences

To see what has changed between your working directory and the last commit:

$ git diff
Enter fullscreen mode Exit fullscreen mode

To view changes staged for the next commit:

$ git diff --cached
Enter fullscreen mode Exit fullscreen mode

16. Use Git Tags

To mark specific points in your project history, use tags. Create a lightweight tag with:

$ git tag tag-name
Enter fullscreen mode Exit fullscreen mode

For an annotated tag, which includes metadata:

$ git tag -a tag-name -m "Tag message"
Enter fullscreen mode Exit fullscreen mode

17. Compare Branches

To compare the differences between two branches, use:

$ git diff branch1..branch2
Enter fullscreen mode Exit fullscreen mode

18. View Commit History with Statistics

To view a commit history with the number of changes in each commit:

$ git log --stat
Enter fullscreen mode Exit fullscreen mode

19. Reflog

If you need to recover lost commits, reflog tracks changes to the tips of branches. Use it to view your command history:

$ git reflog
Enter fullscreen mode Exit fullscreen mode

You can then reset to a specific state:

$ git reset --hard HEAD@{index}
Enter fullscreen mode Exit fullscreen mode

20. Configure Aliases

Create custom shortcuts for your frequently used commands to save time. For example, to create an alias for checking the status:

$ git config --global alias.st status
Enter fullscreen mode Exit fullscreen mode

You can now use git st to check your status.

Conclusion

This article highlights essential advanced Git techniques crucial for software developers and data scientists working collaboratively. Mastering these commands can significantly enhance your productivity and help you navigate version control challenges with ease.

Top comments (0)