Basic Git Commands
Contents
- Check Current Status
- Branch Operation
- Use git commit –amend
- Check commit history
- .gitignore
- Using git stash
- Use kdiff3 as the default tool for git
Git commands can be searched everywheere. Here I will list only the commonly used ones in my development.
First of all, try the following commands first:
git help: get git basic commands.
git help COMMAND: get help of a command.
Check Current Status
Origin
Usegit remote -v
or git config --get remote.origin.url
to get current origin info.
Branch Info
Use git branch -vv
to get all your local branchs' tracking branch. You can also use git show remote origin
to get the tracking info, but with a lot of trivial info.
Branch Operation
To checkout a new branch from remote repo: When you want to change to a branch that is not in your local side,
- use
git branch -a
to list all the branches, and find the one you want to checkout in the remote. - If the branch you are looking for is not there, or you want to make sure that the branch is up-to-date, run
git pull
first before you do checkout. - Then run
git checkout origin/<branch_name>
, and you will get the branch, and change to the branch.
Use git commit --amend
The git commit --amend
command is a convenient way to fix up the most recent commit. It lets you combine staged changes with the previous commit instead of committing it as an entirely new snapshot. It can also be used to simply edit the previous commit message without changing its snapshot.
But, amending doesn’t just alter the most recent commit—it replaces it entirely. To Git, it will look like a brand new commit. It’s important to keep this in mind when working with public repositories.
git commit --amend
will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with: git commit --amend -m "New commit message"
.
Check commit history
search
First, use git log
to find the commit we want. We can use git log --author=<someone>
to narrow the scope.
Then, use git diff COMMIT^ COMMIT to get all the changes of one commit.
More useful commands on git log
git commands | in GUI SourceTree | comments |
---|---|---|
git log --author=<someone> |
on the top left, select Search view | list all commits someone made |
git log --name-only |
on the top left, select Log view | list all commits and each modified or new created files |
git log -- <file> |
right click a file and select log selected… | list all commits on a file that exist |
git log --all -- <path-to-file> |
– | list all commits on a deleted file. If you want to restor the file, use git checkout <commit>^ -- <path-to-file> |
git log -g --grep=<key words> |
on the top left, select Search view | find the commits which contains the key words in the commit messages. Note:use double quotes for key words with blank spaces. |
.gitignore
Sometimes we want to ignore some files/folders from being tracked by git. We can use .gitignore file to do this. But this only works for the untracked items. If you have done git add file to the file once, .gitignore will not exclude this file from being tracked. For this case, we can use the following two ways to ignore the file:
git rm \-\-cached file
This command will remove the file from the stage, and if you exclude the file in the .gitignore, it will not appear in the untrancked files list. But you will get a deleted: file in the changes to be commited list, and the next time you commit, the file in the repo will be deleted.
If this is not what you want, and you only want to ignore the file locally, and don’t want to remove it in the repo, because someone else may need this file, go to the second method.
git update-index \-\-assume-unchanged file
This method doesn’t need .gitignore, and will not delete the file in the repo. The disadvantage is: when you clone the repo in other places, you need to run the command again.
Using git stash
Reference:
Description
When you are woring on a newbranch
which is based on an oldbranch
, and have some uncommitted changes in newbranch
. But now your manager notice you that there is an urgent issue needs to be fixed in the oldbranch
right now, and you don’t want to commit your changes in newbranch
, what should we do here?
Solution
When we trying to checkout oldbranch
using git checkout oldbranch
, what is git doing? There may be three ways:
- Ignore the uncommitted working directory changes in
newbranch
, and set the working directory to the files in theoldbranch
. The uncommited changes in thenewbranch
will be lost. - Ignore the state of the files in
oldbranch
, and use the working directory files innewbranch
. Completely wrong! This will not confirm with the originaloldbranch
. - Attempt to merge in the changes from the working directory in
newbranch
into the files inoldbranch
.
Git actually tries to use #3 method.
With no confilicts
checkout oldbranch
from newbranch
with uncommitted changes in newbranch
, but NO Conflicts between the two branches
The sample log:
1 2 |
|
And the M
means the test.txt
is successfully merged.
With Conflicts
checkout oldbranch
from newbranch
with uncommitted changes in newbranch
, but With Conflict between the two branches. In this situation, if you want to checkout oldbranch
any way, use git checkout -m oldbranch
which will let git list all the conflicts, as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
git stash
But most of the time, we want to keep the uncommited changes staying in newbranch
, and oldbranch
as it was. And when we finish the work in oldbranch
and get back to newbranch
, the uncommited changes is still there. For this purpose, we need git stash
:
- in
newbranch
, rungit stash
. Checkgit status
, and there will be nothing changed. git checkout oldbranch
.- Work in
oldbranch
, and commit the changes. git checkout newbranch
git stash pop
Git will hold a stash stack for all the stashes in all branches. That is to say, no matter how many branches you have, you will have only one stash stack. So when you run git stash pop
, you should check the stash stack using git stash list
, you will get:
1 2 3 4 |
|
If you want to roll back the last stash in newbranch, in #5 step, run git stash pop stash@{1}
. If you don’t use the index stash@{1}
, the top stash in the stash stack will be popped out.
Use kdiff3 as the default tool for git
When you run git diff
, the difference will be listed in the command window. If you want to see the difference in a visual window (for exmple, kdiff3):
- install kdiff3 in your mac
As the new version of git has build-in support for kdiff3, so there is no need for manually configuration, just run
$ git config --global merge.tool *kdiff3*
If kdiff3 is not in your path, run
$ git config --global mergetool.kdiff3.path /Applications/kdiff3.app/Contents/MacOS/kdiff3
Use
git difftool
instead ofgit diff
to let git launch kdiff3 for the difference. Also usegit mergetool
instead ofgit merge
for merging files.Notice:
git mergetool
should be used aftergit merge branch_name
and there are some merge conflicts. Useget help mergetool
for detail.
To check the configuration result, open ~/.gitignore.