{"id":46,"date":"2021-03-01T07:06:08","date_gmt":"2021-03-01T07:06:08","guid":{"rendered":"http:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/?p=46"},"modified":"2021-03-01T07:06:08","modified_gmt":"2021-03-01T07:06:08","slug":"list-of-commonly-used-git-commands","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/2021\/03\/01\/list-of-commonly-used-git-commands\/","title":{"rendered":"List of commonly used Git commands"},"content":{"rendered":"\n<p>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&#8217;t remember many commands. So I decided to create my own blog for daily used Github Commands Collections.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"831\" height=\"259\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3998\/files\/2021\/03\/figure-1.png\" alt=\"\" class=\"wp-image-48\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3998\/files\/2021\/03\/figure-1.png 831w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3998\/files\/2021\/03\/figure-1-300x94.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/3998\/files\/2021\/03\/figure-1-768x239.png 768w\" sizes=\"auto, (max-width: 831px) 100vw, 831px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Create New Project<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Create a new Git code repository in the current directory\n$ git init\n\n# Create a new directory and initialize it as a Git code repository\n$ git init &#x5B;project-name]\n\n# Download a project and its entire code history\n$ git clone &#x5B;url]\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>Configuration<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Show current Git configuration\n$ git config --list\n\n# Edit Git configuration file\n$ git config -e &#x5B;--global]\n\n# Set user information when submitting code\n$ git config &#x5B;--global] user.name &quot;&#x5B;name]&quot;\n$ git config &#x5B;--global] user.email &quot;&#x5B;email address]&quot;\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>Add\/Delete Files<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Add the specified file\n$ git add &#x5B;file1] &#x5B;file2] ...\n\n# Add the specified directory, including subdirectories\n$ git add &#x5B;dir]\n\n# Add all files in the current directory\n$ git add .\n\n# Before adding each change, it will ask for confirmation\n# For multiple changes of the same file, it can be submitted in stages\n$ git add -p\n\n# Delete the specified file\n$ git rm &#x5B;file1] &#x5B;file2] ...\n\n# Stop tracking the specified file, but the file will remain in the local\n$ git rm --cached &#x5B;file]\n\n# Rename the file and put this rename into the temporary storage area\n$ git mv &#x5B;file-original] &#x5B;file-renamed]\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>Upload code<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Submit the temporary storage area to the repository\n$ git commit -m &#x5B;message]\n\n# Submit the specified files in the temporary storage area to the repository\n$ git commit &#x5B;file1] &#x5B;file2] ... -m &#x5B;message]\n\n# Submit the changes in the work area since the last commit, directly to the repository\n$ git commit -a\n\n# Display all diff information when submitting\n$ git commit -v\n\n# Use a new commit to replace the previous commit\n# If there is no new change in the code, it is used to rewrite the last commit information\n$ git commit --amend -m &#x5B;message]\n\n# Redo the last commit and include the new changes in the specified file\n$ git commit --amend &#x5B;file1] &#x5B;file2] ...\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>Branch<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# List all local branches\n$ git branch\n\n# List all remote branches\n$ git branch -r\n\n# List all local branches and remote branches\n$ git branch -a\n\n# Create a new branch but still stay in the current branch\n$ git branch &#x5B;branch-name]\n\n# Create a new branch and switch to this branch\n$ git checkout -b &#x5B;branch]\n\n# Create a new branch and point to the specified commit\n$ git branch &#x5B;branch] &#x5B;commit]\n\n# Create a new branch and establish a tracking relationship with the specified remote branch\n$ git branch --track &#x5B;branch] &#x5B;remote-branch]\n\n# Switch to the specified branch and update the workspace\n$ git checkout &#x5B;branch-name]\n\n# Switch to the previous branch\n$ git checkout -\n\n# Establish a tracking relationship between the current branch and the specified remote branch\n$ git branch --set-upstream &#x5B;branch] &#x5B;remote-branch]\n\n# Merge the specified branch to the current branch\n$ git merge &#x5B;branch]\n\n# Choose a commit and merge it into the current branch\n$ git cherry-pick &#x5B;commit]\n\n# Delete branch\n$ git branch -d &#x5B;branch-name]\n\n# Delete remote branch\n$ git push origin --delete &#x5B;branch-name]\n$ git branch -dr &#x5B;remote\/branch]\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>View Information<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Show changed files\n$ git status\n\n# Display the version history of the current branch\n$ git log\n\n# Show the commit history and the files that have changed each time\n$ git log --stat\n\n# Search submission history according to keywords\n$ git log -S &#x5B;keyword]\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>Remote Sync<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Download all changes in the remote repository\n$ git fetch &#x5B;remote]\n\n# Show all remote repository\n$ git remote -v\n\n# Display information about a remote repository\n$ git remote show &#x5B;remote]\n\n# Add a new remote repository and name it\n$ git remote add &#x5B;shortname] &#x5B;url]\n\n# Retrieve changes in the remote repository and merge with the local branch\n$ git pull &#x5B;remote] &#x5B;branch]\n\n# Upload the local specified branch to the remote repository\n$ git push &#x5B;remote] &#x5B;branch]\n\n# Forcibly push the current branch to the remote repository, even if there is a conflict\n$ git push &#x5B;remote] --force\n\n# Push all branches to the remote repository\n$ git push &#x5B;remote] --all\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>RollBack<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Restore the specified files in the temporary storage area to the work area\n$ git checkout &#x5B;file]\n\n# Restore the specified file of a commit to the temporary storage area and work area\n$ git checkout &#x5B;commit] &#x5B;file]\n\n# Restore all files in the temporary storage area to the work area\n$ git checkout .\n\n# Reset the specified file in the temporary storage area to be consistent with the last commit, but the work area remains unchanged\n$ git reset &#x5B;file]\n\n# Reset the temporary storage area and work area to be consistent with the last commit\n$ git reset --hard\n\n# 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\n$ git reset &#x5B;commit]\n\n# 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\n$ git reset --hard &#x5B;commit]\n\n# Reset the current HEAD to the specified commit but keep the temporary storage area and work area unchanged\n$ git reset --keep &#x5B;commit]\n\n# Create a new commit to revoke the specified commit\n# All changes in the latter will be offset by the former and applied to the current branch\n$ git revert &#x5B;commit]\n\n# Temporarily remove uncommitted changes and move them in later\n$ git stash\n$ git stash pop\n<\/pre><\/div>\n\n\n<p>Git Commands Tutorial \uff1a<a href=\"https:\/\/guides.github.com\/introduction\/git-handbook\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/guides.github.com\/introduction\/git-handbook\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;t remember many [&hellip;]<\/p>\n","protected":false},"author":10795,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-capstoneweekly"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/wp-json\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/wp-json\/wp\/v2\/users\/10795"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":7,"href":"https:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":55,"href":"https:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/wp-json\/wp\/v2\/posts\/46\/revisions\/55"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/techblogofjerryhe\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}