Categories
CapstoneWeekly

List of commonly used Git commands

This week our Project owner completed their training for Neural Network and uploaded it to GitHub. When I personally called and debugged these new codes, I found that I was too unfamiliar with Github Commands (basically, I had to search on google every five minutes). I use Git every day, but I can’t remember many commands. So I decided to create my own blog for daily used Github Commands Collections.

Generally speaking, we only need to remember the 6 commands in the figure below for daily use. But if we want to use it skillfully, we have to remember 60-100 commands.

  • Create New Project
# Create a new Git code repository in the current directory
$ git init

# Create a new directory and initialize it as a Git code repository
$ git init [project-name]

# Download a project and its entire code history
$ git clone [url]
  • Configuration
# Show current Git configuration
$ git config --list

# Edit Git configuration file
$ git config -e [--global]

# Set user information when submitting code
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"
  • Add/Delete Files
# Add the specified file
$ git add [file1] [file2] ...

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

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

# Before adding each change, it will ask for confirmation
# For multiple changes of the same file, it can be submitted in stages
$ git add -p

# Delete the specified file
$ git rm [file1] [file2] ...

# Stop tracking the specified file, but the file will remain in the local
$ git rm --cached [file]

# Rename the file and put this rename into the temporary storage area
$ git mv [file-original] [file-renamed]
  • Upload code
# Submit the temporary storage area to the repository
$ git commit -m [message]

# Submit the specified files in the temporary storage area to the repository
$ git commit [file1] [file2] ... -m [message]

# Submit the changes in the work area since the last commit, directly to the repository
$ git commit -a

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

# Use a new commit to replace the previous commit
# If there is no new change in the code, it is used to rewrite the last commit information
$ git commit --amend -m [message]

# Redo the last commit and include the new changes in the specified file
$ git commit --amend [file1] [file2] ...
  • 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 this branch
$ git checkout -b [branch]

# Create a new branch and point to the specified commit
$ git branch [branch] [commit]

# Create a new branch and 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 current branch and the specified remote branch
$ git branch --set-upstream [branch] [remote-branch]

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

# Choose a commit and merge it into the current branch
$ git cherry-pick [commit]

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

# Delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
  • View Information
# Show changed files
$ git status

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

# Show the commit history and the files that have changed each time
$ git log --stat

# Search submission history according to keywords
$ git log -S [keyword]
  • Remote Sync
# Download all changes in the remote repository
$ git fetch [remote]

# Show all remote repository
$ git remote -v

# Display information about a remote repository
$ git remote show [remote]

# Add a new remote repository and name it
$ git remote add [shortname] [url]

# Retrieve changes in the remote repository and merge with the local branch
$ git pull [remote] [branch]

# Upload the local specified branch to the remote repository
$ git push [remote] [branch]

# Forcibly push the current branch to the remote repository, even if there is a conflict
$ git push [remote] --force

# Push all branches to the remote repository
$ git push [remote] --all
  • RollBack
# Restore the specified files in the temporary storage area to the work area
$ git checkout [file]

# Restore the specified file of a commit to the temporary storage area and work area
$ git checkout [commit] [file]

# Restore all files in the temporary storage area to the work area
$ git checkout .

# Reset the specified file in the temporary storage area to be consistent with the last commit, but the work area remains unchanged
$ git reset [file]

# Reset the temporary storage area and work area to be consistent with the last commit
$ git reset --hard

# Reset the pointer of the current branch to the specified commit and reset the temporary storage area at the same time, but the work area remains unchanged
$ git reset [commit]

# Reset the HEAD of the current branch to the specified commit and reset the temporary storage area and work area at the same time, consistent with the specified commit
$ git reset --hard [commit]

# Reset the current HEAD to the specified commit but keep the temporary storage area and work area unchanged
$ git reset --keep [commit]

# Create a new commit to revoke the specified commit
# All changes in the latter will be offset by the former and applied to the current branch
$ git revert [commit]

# Temporarily remove uncommitted changes and move them in later
$ git stash
$ git stash pop

Git Commands Tutorial :https://guides.github.com/introduction/git-handbook/

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *