Skip to main content

7 Most Common Git Mistakes

 
common git error messages,10 common git problems and how to fix them,error searching and handling in git,branch check fail in git hackerrank,git commit -am not working,git challenges,git reset,git commit error,please tell me who you are
 
Many newbies tend to make mistakes, especially at the beginning. This is completely normal, as Git can get very complex, depending on the project's size. I want to accelerate your learning success with Git in this article by showing you the 7 most common Git errors, more specifically Git problems, and how you can easily solve them.

It starts with the 7 most common Git errors and their solutions:

1. Discard changes to local files

common git error messages,10 common git problems and how to fix them,error searching and handling in git,branch check fail in git hackerrank,git commit -am not working,git challenges,git reset,git commit error,please tell me who you are

As a programmer, it happens every day that unexpected errors occur. To solve the errors quickly, we fumble wildly with the code. Unfortunately, these code changes are not always optimal. It is, therefore, helpful to quickly undo the changes you have made.

With the command “git checkout” you can reset the files to their original state:

# Reset directory "myCode"
git checkout - myCode


2. Undo local commits

common git error messages,10 common git problems and how to fix them,error searching and handling in git,branch check fail in git hackerrank,git commit -am not working,git challenges,git reset,git commit error,please tell me who you are

You have already created four new commits and only now realize that one of these commits contains a major error. Oops!

No panic. If you want to undo one or more commits, you can use the “git reset” command. The command knows three different modes (soft, hard, mixed):

# Undo the last four commits, keep changes
git reset HEAD ~ 4

# Undo the last four commits, discard changes
git reset --hard HEAD ~ 4

3. Remove the file from Git without deleting it completely

common git error messages,10 common git problems and how to fix them,error searching and handling in git,branch check fail in git hackerrank,git commit -am not working,git challenges,git reset,git commit error,please tell me who you are
 
You often add a file to the staging area ( git add ) that doesn’t belong there. You can use the command “ git rm “ here. However, this also removes the file from your file system.

However, if you want to keep the file in the filesystem, you can better remove it from the staging area with “git reset <filename>”. Then add the file to the .gitignore so that you do not mistakenly pack it back into the staging index in the future. That’s how it’s done:

git reset Dateiname
echo Dateiname >> .gitignore

4. Subsequently edit the commit message

common git error messages,10 common git problems and how to fix them,error searching and handling in git,branch check fail in git hackerrank,git commit -am not working,git challenges,git reset,git commit error,please tell me who you are
 
Every programmer makes a typo on a commit. Fortunately, commit messages are very easy to correct using the “git commit — amend” command. That’s how it’s done:

# Start the standard text editor to edit the commit message
git commit --amend

# Sets the new message directly
git commit --amend -m "My new commit message


5. Change local commits before a push

common git error messages,10 common git problems and how to fix them,error searching and handling in git,branch check fail in git hackerrank,git commit -am not working,git challenges,git reset,git commit error,please tell me who you are

In the last point, you got to know the option “ — amend”. This is useful if you want to change the last commit. But what if the commit to be corrected wasn’t the last? You can use “git rebase-interactive” for this.

You must enter your remote (usually “origin”) and the branch name here.

git rebase --interactive origin branchName

Running the above command will run your default text editor with the following text:

pick 9g2020f New feature ready
pick 23js34cc My commit
# Rebase 9g2020f..23js34cc onto 9g2020f
#
# Commands:
# p, pick = use commit
# r, reword = Used commit, but changed commit message
# e, edit = Use commit, but stop "amend"
# s, squash = use commit, but merge with last commit
# f, fixup = like "squash", but removes this commit message from the history
# x, exec = execute command (via the command line)
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out


Above you will see a list of local commits, followed by an explanation of the commands available. Just select the commit or commits you to want to update. Change pick to reword (or r for short ), and it will take you to a new view where you can edit the message.

As you can see from the list above, however, interactive rebase offer much more than simply editing commit messages: you can delete commits by deleting them from the list, as well as “can” edit, rearrange and squash.

6. Undo pushed commits

common git error messages,10 common git problems and how to fix them,error searching and handling in git,branch check fail in git hackerrank,git commit -am not working,git challenges,git reset,git commit error,please tell me who you are
 
Yes, unfortunately, bad commits occasionally make it into the remote repository.

Still, don’t be desperate, as Git offers a simple solution to rolling back single or multiple commits:

# Commit anhand von Commit-ID rueckgaengig machen
git revert 453fsdu2#Undo the penultimate commit
git revert HEAD ^#Undo multiple commits
git revert feature ~ 4..feature ~ 2


If you don’t want to create any additional revert commits but only want to make the necessary changes to the working tree, you can use the — no-commit option ( — n for short):

# Undo the last commit, but don't create a revert commit
git revert -n HEAD

7. Find the commit that causes an error after a merge

common git error messages,10 common git problems and how to fix them,error searching and handling in git,branch check fail in git hackerrank,git commit -am not working,git challenges,git reset,git commit error,please tell me who you are
 
Tracking down the exact commit that causes a failure after a large merge can be quite time-consuming.

Fortunately, Git has a good binary search facility with the “git bisect” command. First, you have to start the set-up:

# Start bisecting session
git bisect start#Mark the current revision as "bad"
git bisect bad#Mark the last known good revision as "good"
git bisect good revision

After that, Git will automatically check out the revision between “Good” and “Bad.” Then rerun your code. Mark the commits with “bad” or “good” until you find the appropriate commit causing the error.

Bonus tip: Everything else has failed — this is how you solve the problem
So far, we’ve seen many commands and tips to help solve common mistakes in everyday Git use.

Most problems have a fairly simple solution. But some incidents require the big “hammer.” The only thing that helps here is to rewrite the history of the respective branch. In practice, this often happens with sensitive data that needs to be removed. This can sometimes be customer log-in data that was accidentally transferred to a publicly visible repository. Action must be taken here quickly and safely. That’s how it’s done:

git filter-branch — force — index-filter \
 ‘git rm — cached — ignore-unmatch logindaten.txt’ \
 — prune-empty — tag-name-filter cat — — all

The above command removes the logindaten.txt file from every branch and every day. Also, all commits are removed that would be empty by the above operation.

Please be particularly careful with the filter-branch command, as the entire history of the repository will be rewritten. This can lead to high transaction costs and disrupt the entire development team at work in an ongoing project.
 
 

Comments

  1. whenever we take a Pull from existing project than some confusing bcz each project more than 2 developers so each time discuss each other and than take pull this is the only rules. so some time accidantly by mistake some one take a pull than create some unusual problems

    ReplyDelete

Post a comment

Popular posts from this blog

13 VSCode Extensions That Every Web Developer Should Use

These are the most common extensions at VSCode that may be useful for you Almost every Web-Developer knows what VSCode is and why is it so important to use its extensions, they help us to debug our code, make it more readable and prettier, and just add more useful features to our workspace Of course, there’s not every useful extension, however, I have added 13 extensions without which I just can’t live. 1. Auto Close Tag It’s one of the most important extensions, As you may have noticed from the title, it automatically adds a close tag that you wanted to write. You don’t need any command to activate this extension 2. Auto Rename tag When you want to change your <button> tag to <a> you just will need to change the first(opening) tag and the second will change automatically 3. Beautify The function of it is straightforward— when you have an ugly file without any “spaces” and “tabs”, where every tag goes right after earlier, beautify will really help you To use this extension

8 VS Code Plugins To Improve Your Productivity

  VS Code is a great editor that ships with many features out of the box. That is one of the reasons why it’s the most used IDE. However, there are some areas where it can be improved — and that’s where extensions come into play. I’m not a big fan of having many plugins installed. There is always the risk of making your editor slower or introducing too much magic into your daily routine. You still need to be in the driver’s seat. That’s why I try to stay as close to the raw features as possible. I have no more than ten plugins installed at any given time. I try to be very tidy there. If you installed something and you feel it is not helping, just delete it. It might be distracting you or impacting performance. However, there are some very specific extensions that will have a very important impact on your productivity. I want to focus on those in this article. You are most likely not going to need them all, as your workflow might be different than mine, but you might discover something

5 Common CSS mistakes developers make

If you are starting your carrier as a web developer, CSS is a headache for many developers, based on my experience. It takes time to learn it and master it. This article is covering some common mistakes that you may face when writing CSS code. Ready, let’s go! 1. Using px when it’s not needed⁣   Do you know that you don’t need to use px when you want to set the a value to 0: 2. Repeating the same code⁣   Always keep in mind the DRY rule. Don’t repeat yourself. The number of code lines will cost you in terms of app performance and loading time. 3. Using color names⁣   Using a color code is better: 4. Not using shorthand properties⁣   Avoid multiline of code when you can do the same thing with one line: 5. No font fallback⁣ Always complete the font you’re using:   Read More :: 5 Common Challenges Faced by Web Developers   Read More :: Top 10 Web Development Trends for 2021