Git Like a Pro: 8 Essential Commands and Workflows

Daniel
3 min readApr 13, 2023

--

Photo by Roman Synkevych 🇺🇦 on Unsplash

In the world of software development, managing source code effectively is crucial. Git has become the de facto standard for version control, enabling collaboration and safeguarding your code. However, Git can be overwhelming for beginners and even seasoned developers. In this article, we’ll explore eight essential Git commands and workflows to help you navigate the Git ecosystem like a pro, streamline your development process, and improve your overall coding experience.

1. Branching Made Easy

One of Git’s greatest features is its support for branching. Branches enable you to work on different features or bug fixes simultaneously, without affecting the main codebase. To create a new branch and switch to it, use the following command:

git checkout -b your-branch-name

This command is a shorthand for creating a new branch (git branch your-branch-name) and switching to it (git checkout your-branch-name).

2. Stashing Changes

Sometimes, you may find yourself in the middle of working on a feature or bugfix when you need to switch branches. Git stash allows you to save your current changes temporarily without committing them:

git stash save "Your stash message"

To view your stashed changes, use git stash list. To apply your stashed changes to the current branch, use git stash apply followed by the stash reference (e.g., stash@{0}).

3. Interactive Rebasing

Interactive rebasing allows you to clean up your commit history by combining, editing, or reordering commits. To perform an interactive rebase, use the following command:

git rebase -i HEAD~n

Replace “n” with the number of commits you want to rebase. A text editor will open, allowing you to choose how to modify each commit.

4. Cherry-Picking Commits

If you want to apply a specific commit from one branch to another, use the cherry-pick command:

git cherry-pick commit-hash

Replace “commit-hash” with the hash of the commit you want to apply. This command creates a new commit on your current branch with the changes from the specified commit.

5. Aliases for Efficiency

Git allows you to create aliases for frequently used commands, making your workflow more efficient. To set an alias, use the following command:

git config --global alias.shortcut "command"

Replace “shortcut” with your desired alias and “command” with the Git command you want to shorten.

6. Reflog Rescue

The Git reflog is a log of every operation that modifies the repository’s HEAD. If you’ve accidentally deleted a branch or lost a commit, the reflog can help you recover it:

git reflog

Search the reflog for the lost commit or branch, and use git checkout or git branch to restore it.

7. Partial Commits

When you have multiple changes in your working directory but want to commit them separately, use the git add -p command. This command prompts you to stage changes for each modified file interactively:

git add -p

Review each change and choose whether to stage, skip, or split the change into smaller parts.

8. Visualizing Your Commit History

To visualize your commit history as a graphical representation, use the following command:

git log --graph --oneline --decorate --all

This command displays a compact, graphical view of the commit history, making it easier to understand the project’s history and branch relationships.

Photo by Roman Synkevych 🇺🇦 on Unsplash

Mastering Git commands and workflows is essential for any developer looking to streamline their development process and collaborate effectively with others. By incorporating these eight essential Git commands and workflows into your daily routine, you’ll be well on your way to becoming a Git pro. Remember, practice makes perfect, and the more you use Git, the more comfortable and efficient you’ll become. Happy coding!

--

--

Daniel

Passionate programmer & tech enthusiast, exploring cutting-edge technologies. Avid reader & coder, dedicated to sharing knowledge on Medium.