Author: zaiste

Translation: Crazy geek

Original text: zaiste.net/15-git-comm…

Reproduced without permission

Git can be intimidating at times. There are too many commands and details to learn. But while the document is extensive, it’s a breeze to read. Once you get over the initial feeling of being overwhelmed, you’ll feel tangible progress. Here is a list of 15 Git commands that you may not know about and hopefully will help you master Git.

1. Modify the most recent commit

git commit --amend
Copy the code

– Amend allows you to attach periodic changes (such as adding forgotten files) to the last commit. Adding –no-edit will modify the last commit without changing its commit message. If there are no changes, — Amend will allow you to reenter the last commit message.

Git help commit

2. Interactively add selected portions of the file

git add -p
Copy the code

-p (or -patch) allows an interactive selection of the parts of each trace file to commit. This way each commit contains only relevant changes.

Git help add

3. Interactively hide selected parts of the file

git stash -p
Copy the code

Similar to git-add, you can interactively select each part of the file to track using the –patch option.

More information: Git help Stash

4. Hide untraced files

git stash -u
Copy the code

By default, untraced files are not included in the storage. To change this behavior and include those files, you need to use the -u argument. There is also a -a (-all) parameter to store all untraced and ignored files, which is usually unnecessary.

5. Interactively restore selected parts of the file

git checkout -p
--patch` can be also used to selectively discard parts of each tracked file. I aliased this command as `git discard
Copy the code

Git Help Checkout

6. Switch to the previous branch

git checkout -
Copy the code

This command allows you to quickly switch to the previously checked out branch. Usually – is the alias of the previous branch. It can also be used with other commands. I created an alias for checkout, co, so it could be Git co –

7. Restore all local changes

git checkout .
Copy the code

If you are sure you can discard all changes locally, do it all at once with. But it’s a good habit to always use Checkout –patch.

8. Display the changes

git diff --staged
Copy the code

This command shows all staged changes (changes that have been added to the index), compared to git diff, which shows only changes in the working directory (no changes in the index).

Git help diff

9. Rename the branch locally

git branch -m old-name new-name
Copy the code

If you want to rename the currently checked out branch, you can shorten the command to the following form:

git branch -m new-name
Copy the code

Git Help Branch

10. Remotely rename branches

To remotely rename a branch, after you rename a branch locally, you need to remotely delete the branch and then push the renamed branch again.

git push origin :old-name
git push origin new-name
Copy the code

11. Open all conflicting files at once

Resetting the baseline may cause conflicts, and the following command will open all files that you need to resolve those conflicts.

git diff --name-only --diff-filter=U | uniq  | xargs $EDITOR
Copy the code

12. What has changed?

Git whatchanged -- since= '2 weeks ago'Copy the code

This command displays a log containing the differences introduced for each commit in the last two weeks.

13. Delete files from the last commit

You can quickly remove an error from a previous commit by combining rm and commit –amend:

Git rm -- cached <file-to-remove> git commit -- amendCopy the code

14. Find branches

git branch --contains <commit>
Copy the code

This command displays all branches that contain a particular commit.

15. Optimize the repository locally

git gc --prune=now --aggressive
Copy the code

Git help GC

conclusion

As much as I love the CLI, I strongly recommend using Magit to further improve your Git productivity. It’s one of the best programs I’ve ever used.

You can also use the help command to get an excellent overview of Git workflow. Be sure to read it carefully!

git help workflows
Copy the code

Welcome to pay attention to the front public number: front pioneer, free webpack from the entry to the full series of advanced tutorials.