In the process of working, I found that most of beginners often so confused with Git. They often commit wrong files, delete wrong branch, want to re-commit… Not only them, sometime me too.
Therefor, in this article, I’ll present my problem-solving experience in Git. So, if you are a beginner, hope my experience is useful for you. If not, hope you’ll find something new.
1. How to restore a branch you were deleted
This often occur when you have two or “n” branch have the same name. And then, you delete wrong branh you want.
Just calm down and type following command:
$ git reflog
Now, look into the logs on the screen, look at the commit you want restore. Take notice to SHA1 Hash on the left side of each’s.
In my case, I want to restore the commit “restore example” and its sha code is “01c8713”. So, I’ll do like this:
$ git checkout -b deleted_branch_name 01c8713
Try checkout to abc branch and enjoy the fruits.
2. Set wrong branch name
Imagine that your task is about calendar and relate to spring. But because you have not good english ability, you have named your branch
To resolve this, just rename this branch in a similar way to how you rename a file with the
$ git branch -m feature-springs feature-spring
In case you pushed wrong named branch to remote, you need delete it and push up on new one.
$ git push origin --delete feature-springs $ git push origin feature-spring
3. Change latest Commit Message
This is very simple, and it also the most common. You have done a feature and commited. But when you look back to Commit Message, you realize that, you made misspellings. Of cource you don’t want anyone read it. To change, you just use following command:
$ git commit --amend -m "new commit message"
4. Fogot to add a file to that last commit
Assume you just make a commit. When you review your code, you realize that, you can make it better with another way intead of present way. So, you want change the code. But you if you make changes, you’ll need to re-commit and everybody still can see your old code.
There’s a solution for this too. We will “push” alteration to latest commit intead of make a new commit. To do this, just add changed files normally and use git commit –amend:
$ git add changed_file_1.php changed_file_2.php $ git commit --amend
In this moment, all changed files were “pushed” to latest commit and no one can see your old code.
5. Commited a wrong file
What if you added a file that you didn’t want to commit? a laradock directory, a .env file, a file you config file you were changed for development easier,…
$ git reset --soft HEAD~1 $ git reset config_app.php $ git commit
This will undo the commit, take the wrong file out of added satus, then added a new commit in its place.
6. Accidentally committed all changes to the master branch
So you are working in your haste and forgot create a new branch for it. You have already committed a load of files and now those commits are all sitting on the master branch.
So we can roll back all those changes to a new branch with the following three commands:
Note: if you’re changing your code, make sure you commit or stash your changes first, or all will be lost!
$ git branch feature-branch $ git reset HEAD~ --hard $ git checkout feature-branch
As you can see, we’ll creates a new branch, then rolls back the master branch to where it was before you made changes, before finally checking out your new branch with all your previous changes intact.
7. Pushed a wrong branch to The Remote Repo
Let’s suppose you create a new branch to test code and push that branch to remote repo for your team to do something. When your purpose is done, maybe you don’t want to let your customer see that branch.
Very simple way to resolve your wishes, delete the branch on repo with this command:
$ git push origin --delete test-code-branch
8. Undo pull a branch
Sometime, maybe you’ll pull wrong branch that you want, and your code become very chaotic. So you want back to latest commit and re-pull with correct branch.
First, keep calm and type below command:
$ git log
We can see hash<SHA-1> of the revision you want. Copy that hash and use git reset to back to the latest commit
$ git reset --hard <sha-1-hash found in CLI>
Tada, now run a
git status and see the result you just get on.
9. Change one or a large of files but can’t “Ctrl + Z”
This case often occur when you change your code to fix bug or add a new feature. But after, you realize that your way is not working and you want undo all changed but “Ctrl + Z” is too late for that. Let me show you some magics :))
In case you want to unchanged one or several files, just only use git checkout file_path
$ git checkout README.md demo1.php demo2.php
If you’re want to revert all changed files, you can use
$ git checkout .
After run above command, you can see all changed files were rollbacked. This solution can apply to get files you were deleted but not commited yet.
These is a little bit experience with Git. It is not enough, but they’re common cases. If you feel this post is useful, consider share this with your friends. Thank you!