#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
—-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
-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
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
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
More information: git help checkout
6. Switch to the previous branch
git checkout -
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 -
git checkout .
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
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
If you want to rename the currently checked out branch, you can shorten the command to the following form:
git branch -m new-name
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
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
12. What has changed?
git whatchanged —-since=‘2 weeks ago’
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
14. Find the branch
git branch --contains <commit>
The The command will display all branches containing a specific commit.
15. Optimize the repository locally
git gc --prune=now --aggressive
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
英文原文地址: 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!