Git commands
Frequently used git commands
Create a git repository
Set the name and email of current git repo user
Globally1
2git config --global user.name <USER_NAME>
git config --global user.email <EMAIL>Locally
1
2git config --local user.name <USER_NAME>
git config --local user.email <EMAIL>To see the existing configurations, use command
1
git config --list
To get a specific configuration, use command ‘git config
‘, e.g. 1
git config user.name
To get help, use command like this, ‘git help
‘, e.g. 1
git help config
Check logs.
1
2
3
4Check last 4 git logs by printing each log in one line
git log --oneline --graph -4
Check last one log, normally
git log -1If you add a new file which hasn’t been tracked before in the repository,
first you have to put this file into the so-called “staged area”, using command below,1
git add <FILE_NAME>
If you have already modified a file which has been tracked before in the repository,
you also have to put file into the so-called “staged area”, using the command below (indeed same as the command above),1
git add <FILE_NAME>
After you have put the file (newly added or already be tracked in repo), you put file(s) into (local) repository using the command below,
1
git commit
If you modify a file, you can tell the difference by the command below,
1
git diff
If you have already put the file(s) into the staged area, you will see nothing by ‘git diff’.
This time, you should use the command below to tell the differences,1
2git diff --cached
git diff --staged --> git version >= 1.61Use a tool to check the differences of files,
1
2
3
4full
git difftool --tool=tkdiff
short
git difftool -t <tool_name>To see all the differences in current branch and master branch, use command below to output those differences,
1
git format-patch -M master -o <OUTPUT_DIR>
To patch the current branch with
*.patch
files, use command,1
git am ~/<SOME_DIR>/*.patch
To create a new branch from current master branch, use command,
1
git checkout -b <BRANCH_NAME> master
To remove/delete a branch (git branch), use command
1
git branch -d <BRANCH_NAME>
If there’s anything not fully merged, git will stop the deletion. Use force command to do it (if confirmed to delete),
1
git branch -D <BRANCH_NAME>
One way to remove untracked files is,
(1) To see all the untracked files,1
git clean -n
Note here the option “-n” is very important, it just shows files untracked, no deleting
(2) Clean if all the files listed above are supposed to be removed,1
git clean -f
To remove unstaged files, use commands below,
(1) For a specific file use:1
git checkout <PATH_TO_FILE_TO_REVERT>
(2) For all unstaged files use:
1
git checkout -- .
Rename a branch
1
git branch -m <OLD_BRANCH_NAME> <NEW_BRANCH_NAME>
Rebase a branch
(1) Switch to a branch which need rebase1
git checkout <DEV_BRANCH>
(2) Rebase
1
git rebase master
Show origin information
1
2
3git remote
git remote show originMove a file
1
git mv <OLD_FILE> <NEW_FILE>
By moving files with git, we notify git about two things
(1) The hello.html file was deleted.
(2) The lib/hello.html file was created.
Both facts are staged immediately and ready for a commit. Git status command reports the file has been moved.Git aliases
Equals to ‘git checkout’1
git config --global alias.co checkout
Equals to ‘git branch’
1
git config --global alias.br branch
Equals to ‘git commit’
1
git config --global alias.ci commit
Equals to ‘git status’
1
git config --global alias.st status
Create a new branch in local and then push it to the remote (GitHub) and then track it
1
2
3
4
5
6
7
8
9
10
11
12
13
141. Create a new branch in local
git branch <NEW_BRANCH_NAME>
2. Get the remote repo name
git remote
3. Push the new branch to remote repo
git push <REMOTE_REPO_NAME> <NEW_BRANCH_NAME>
Usually remote repo name is 'origin'
git push origin <NEW_BRANCH_NAME>
4. Connect this new local branch with the new branch just pushed to the remote repo
git branch --set-upstream-to=origin/<NEW_BRANCH_NAME>
5. After that, modify code and push as usaul
git add -A
git commit -m "XXXXX"
git push (or use git push origin <NEW_BRANCH_NAME>)Remove files and restore files
Reference website git 删除文件与恢复
A file was deleted locally by
shell
commands other thangit
commands, but it was not added to the stage area, usecheckout
option to restore1
2
3
4Remove a file locally by shell commands
rm <FILE>
Restore the file
git checkout -- <FILE>A file was deleted locally by
shell
commands other thangit
commands, and it was added to the stage area, first usereset
command to rollback the file to the status of locally removed, and then usecheckout
option to restore1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#### 1st situation
Remove a file locally by shell commands
rm <FILE>
Add to staged area (deleted)
git add <FILE>
Rollback
git reset HEAD <FILE>
Restore the file
git checkout -- <FILE>
#### 2nd situation
Remove a file locally by git command which will add it to staged area (deleted)
git rm <FILE>
Rollback
git reset HEAD <FILE>
Restore the file
git checkout -- <FILE>A file was deleted locally either by
shell
commands orgit
commands, and it was not only added to the stage area, but also committed to local repository, then we need to usereset --hard <ID>
to rollback (ID is got by usinggit log
command)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#### 1st situation
Remove a file locally by shell commands
rm <FILE>
Add to staged area (deleted)
git add <FILE>
Commit
git commit -m "Delete a file"
Get the version ID by using the log option
git log --pretty=oneline
Rollback
git reset --hard <ID>
#### 2nd situation
Remove a file locally by git command which will add it to staged area (deleted)
git rm <FILE>
Commit
git commit -m "Delete a file"
Get the version ID by using the log option
git log --pretty=oneline
Rollback
git reset --hard <ID>A file was deleted locally either by
shell
commands orgit
commands, and it was not only committed to the local repository, but also push to the remote repository (e.g., Github), then following the steps above, and the usegit push -f
to push the restored files back to the remote repository. Note here-f
is a must, because git doesn’t allow lower versions override higher versions.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25##### 1st situation
# Remove a file locally by shell commands
rm <FILE>
# Add to staged area (deleted)
git add <FILE>
# Commit
git commit -m "Delete a file"
# Get the version ID by using the log option
git log --pretty=oneline
# Rollback
git reset --hard <ID>
# Push to remote
git push -f
##### 2nd situation
# Remove a file locally by git command which will add it to staged area (deleted)
git rm <FILE>
# Commit
git commit -m "Delete a file"
# Get the version ID by using the log option
git log --pretty=oneline
# Rollback
git reset --hard <ID>
# Push to remote
git push -f
Git commands
install_url
to use ShareThis. Please set it in _config.yml
.