Home > Development Tools > git > body text

15 Git commands you may not know yet

青灯夜游
Release: 2020-06-18 17:58:17
forward
3135 people have browsed it

15 Git commands you may not know yet

#Git can be intimidating at times. Because there are so many commands and details to learn. However, although Document has a lot of content, it is still very easy to read. Once you get over the initial feeling of overwhelm, you'll feel significant progress. Here is a list of 15 Git commands you may not know yet, hopefully they will help you become proficient in Git.

1. Modify the latest commit

git commit --amend
Copy after login

—-amend allows you to make staged changes (such as adding Forgotten files) are appended to the previous commit. Adding --no-edit will modify the last commit but not change its commit message. If there are no changes, --amend will allow you to re-enter the last commit message.

More information: git help commit.

2. Interactively add selected portions of files

git add -p
Copy after login

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

More information:git help add

3. Interactively hide selected portions of files

git stash -p
Copy after login

Similar to git-add, you can interactively select parts of each file to be tracked using the --patch option.

More information:git help stash

4. Hide untracked files

git stash -u
Copy after login

By default, untracked files are not included when storing. In order to change this behavior and include those files, you need to use the -u parameter. There is also a -a (-all) parameter that can store all untracked and ignored files, which is usually something you don't need.

5. Interactively restore selected portions of files

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

More information: git help checkout

6. Switch to the previous branch

git checkout -
Copy after login

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

7. Restore all local Change

git checkout .
Copy after login

If you are sure you can discard all local changes, you can use . to do it all at once. But it's a good practice to always use checkout --patch.

8. Show changes

git diff --staged
Copy after login

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

More information: git help diff

9. Rename the branch locally

git branch -m old-name new-name
Copy after login

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 after login

More information: git help branch

10. Rename a branch remotely

In order to rename a branch remotely, after renaming the branch locally, you need to delete the branch remotely first, and then push the renamed branch again.

git push origin :old-name
git push origin new-name
Copy after login

11. Open all conflicting files at once

Resetting the baseline may cause conflicts, the following command will open and require you to resolve these All conflicting files.

git diff --name-only --diff-filter=U | uniq  | xargs $EDITOR
Copy after login

12. What has changed?

git whatchanged —-since=‘2 weeks ago’
Copy after login

This command will display a log containing the differences introduced by each commit over the last two weeks.

13. Remove files from previous commit

You can do this by combining rm and commit - -amend command to quickly delete accidentally committed files from the last commit:

git rm —-cached <file-to-remove>
git commit —-amend
Copy after login

14. Find the branch

git branch --contains <commit>
Copy after login

The The command will display all branches containing a specific commit.

15. Optimize the repository locally

git gc --prune=now --aggressive
Copy after login

More information: git help gc

Summary

Although I like the CLI very much, I still strongly recommend using Magit to further improve the efficiency of your use of Git. It's one of the best software I've ever used.

You can also view an excellent overview of the Git workflow through the help command. Please read carefully!

git help workflows
Copy after login

英文原文地址: https://zaiste.net/15-git-commands-you-may-not-know/

 为了保证的可读性,本文采用意译而非直译。

教程推荐:《Git教程

The above is the detailed content of 15 Git commands you may not know yet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
git
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!