From: www.codeceo.com/7-git-skill…

Git should save more developers’ jobs than any other technology. As long as you’re using Git to save your work, there’s always a chance you can revert your code back to where it was before, and therefore undo those late night errors you made in a daze.

Having said that, Git’s command-line interface is notoriously difficult to master. Here are seven tips to get the most out of Git.

Add, commit, branch, and push/pull are the only commands we use most of the time. Most people are familiar with this workflow that works in only one direction. Have you ever wondered how you can undo something if you add the wrong file to the repository or commit code to the wrong branch? If you are following the same steps described in the cartoon above (deleting the local project folder and re-downloading the repository), then it is important to know the following Git tips.



1. Modify the error commit message

Commit information will remain in your code base for a long time, so you will want to know exactly what changes have been made. The following command allows you to edit the most recent commit, but you must make sure that you have not made any changes to the current working copy, or the changes will be committed with it.

$git commit -- amend-m "your-new-commit-message"Copy the code

If you have pushed a git commit to a remote branch, you need to force the commit with the following command.

$ git push   --forceCopy the code

You can follow this q&A on Stack Overflow for more details.

2. Undo git add before committing

If you add some wrong files to the staging area, you haven’t committed the code yet. You can undo it with a simple command. If only one file needs to be removed, type:

$git resetCopy the code

Or if you want to remove all uncommitted changes from the staging area:

$ git resetCopy the code

You can follow this q&A on Stack Overflow for more details.

3. Undo the last code commit

Sometimes you may accidentally submit the wrong document or leave something out in the first place. The following three steps can help you solve this problem.

$ git reset --soft HEAD~1
Make the necessary changes to the working file
$ git add -A .
$ git commit -c ORIG_HEADCopy the code

When you execute the first command, Git moves the HEAD pointer back to the previous commit so you can move the file or make any necessary changes.

Then you can add all your changes, and when you execute your last command, Git will open your default text editor, which will contain information from the last commit. You can modify the commit information if you wish, or you can skip this step by using -c instead of -c in the final command.

4. The Git repository is withdrawn to the previous commit state

“Undo” is absolutely necessary in many situations — especially if you’ve made a mess of code. Most often, you want to go back to the previous version of the code, check the code base at that time, and then come back to the present state. This can be done with the following command:

$ git checkout Copy the code

“” is the first 8 to 10 characters in the Hash Code that you want to see the submission has. This command detach the pointer, allowing you to view the code without checking out any branches — leaving the HEAD is not as scary as it sounds. If you want to commit changes in this case, you can do so by creating a new branch:

$ git checkout -b Copy the code

To get back to where you are, simply check out the branch you were in before.

You can follow this q&A on Stack Overflow for more details.

5. Merge

To undo the merge, you may have to use the HARD RESET command to return to the state of the last commit. What merge does is basically reset the index, update different files in the Working Tree, that is, between the current submission () code and the code the HEAD cursor points to; However, the merge preserves differences between the index and the Working Tree (for example, changes that are not tracked).

$ git checkout -b Copy the code

Of course, there are always other ways to implement Git, and you can check out this article to learn more.

6. Remove untracked local files from the current Git branch

Let’s say you happen to have some files that aren’t tracked (because you no longer need them) and don’t want them to show up every time you use git status. Here are some ways to solve this problem:

$ git clean -f -n         # 1
$ git clean -f            # 2
$ git clean -fd           # 3
$ git clean -fX           # 4
$ git clean -fx           # 5Copy the code
  • (1): The -n option shows which files will be removed when (2) is executed.
  • (2): This command will remove all files displayed in command (1).
  • (3): If you also want to remove files, use the -d option.
  • (4): If you only want to remove files that have been ignored, use the -x option.
  • (5): If you want to remove ignored and unignored files, use the -x option.

Notice the difference in X in the last two commands.

For more details, see the documentation for git-Clean.

7. Delete the local and remote Git branches

Delete a local branch:

$ git branch --delete --force Copy the code

Or use the -d option as a shorthand:

$ git branch -DCopy the code

Delete remote branches:

$ git push origin --delete Copy the code

Suggestion: To better understand how to use Git, please read the official Git documentation carefully.