DEV Community

Sanket Patel
Sanket Patel

Posted on • Edited on

Git FAQs I Encounter Regularly

Git is much more than just push/pull of your code. In this post I will discuss other supporting commands and tricks to handle some scenarios I have encountered while coding in a team. These are also the questions that I'm asked by most git beginners I work with.

I will assume that you already know the push/pull and branching concepts of git. Please let me know in comments/DM if you are not and I will create a post on the same.

1. Branch switching is forcing me to commit my changes to the current branch before the checkout. But I don't want to commit my changes yet. What should I do?

If so, you can stash (store them safely aside) your current changes and navigate to the other branch easily.

$ git stash

It will NOT stash the newly added files. Generally the newly added files won't cause any issue while switching to another branch. But still if you don't want to see the newly added files in changes after a branch switch, you can stash them too by adding -U parameter to the command above.

$ git stash -U

Thenafter, whenever you want your changes back, run:

$ git stash pop

2. How can I remove/discard all of my uncommited changes?

$ git reset --hard

To remove the untracked files and directories [added on 26th Oct 2018]

$ git clean -d -f

3. I accidentally committed in master or in a branch I should not have. What to do?

First just take a backup of what you committed in a new branch so that you won't lose your changes, using:

$ git checkout -b new-branch-name

It will checkout everything you have in master branch to new-branch-name. Next, we need to remove unnecessary commits (hence the changes) from the master.

You will first need to see the log and get the git commit hash to where you want your master branch to go back. Think of it like undo.

$ git log
commit be6bf5e001852e57bbc2c4e14054e3e539b6601c (HEAD -> master)
Author: Sanket Patel <3sanket3@gmail.com>
Date:   Wed Sep 12 18:19:24 2018 +0530

    another commit by mistake

commit f7169231dc4b729c97f69a877487cf2ddae54133
Author: Sanket Patel <3sanket3@gmail.com>
Date:   Wed Sep 12 18:18:56 2018 +0530

    commit by mistake

commit 9f6deced44efecb59f6d70283b8d0ba2d0a57550 <-- We want to restore at this point
Author: Sanket Patel <3sanket3@gmail.com>
Date:   Wed Sep 12 18:18:26 2018 +0530

    good commit

Using pretty=oneline i.e (git log --pretty=oneline) you can get rid of additional details and display each commit in single line.

Let's say we are good upto the commit titled good commit i.e. 9f6deced44efecb59f6d70283b8d0ba2d0a57550 and we want to remove the other 2 commits added afterwards. We can do this by:

$ git reset --hard 9f6deced44efecb59f6d70283b8d0ba2d0a57550

It will remove the commits and changes made by mistake in Local.

4. I pushed some commits on remote by mistake. How can I discard them?

As explained above in #3, we should first backup and discard commits from local. Our first thought can be, can't just git push simply sync the discard operation we did in local. No, instead the local is now behind than the remote. If you will run git push, it will throw an error and will ask to first git pull. So we will have to forcefully instruct remote to accept our push and discard all additional changes the remote have.

⚠️ CAUTION: Before forcefully pushing the code, one should make sure that there is no commit in the remote branch that we want to keep(not want to discard). It is recommended that you do this operation only after communicating with other team members if they are also working in the same branch.

$ git push origin +my-branch

The prefix + to the branch name will forcefully push your changes. That also means, it will discard the commits you discarded in local.

5. Before merging any branch into my branch, I want to review the changes.

$ git merge others-branch-name --no-commit --no-ff

This tries to merge the changes from others-branch-name to your branch, but won't commit. So that you will get a chance to review what your branch would look like if the merge happened. You can review and the commit the merge manually.

6. I want to rename the directory

$ git mv old-name new-name

And if you want to change the case of the existing directory name, you will have to swap them using the temporary directory, as below:

$ git mv DirectoryName temp
$ git mv temp directory-name

Happy gitting! :)

Proofread by: @ron4ex

Top comments (6)

Collapse
 
aergonaut profile image
Chris Fung

For #2, another option is git checkout -- <paths>. This will replace the contents of all <paths> with whatever is in the Git index at those paths. If you have not staged anything for commit, then this will replace the files with the contents from the most recent commit in your current branch.

For #3, as always when erasing commits, be careful that the commit you are about to erase has never left your local machine! If it has, then it's possible someone else has based work off the commit and erasing it would cause a conflict on their machine.

If you have only one commit that you want to erase, you can easily reset to the previous commit using the shorthand notation HEAD^. Here HEAD references the current commit, and ^ means "the first parent of this commit". This is more convenient if you only want to step back by one commit as you don't need to look up the exact SHAs.

You can also use symbolic notation if you want to step more than one commit back. Instead of ^ use ~n where n is the number of steps to take. HEAD~1 means "one step back from HEAD", while HEAD~3 means "three steps back from HEAD". This is also useful if you know you want to erase a certain number of commits, as you can just tell Git to walk backwards a certain number of steps instead of needing to look up the SHAs.

Collapse
 
edisonywh profile image
Edison Yap

Great tips here, to add on to top #3, instead of having to checkout into another new branch to retain your change, you can actually just do

git reset --soft head~1 (~1 being one commit, so obviously this varies)

Basically, --hard reverts your commits and discards your change, but whereas --soft reverts your commits but retains your change, so you can just checkout to another branch and commit there.

Collapse
 
3sanket3 profile image
Sanket Patel

That seems nicer approach. Will give a try. Thanks

Collapse
 
ozzyaaron profile image
Aaron Todd

I think there is a small mistake in that

git checkout new-branch-name

should probably be

git checkout -b new-branch-name

The first command will only work if new-branch-name already exists which would probably defeat the purpose :)

Collapse
 
3sanket3 profile image
Sanket Patel

Yes, I'm Sorry. You are correct @ozzyaaron . Thanks for pointing out. Updating the post.

Collapse
 
ozzyaaron profile image
Aaron Todd

No worries! I know I don't make mistakes, but many other people do :P