Basic GIT commands

1. Create a New Repository

# Create a new Git repository in the current directory.
$ git init

# Create a new directory and initialize it to the Git repository.
$ git init [project-name]

# Download a project and its entire code history.
$ git clone [url]

2. Configuration

The setting file for Git is .gitconfig, which can be placed under the user's home directory (global configuration) or under the project directory (project configuration).
# Display the current Git configuration.
$ git config --list

# Edit Git Configuration File.
$ git config -e [--global]

# Set the user information when submitting code.
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

3. Add/Delete Files

# Add the specified file to the Index.
$ git add [file1] [file2] ...

# Add the specified directory to the Index, including subdirectories.
$ git add [dir]

# Add all the files in the current directory to the Index.
$ git add .

# Being asked to confirm it before adding every change.
# For multiple changes to the same file, split submission is supported.
$ git add -p

# Delete the files in the Workspace and put this deletion into the Index.
$ git rm [file1] [file2] ...

# Stop tracking the specified file, but the file will be remained in the Workspace.
$ git rm --cached [file]

# Rename the file and put the renamed name into the Index.
$ git mv [file-original] [file-renamed]

4. Code Submission

# Submit the code from the Index to the Repository.
$ git commit -m [message]

# Submit the specified file from the Index to the Repository.
$ git commit [file1] [file2] ... -m [message]

# Submit the changes in the Workspace since the last commit, to the Repository directly.
$ git commit -a

# Display all diff information when submitting.
$ git commit -v

# Use a new commit to take the place of the last commit.
# If the code doesn't have any new changes, it will be used to rewrite the commit information of the last commit.
$ git commit --amend -m [message]

# Redo the last commit, including the new changes to the specified file.
$ git commit --amend [file1] [file2] ...

5. Branch

# List all local branches.
$ git branch

# List all remote branches.
$ git branch -r

# List all local branches and remote branches.
$ git branch -a

# Create a new branch, but still stay in the current branch.
$ git branch [branch-name]

# Create a new branch and switch to the branch.
$ git checkout -b [branch]

# Create a new branch, pointing to the specified commit.
$ git branch [branch] [commit]

# Create a new branch to establish a tracking relationship with the specified remote branch.
$ git branch --track [branch] [remote-branch]

# Switch to the specified branch and update the workspace.
$ git checkout [branch-name]

# Switch to the previous branch.
$ git checkout -

# Establish a tracking relationship between the existing branch and the specified remote branch
$ git branch --set-upstream [branch] [remote-branch]

# merge the specified branch to the current branch.
$ git merge [branch]

# Select a commit to be merged into the current branch.
$ git cherry-pick [commit]

# Delete the branch.
$ git branch -d [branch-name]

# Delete the remote branch.
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

6. Tag

# List all tags.
$ git tag

# Create a new tag in the current commit.
$ git tag [tag]

# Create a new tag in the specified commit.
$ git tag [tag] [commit]

# Delete the local tag.
$ git tag -d [tag]

# Delete the remote tag.
$ git push origin :refs/tags/[tagName]

# View the tag information.
$ git show [tag]

# Commit the specified tag.
$ git push [remote] [tag]

# Commit all tags.
$ git push [remote] --tags

# Create a new branch pointing to a certain tag
$ git checkout -b [branch] [tag]

7. View Information

# Display the changed files.
$ git status

# Display the version history of the current branch.
$ git log

# Display the commit history, and the changed files for each commit.
$ git log --stat

# Search commit history, according the keyword.
$ git log -S [keyword]

# Display all changes since a certain commit, occupying one line for one commit.
$ git log [tag] HEAD --pretty=format:%s

# Display all changes since a certain commit, and its "commit description" must meet the search criteria.
$ git log [tag] HEAD --grep feature

# Display the version history of a certain file.
$ git log --follow [file]
$ git whatchanged [file]

# Display each diff related to the specified file.
$ git log -p [file]

# Display the past 5 commits.
$ git log -5 --pretty --oneline

# Display all the users who have committed, sorted by number of commits.
$ git shortlog -sn

# Display when and by whom the specified file was modified.
$ git blame [file]

# Display the difference between the Index and the Workspace.
$ git diff

# Display the difference between the Index and the previous commit.
$ git diff --cached [file]

# Display the difference between the Workspace and the latest commit of the current branch.
$ git diff HEAD

# Display the difference between two commits.
$ git diff [first-branch]...[second-branch]

# Display how many lines of code you have written today.
$ git diff --shortstat "@{0 day ago}"

# Show the metadata and the changed content for a certain commit.
$ git show [commit]

# Show the changed file for a certain commit.
$ git show --name-only [commit]

# Show the contents of a certain file for a certain commit.
$ git show [commit]:[filename]

# Show the latest commits of the current branch.
$ git reflog

Comments

Popular posts from this blog

What is Springboot and its Advantages

What is Node?

An Introduction to JavaScript