In this article this is what we are going to learn
- How to use the
git reset
command? How does it work? - Step by Step code example for undoing the most recent commit, using
git reset HEAD~
- Discussing the use of
--soft
,--mixed
and--hard
flags
2. Reverting changes with git revert
- What is
git revert
and when to usegit revert
instead ofgit reset
- Code example explaining how to revert a specific commit without altering project history
3. Using git reflog
to navigate and restore commit history
git reflog
a tool for viewing all past commits, even the ones removed from branch history- code example to demonstrate how to use
git reflog
withgit reset
to revert to a specific point in history
4. Bonus-managing-merge-conflicts-after-reverting-commits
- Overview of merge conflicts
- Strategies for resolving conflicts effciently with code examples
5. Bonus: Best Practices for Commit Management
- Using clear and descriptive titles
- Using branches effectively for feature development and testing
-
git cherry-pick
command and its uses
Immediate undo with git reset
You can immediately undo changes with the git reset
command. With git reset you can revert the project to previous state without altering the files ( depending on the options selected). In this section we are going to learn how to effectively use the git reset command
git reset changes the current state of a branch to a specified commit thus effectively undoing the subsequent commits
here are the primary flags that you can use with git reset
--soft
:--mixed
(default)--hard
These flags are explained in detail in the below coding example
each of these flags does a different level of undo, affecting commit history and the state of files in the working directory
Coding example with git reset
- Open the terminal and navigate to the project root directory
- to undo the most recent commit type the below command
git reset HEAD~
What are we doing here?
We are using the HEAD~ which means we want to revert to the commit just before the current HEAD.
We are also not signifying any options for the git reset
command and thus it will default to mixed
which means it will revert any changes but not affect any files in the working directory
here is an example with the --hard
option
git reset --hard HEAD~
3. You can verify the changes by checking the status like so
git status
You can see the changes from the undone commit in the staging area
Explaining different flag options namely --soft
, --mixed
and --hard
.
--soft
: This will undo your commits but leave your changes staged. You can use the--soft
option to modify commit messages or combine them with another commit--mixed
(default): This undo the commit and unstages the changes, but leaves them in your working directory. This is a safe option because it does not risk losing any changes.--hard
: the undoes the commit, unstages the changes and discards all the changes and the files in the working directory, since the commit. This could lead to loss of work when not used properly
Revert Changes with git revert
git revert
creates a new commit that undo the changes that were made by the previous commit (one or more previous commit).
Contract it with git reset
which basically erases the commit (one or more previous commits) from the commit history
This approach ensures that project history remains intact and traceable, thus it is a good method for undoing the changes when working in a shared repo with code that has already been shared with other people
Git revert
is Non Destructive: It does not erases the commit, creates a new commit that undo the changes by one or more previous commits- Teams and collaborative environments: When working in teams you might want to not erase tehe faulty comit from history
When to use git revert
and when to use git reset
- Shared History: Use the Git revert when you want to undo the changes that have been pushed to a public repository. This is because the git reset deletes the history which might cause confusion and might create conflicting histories. Using git revert keeps the history consistant for all contributers
- Preserving History: If we are maintaining a detailed record for all the changes that are occuring the repository then you want to use the
git revert
command. The git revert command allows you to see what was undone and why it was undone by the developer.
Code example using git revert
Let us consider an example of git revert
. Here we have a commit with hash jdfhg4535289
that we want to revert
- open the terminal and go to the working directory of your project
- use the git revert like
This command creates a new commit that will undo the changes of the
commit.jdfhg4535289
The new commit will create a default commit message that will indicate which commit is being reverted but you can customize the message as well as you see fit
Git might automatically open your code editor as well in order to let you specify any commit message you might want to
3. Resolve any conflicts that might arise
- If the changes introduced by the commit
jdfhg4535289
conflict with changes in subsequent commit (the commit that will be there once you undo this commit) you will be asked by Git to resolve these conflicts manually - After you have resolved the conflicts you will be asked to finalize the revert by completing the revert process. This might involve adding the resolved files to the staging area using
git add
and then completing the commit usinggit commit
4. Verify the operation using git log
You can verify the operation that has been completed using the git log
command. You can see a new commit that revert the changes of the jdfhg4535289
commit in the
Using git reflog
to navigate and restore commit history
git reflog
gives a record of the git branches and other references in your repository. This includes commits, resets, merges and rebases
This includes actions that might alter your visible history, thus making git reflog
useful for recovering lost commits or viewing the project's state at a specific instance in time
Contract this with git log
which just shows your commit history of your current branch
Detailed explanation of git reflog
- Git reflog tracks the tips of the branches and git references within your local repository
- the reflog in
git reflog
means reference log - Each entry in reflog is a reference to a specific point in time thus the reflog provides a chronological list of changes in the repository.
- These changes might include branch commit history, resets or rebases and thus it offers a way to restore lost commits
Code Example for git reflog
In this example we are going to ue git reflog along with git reset to restore a commit.
let us consider a situation where you accidentally reset the current branch to an older commit thus losing several important commits in the process
We can use the git reflog
. to find these lost commits and restore the code to its previous state
- Open the terminal and navigate to the working directory
- type the
git reflog
command to view the recent changes in your repo
git reflog
The commit will give you a list of commits with HEAD, the commit hash and a description of the action (That is commit, reset or merge).
3. Select the entry corresponding to the state that you want to restore
- Each entry in the git reflog shows a snapshot of a specific action. Look for the commit or action that is just before the accidental reset.
4. Restore your branch to the desired state using git reset
git reset --hard HEAD@{the entry from the git reflog};
This command will reset your current branch to the previous branch at the specific point in time.
git reset --hard HEAD@{0}
Metered TURN servers
- API: TURN server management with powerful API. You can do things like Add/ Remove credentials via the API, Retrieve Per User / Credentials and User metrics via the API, Enable/ Disable credentials via the API, Retrive Usage data by date via the API.
- Global Geo-Location targeting: Automatically directs traffic to the nearest servers, for lowest possible latency and highest quality performance. less than 50 ms latency anywhere around the world
- Servers in 12 Regions of the world: Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapore,Sydney, Seoul
- Low Latency: less than 50 ms latency, anywhere across the world.
- Cost-Effective: pay-as-you-go pricing with bandwidth and volume discounts available.
- Easy Administration: Get usage logs, emails when accounts reach threshold limits, billing records and email and phone support.
- Standards Compliant: Conforms to RFCs 5389, 5769, 5780, 5766, 6062, 6156, 5245, 5768, 6336, 6544, 5928 over UDP, TCP, TLS, and DTLS.
- Multi‑Tenancy: Create multiple credentials and separate the usage by customer, or different apps. Get Usage logs, billing records and threshold alerts.
- Enterprise Reliability: 99.999% Uptime with SLA.
- Enterprise Scale: With no limit on concurrent traffic or total traffic. Metered TURN Servers provide Enterprise Scalability
- 50 GB/mo Free: Get 50 GB every month free TURN server usage with the Free Plan
- Runs on port 80 and 443
- Support TURNS + SSL to allow connections through deep packet inspection firewalls.
- Support STUN
- Supports both TCP and UDP
Bonus: Managing merge conflicts after reverting commits
Merge conflicts happen when two branches that have conflicting code are merged together
This can happen in different scenarios including when you revert the changes in one branch
A revert modifies the files to a previous state and if we try to merge this branch with another that has moved forward, a conflict might arise if same parts of the file were edited
Merge conflicts arise when GIT cannot automatically resolve differences in code between two commits.
Thus these commits need to be manually resolved before merging can be completed.
Ways to resolve conflicts
- Understanding the conflict: understand why the conflict is occuring, you can use the
git status
command to see which files have a conflict. - Deciding on the correct changes: See the conflict and decide which changes you want to keep and which one you want to discard
- Using Git tools and editors: Instead of using the
git status
you can use various git tools, even vscode has conflict resolution tool built in that you can use.
Best Practices for Commit Management
- Using clear and descriptive titles: Use titles that clearly describe the changes in the commit in clear and concise manner. One look at the message should tell you why the commit was made
- Add a detailed body when necessary: For complex and big changes create a detailed summary of the changes which should tell the reader what changes have been made to the code and why these changes were made.
- Reference issue tracker IDs: If you are using issue trackers include references of the isuues id in the commit message
- Use a particular style: Decide on a perticular pattern or style of writing the commits and use it in your project.
Using branches effectively for feature development and testing
- Master and development branches: Always keep the master branch clean and deployable and work in seprate development branches
- Feature branches: You can write all the code in the development branch if you are working on a small project. For large projects, keep a seprate branch for each featuer
- Merge strategies: At regular intervals merge the master branch with the feature branches to keep them up to date and avoid large merge conflicts later on.
- Pull requests: always use pull request before merging especially when working as a team or on public projects.
Git cherry-pick command and its uses
Git cherry-pick is a git command that lets you select specific commits from one branch and them apply them to another branch
- Selective feature integration: You want to apply a specific commit from one branch to another branch without merging all the changes
- Hotfixes: If there a cretical bug in the master branch which needs to fixed immideately and has been fixed in the development branch along with all the other changes then
git cherry-pick
to fix the bug with merging the whole branch.
Need Chat API for your website or app
DeadSimpleChat is an Chat API provider
- Add Scalable Chat to your app in minutes
- 10 Million Online Concurrent users
- 99.999% Uptime
- Moderation features
- 1-1 Chat
- Group Chat
- Fully Customizable
- Chat API and SDK
- Pre-Built Chat
Conclusion
Thank you for reading! I hope you liked the article