top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Five Must-Know Advanced Git Commands for Developers


Git is an essential version control tool, and learning its core commands can significantly improve the source code management skills. This blog will walk you through some of the most important Git commands, including log, rebase, merge, stash, and how to undo a commit. By the end of this article, you will understand how to better manage your codebase.


Introduction:

Git has a lot of features and can be overwhelming, especially for new users. This article focuses on five commands that are especially beneficial for typical Git operations. Whether you're working on a single project or with a team, these commands will help you have a clean and effective workflow.

 

Git Log:

 

Displays the commit history. It shows the author, date, and commit message for each commit. It can be customized in a variety of ways to show the information you require in the format you wish for.

 

 

The basic git log command displays the commit history, including hashes, authors, dates, and messages. You may press ‘q’ to exit the display of logs.

 

 To limit the number of commits displayed by git log, you can use the -n or --max-count option followed by the number of commits you want to see.

The one-line log style displays each commit on a single line, making it easier to navigate a large number of commits.

To display the commit history as a graph with a set number of commits:

git log --graph

"After" option displays all commits made after the specified date: 

git log --after="yyyy-mm-dd"


Git Rebase: 

 Rebase serves as an alternative to a pull request. It allows you to merge all new commits made after a branch divergence into a new branch and place the changes at the base's tip. This is useful if you want to include changes from another branch without using a merge.

 

To rebase a branch, run the git rebase command. Here's an example of how to rebase one branch onto another:

 

For Example you have a feature branch (feature) and you want to rebase it onto the main branch (main).

git checkout feature:

git rebase main


Git Merge 

Merging is the process of integrating recent changes from several branches into a single new commit that will apply to all of those branches. In some ways, merging is the extension to branching in version control: a branch allows you to collaborate with others on a specific collection of files at the same time, but a merge allows you to subsequently integrate separate work on branches that diverged from a common ancestor commit.

 

Steps:

1.     Checkout the Branch to Merge Into: git checkout main

2.     Merge the Feature Branch: git merge feature

Git Merge vs Rebase

·        Merge takes all the changes in one branch and merges them into another branch in one commit.

·        Rebase says I want the point at which I branched to move to a new starting point

So when do you use either one?

Merge

·        Let's say you have created a branch for the purpose of developing a single feature. When you want to bring those changes back to master, you probably want merge.

Rebase

A second scenario would be if you started doing some development and then another developer made an unrelated change. You probably want to pull and then rebase to base your changes from the current version from the repository.

 

Squashing: All commits are preserved in both cases (for example: "add feature", then "typo", then "oops typo again"...). Commits can be combined into a single commits by squashing. Squashing can be done as part of a merge or rebase operation (--squash flag), in which case it's often called a squash-merge or a squash-rebase.

 

 

Undoing a Commit

Sometimes you need to undo a commit. Depending on the circumstances, Git offers numerous options for doing so.

 

Amend the Last Commit

If you want to make changes to the last commit, you can use:

git commit –amend 

Revert a Commit

To undo a commit and create a new commit that reverses the changes:

git revert <commit-hash> 

Reset to a Previous State

To move the current branch to a specific commit, discarding all changes since then:

 

Soft Reset: Keeps changes in the working directory.

git reset --soft <commit-hash> 

Hard Reset: Discards all changes.

git reset --hard <commit-hash>

 

Git stash  

 

Git stash temporarily stores (or stashes) modifications you've made to your working copy so you can work on something else before returning to re-apply them later. Stashing is useful when you need to rapidly switch context and work on something else, but you're still in the middle of a code change and aren't ready to commit.


Stash Changes

git stash


List Stashes

git stash list

 

Apply a Stash

git stash apply

Apply and Drop a Stash

            git stash pop


Conclusion

Understanding and applying fundamental Git commands—rebase, log, merge, stash, and reset—will significantly enhance your ability to manage your code. These commands are essential for ensuring a clean, efficient, and structured workflow in both solo and collaborative projects. Mastering these git commands will improve your development process and ensure that your project's version control is strong and dependable. Each one of these commands have more options to explore based on the situation.

19 views0 comments

Comentarios

Obtuvo 0 de 5 estrellas.
Aún no hay calificaciones

Agrega una calificación
bottom of page