Categories
Git

Revert a check-in file in Git

To revert a file to a previous version in Git, follow these steps:

  1. View the commit history for the file: Use git log followed by the file name to see the commit history for that specific file:
git log --oneline <filename>

This will display a concise list of commits that have modified the file.

2. Checkout the previous version of the file: Use the git checkout command followed by the commit hash and the file name:

git checkout <commit_hash> -- <filename>

Replace <commit_hash> with the hash of the commit that contains the version of the file you want to revert to.

3. Commit the change: To save the reverted file, commit the change:

git add <filename>
git commit -m "Reverted changes to <filename>"

4. Push the changes: Finally, push the updated branch to the remote repository:

git push origin <branch_name>

Happy Coding 🙂

Categories
Git

Git Rebase Interactive: A Step-by-Step Guide

Git Rebase Interactive is a powerful tool for rewriting commit history. Here’s how to use it:

Step 1: Rebase the Branch

git rebase -i "origin/remote-branch"

Step 2: Edit the Commit History

  • pick: Keep the commit as is
  • squash: Squash the commit into the previous commit
  • edit: Edit the commit message
  • drop: Remove the commit

Step 3: Save and Close

Save and close the interactive shell. Git will rebase the branch according to your changes.

Step 4: Push the Updated Branch

git push --force-with-lease origin local-branch

Example Output

$ git rebase "origin/remote-branch" -Xtheirs
$ git rebase -i "origin/remote-branch"
$ git push --force-with-lease origin local-branch

Conclusion

Git Rebase Interactive makes it easy to squash commits, reorder commits, and push the updated branch to the remote repository. Just remember to use git push --force-with-lease to update the remote branch with the new commit history.

Happy Coding 🙂

Categories
Git Quick Note To Me

Git clone repo with specific branch

Git clone command clones the default or master repository from git remote source. If need to clone a specific branch, we need to specify the branch name in the git clone command. Here is the command to do so

git clone --branch <branch-name> <git-url-to-clone>

Or we can write the same command as follows

git clone -b <branch-name> <git-url-to-clone>

Hope this helps.

Happy Coding 🙂

Categories
Git KeepRemaindingMe

Folder rename in Git

Git can’t detect directory name change. For example rename an existing folder to “Dashboard” from “DashBoard”. Even if we rename the folder name from the directory but git will still not detect the change. Here is the git command to make it work for rename

git mv oldname newname 

For the above example it will not work since it’s case sensitive, to rename a case sensitive name, follow the below commands

git mv DashBoard temp
git mv temp Dashboard

We need to rename to a temporary folder and then rename to intendent folder name.

Happy Coding 🙂

Categories
Git Quick Note To Me

Create git repo from existing project

Often I forget the commands to create git repo from existing project in my local machine.

First step is to init a git repo in your project. Use git bash

$ git init

Second step to add the project files into local git repo

$ git add .

Use the .(dot) after the add, this is saying add all files.

This command is to commit the files with message.

$ git commit -m "Commit message"

Create a repo in the github

$ git remote add origin 'remote repository URL'

After that use following command to push your code to remote repo in the github.

$ git push origin master

After the this command you should be able to see your code in the github.

Happy Coding 🙂