{"id":5911,"date":"2012-09-11T21:23:32","date_gmt":"2012-09-12T02:23:32","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=5911"},"modified":"2023-05-26T14:12:08","modified_gmt":"2023-05-26T19:12:08","slug":"git","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=5911","title":{"rendered":"GIT"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Overview\">Overview<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Features\">Features<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Diff_from_CVCS_Centeralized_VCS\">Diff from CVCS (Centeralized VCS)<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Object_Store\">Object Store<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#File_Status\">File Status<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Checking_File_Status\">Checking File Status<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Three_Config_Files\">Three Config Files<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Install\">Install<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Windows_Install\">Windows Install<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Linux_Install\">Linux Install<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#First_Time_Setup\">First Time Setup<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Ignore_Files\">Ignore Files<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Help\">Help<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Basics\">Basics<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Getting_a_Git_Repository\">Getting a Git Repository<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Recording_Changes_to_the_Repository\">Recording Changes to the Repository<\/a>\n\t\t\t\t<ol class='toc-odd level-3'>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Stage_Files\">Stage Files<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Un-Stage_Files\">Un-Stage Files<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#View_Changes\">View Changes<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Commit_Changes\">Commit Changes<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Remove_Files\">Remove Files<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Moving_Files\">Moving Files<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#View_Commit_History\">View Commit History<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t<\/ol>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Stash_Changes\">Stash Changes<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Undo_Things\">Undo Things<\/a>\n\t\t\t\t<ol class='toc-odd level-3'>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Change_or_Redo_Last_Commit\">Change or Redo Last Commit<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Discard_Commits\">Discard Commits<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Revert_Changes\">Revert Changes<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t<\/ol>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Working_with_Remotes\">Working with Remotes<\/a>\n\t\t\t\t<ol class='toc-odd level-3'>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Show_Remotes\">Show Remotes<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Add_remote_short_name\">Add remote short name<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#FetchPull_from_Remotes\">Fetch\/Pull from Remotes<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Pushing_to_Remotes\">Pushing to Remotes<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Inspecting_a_Remote\">Inspecting a Remote<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Rename_Remotes\">Rename Remotes<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Remove_Remote\">Remove Remote<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t<\/ol>\n<\/ol>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Branching\">Branching<\/a>\n\t\t\t\t<ol class='toc-even level-2'>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Create_New_Branch\">Create New Branch<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Show_Branches\">Show Branches<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Switch_Branch\">Switch Branch<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Delete_Branch\">Delete Branch<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Renaming_a_Branch\">Renaming a Branch<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Push_Branch_to_Remote\">Push Branch to Remote<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t<\/ol>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Merging\">Merging<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Examples\">Examples<\/a>\n\t\t\t\t<ol class='toc-even level-2'>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Generate_Patch_Files\">Generate Patch Files<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t<\/ol>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Issues\">Issues<\/a>\n\t\t\t\t<ol class='toc-even level-2'>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Couldnt_set_refsheadsmaster\">Couldn't set refs\/heads\/master<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#File_is_far_too_short_to_be_a_packfile\">File is far too short to be a packfile<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t<\/ol>\n\t\t\t<li>\n\t\t\t\t<a href=\"#References\">References<\/a>\n\t\t\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Overview\"><h2>Overview<\/h2><\/span>\n<span id=\"Features\"><h3>Features<\/h3><\/span>\n<p>* Created in 2005<br \/>\n* Fully distributed<br \/>\n* Fast and efficient with large projects<br \/>\n* Incredible branching system for non-linear development (thousands of parallel branches)<\/p>\n<span id=\"Diff_from_CVCS_Centeralized_VCS\"><h3>Diff from CVCS (Centeralized VCS)<\/h3><\/span>\n<p>* GIT keeps file <strong>snapshots<\/strong> not deltas (such as SVN)<br \/>\n* Nearly every operation is local (no need for network connectivity)<br \/>\n* Everything is checksumed (by SHA-1) before being installed<br \/>\n* Uses object database (not relational db) called <em>object store<\/em><\/p>\n<span id=\"Object_Store\"><h3>Object Store<\/h3><\/span>\n<p>* Object store contains three types of objects:<br \/>\n&#8211; blob: represents a file<br \/>\n&#8211; tree: represents a directory<br \/>\n&#8211; commit: represents a snapshot of the entire repository content<br \/>\n&#8211; annotated tags: human readable object associated with a particular commit<br \/>\n* Git objects are stored in <em>.git\/objects<\/em> folder<\/p>\n<span id=\"File_Status\"><h3>File Status<\/h3><\/span>\n<p>* Unstaged: not under Git control<br \/>\n* Staged: added to Git control, e.g. command <em>git add file.txt<\/em> stages file.txt file.<br \/>\n* Committed: added to Git control, e.g. command <em>git commit -m &#8220;Commit message&#8221; file.txt<\/em> adds file.txt file got Git control.<\/p>\n<span id=\"\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/09\/git_file_lc.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/09\/git_file_lc-300x188.jpg\" alt=\"\" title=\"git_file_lc\" width=\"300\" height=\"188\" class=\"aligncenter size-medium wp-image-5915\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/09\/git_file_lc-300x188.jpg 300w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/09\/git_file_lc.jpg 517w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/h6><\/span>\n<span id=\"Checking_File_Status\"><h3>Checking File Status<\/h3><\/span>\n<p>* <em>git status<\/em><\/p>\n<pre lang=\"bash\">\r\n$ git status\r\nOn branch master\r\nnothing to commit, working tree clean\r\n<\/pre>\n<span id=\"Three_Config_Files\"><h3>Three Config Files<\/h3><\/span>\n<p>* <em>\/etc\/gitconfig<\/em><br \/>\n&#8211; System wide configuration<br \/>\n&#8211; For <em>all<\/em> users and <em>all<\/em> repositories<\/p>\n<pre lang=\"bash\">\r\ngit config --system\r\n<\/pre>\n<p>* <em>~\/.gitconfig<\/em><br \/>\n&#8211; User specific configuration<\/p>\n<pre lang=\"bash\">\r\ngit config --global\r\n\r\n# Edit global configuration\r\ngit config --global --edit\r\n<\/pre>\n<p>* <em>.git\/config<\/em><br \/>\n&#8211; Config file in the git directory<br \/>\n&#8211; For single repository<br \/>\n&#8211; Take precedence over <em>~\/.gitconfig<\/em> over <em>\/etc\/gitconfig<\/em><\/p>\n<span id=\"Install\"><h2>Install<\/h2><\/span>\n<span id=\"Windows_Install\"><h3>Windows Install<\/h3><\/span>\n<p>* Download from <a href=\"http:\/\/git-scm.com\/downloads\">git download site<\/a>, e.g. <em>Git-1.8.4-preview20130916.exe<\/em><br \/>\n* Double click to install, accept all defaults<br \/>\n* Open git shell: <strong>All Programs > Git > Git Bash<\/strong><\/p>\n<span id=\"Linux_Install\"><h3>Linux Install<\/h3><\/span>\n<p>* <em>yum install git-core<\/em><br \/>\n* <em>yum install git<\/em><\/p>\n<span id=\"First_Time_Setup\"><h3>First Time Setup<\/h3><\/span>\n<p>* Setup user identity<\/p>\n<pre lang=\"bash\">\r\ngit config --global user.name \"Jimmy Li\"\r\ngit config --global user.email jimmy.li@yahoo.com\r\n<\/pre>\n<p>* Setup editor (defaults to vi)<\/p>\n<pre lang=\"bash\">\r\ngit config --global core.editor vim\r\n\r\n# For Git-for-Windows 2.5.3 and above\r\ngit config --global core.editor notepad\r\n<\/pre>\n<p>* Setup diff tool<\/p>\n<pre lang=\"bash\">\r\ngit config --global merge.tool vimdiff\r\n<\/pre>\n<p>* Setup aliases<\/p>\n<pre lang=\"bash\">\r\ngit config --global alias.co checkout\r\ngit config --global alias.br branch\r\ngit config --global alias.ci commit\r\ngit config --global alias.st status\r\ngit config --global alias.lg \"log --pretty=oneline\"\r\n<\/pre>\n<p>* Check config:<\/p>\n<pre lang=\"bash\">\r\ngit config --list\r\n<\/pre>\n<pre lang=\"bash\">\r\n$ git config --global user.name \"Jimmy Li\"\r\n$ git config --global user.email jimmy.li@yahoo.com\r\n$ git config --list\r\nuser.name=Jimmy Li\r\nuser.email=jimmy.li@yahoo.com\r\nalias.co=checkout\r\nalias.br=branch\r\nalias.ci=commit\r\nalias.st=status\r\nalias.lg=log --pretty=oneline\r\n<\/pre>\n<span id=\"Ignore_Files\"><h3>Ignore Files<\/h3><\/span>\n<p>* Ignore files: place ignore rules in <strong>.gitignore<\/strong> file<br \/>\n* Rules:<\/p>\n<pre lang=\"bash\">\r\n Blank lines or lines starting with # are ignored.\r\n Standard glob patterns work.\r\n You can end patterns with a forward slash (\/) to specify a directory.\r\n You can negate a pattern by starting it with an exclamation point (!).\r\n<\/pre>\n<p>* Examples:<\/p>\n<pre lang=\"bash\">\r\n# a comment - this is ignored\r\n# no .a files\r\n*.a\r\n# but do track lib.a, even though you're ignoring .a files above\r\n!lib.a\r\n# only ignore the root TODO file, not subdir\/TODO\r\n\/TODO\r\n# ignore all files in the build\/ directory\r\nbuild\/\r\n# ignore doc\/notes.txt, but not doc\/server\/arch.txt\r\ndoc\/*.txt\r\n# ignore all .txt files in the doc\/ directory\r\ndoc\/**\/*.txt\r\n\r\n# Ignore Thumbs.db\r\nThumbs.db\r\n\r\n# Ignore $tf folders\r\ntfs\/DEV\/$tf*\/\r\n\r\n# Ignore all folders in Java folder \r\n# but not ant and config subdirectories\r\ntfs\/DEV\/Java\/*\r\n!tfs\/DEV\/Java\/ant\/\r\n!tfs\/DEV\/Java\/config\/\r\n<\/pre>\n<span id=\"Help\"><h3>Help<\/h3><\/span>\n<pre lang=\"bash\">\r\ngit help <command>\r\ngit <command> --help\r\nman git-<command>\r\n\r\ne.g.\r\ngit help config\r\n<\/pre>\n<span id=\"Basics\"><h2>Basics<\/h2><\/span>\n<span id=\"Getting_a_Git_Repository\"><h3>Getting a Git Repository<\/h3><\/span>\n<p>* Initialize a Repository in an Existing Directory<\/p>\n<pre lang=\"bash\">\r\n# Creates a git repository in current directory\r\n# Also setup current directory as working directory \r\ngit init\r\n\r\n# Creates a git repository named my-proj.git without working directory\r\n# Use this for a central repository\r\ngit init --bare my-proj.git\r\n<\/pre>\n<p>* Cloning an Existing Repository. Usually we clone from a central repository to work on.<\/p>\n<pre lang=\"bash\">\r\ngit clone [url]\r\n<\/pre>\n<p>* git url formats:<\/p>\n<pre lang=\"bash\">\r\ngit:\/\/\r\nhttp(s):\/\/\r\nuser@server:\/path.git\r\n\r\n# Local path\r\n--local file:\/\/\/C:\/path\/to\/repo\/my_project\r\n<\/pre>\n<p>* Example<\/p>\n<pre lang=\"bash\">\r\n# Clone a central repository\r\ngit clone file:\/\/\/w\/git\/test2\/my-proj.git\r\n<\/pre>\n<pre lang=\"bash\">\r\n# Clone Azure DevOps using personal auth token\r\n$MyPat = 'xxx'\r\n$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(\"`:$MyPat\"))\r\ngit -c http.extraHeader=\"Authorization: Basic $B64Pat\" clone devops_git_url\r\n<\/pre>\n<span id=\"Recording_Changes_to_the_Repository\"><h3>Recording Changes to the Repository<\/h3><\/span>\n<p>* Check file status:<\/p>\n<pre lang=\"bash\">\r\ngit status\r\n<\/pre>\n<span id=\"Stage_Files\"><h4>Stage Files<\/h4><\/span>\n<p>* Add new files to stage area:<\/p>\n<pre lang=\"bash\">\r\n# Add a particular file\r\ngit add file1.txt\r\n\r\n# Add current directory and all its content to stage area:\r\ngit add .\r\n\r\n# Add a particular directory\r\ngit add com.acme\r\n\r\n# Add all changed and deleted files but not new files\r\ngit add -u\r\n\r\n# Add all files including new files\r\ngit add -A\r\n\r\n# Interactive add\r\ngit add -p\r\n<\/pre>\n<span id=\"Un-Stage_Files\"><h4>Un-Stage Files<\/h4><\/span>\n<p>* Remove staged files<\/p>\n<pre lang=\"bash\">\r\n# Unstage all staged files\r\ngit reset\r\n\r\n# Unstage a particular staged file\r\ngit reset file1.txt\r\ngit reset HEAD file1.txt\r\n\r\n# Interactive unstage\r\ngit reset --patch\r\n\r\n# Reset to remote and discard all local changes. Be careful!\r\ngit reset --hard origin\/master\r\n<\/pre>\n<span id=\"View_Changes\"><h4>View Changes<\/h4><\/span>\n<p>* View <em>un-staged<\/em>, <em>i.e.<\/em> have not being <em>git add<\/em>, changes:<\/p>\n<pre lang=\"bash\">\r\ngit diff web.config\r\n<\/pre>\n<p>* View <em>staged<\/em>, <em>i.e.<\/em> have been <em>git add<\/em> changes:<\/p>\n<pre lang=\"bash\">\r\ngit diff --staged web.config\r\n<\/pre>\n<p>* View diff between any two files:<\/p>\n<pre lang=\"bash\">\r\ngit diff a\/web.config b\/web.config\r\n<\/pre>\n<p>* View diff between any two commits:<\/p>\n<pre lang=\"bash\">\r\ngit diff b7b382a3 f31018af \/\/ Display diff for all files between two commits\r\ngit diff b7b382a3 f31018af web.config\r\n<\/pre>\n<span id=\"Commit_Changes\"><h4>Commit Changes<\/h4><\/span>\n<p>* Commit <em>staged<\/em> changes:<\/p>\n<pre lang=\"bash\">\r\n# Commit all staged files\r\ngit commit -m 'Commit staged files.'\r\n\r\n# Commit a particular staged file\r\ngit commit -m 'Add file1.txt.' file1.txt\r\n<\/pre>\n<span id=\"Remove_Files\"><h4>Remove Files<\/h4><\/span>\n<p>* Remove <em>file1.txt<\/em> from git,<br \/>\n&#8211; <em>also <\/em>remove it from working directory:<\/p>\n<pre lang=\"bash\">\r\ngit rm file1.txt\r\n\r\n# Need to commit to make it into Git\r\ngit commit -m \"Remove fil1.txt.\" file1.txt\r\n\r\n# Log\r\ngit log --pretty=oneline\r\nd546e7cee2d190d1a063735dad91584734d1268c Remove fil1.txt.\r\n17b6750215ba69a7dd132a4469fcfdaa01262d80 Add file1.txt\r\n<\/pre>\n<p>* Examples:<\/p>\n<pre lang=\"bash\">\r\n# Remove all .log files\r\ngit rm log\/\\*.log\r\ngit commit -m \"Remove all log files in log directory.\"\r\n<\/pre>\n<span id=\"Moving_Files\"><h4>Moving Files<\/h4><\/span>\n<pre lang=\"bash\">\r\ngit mv from_file to_file\r\n<\/pre>\n<span id=\"View_Commit_History\"><h4>View Commit History<\/h4><\/span>\n<pre lang=\"bash\">\r\ngit log\r\ngit log -p # show diff\r\ngit log -p -2 # show diff of last two entries\r\ngit log --stat\r\ngit log --pretty=oneline # others: short, full, fuller\r\ngit log --pretty=format:\"%h - %an, %ar : %s\"   # Use custom format\r\ngit log --pretty=format:\"%s %s\" --graph    # Use graph\r\ngit log --since=2.weeks\r\ngit log --pretty=\"%h - %s\" --author=gitster --since=\"2008-10-01\" \\\r\n   --before=\"2008-11-01\" --no-merges -- t\/\r\n\r\n# Show reflog\r\ngit log -g --pretty=oneline\r\n<\/pre>\n<p>* Launch log history in GUI:<br \/>\n<em>gitk<\/em><\/p>\n<span id=\"Stash_Changes\"><h3>Stash Changes<\/h3><\/span>\n<pre lang=\"bash\">\r\n# Stash tracked files, but NOT new files, NOT ignored files\r\ngit stash\r\n\r\n# Stash with message\r\ngit stash save \"Add readme.txt\"\r\n\r\n# Stash tracked files, new files, but NOT ignored files\r\ngit stash -u\r\n\r\n# Stash with message. Note the save keyword\r\ngit stash save -u \"Add readme.txt\"\r\n\r\n# Stash tracked files, new files AND ignored files\r\ngit stash -a\r\n\r\n# Reapply stashed changes, also remove from stashed files\r\ngit stash pop\r\n\r\n# Reapply stashed changes, also do NOT remove from stashed files\r\ngit stash apply\r\n\r\n# You can stash multiple stashes. List all stashes\r\ngit stash list\r\n\r\n# Reapply a specific stash\r\ngit stash pop stash@{2}\r\n<\/pre>\n<span id=\"Undo_Things\"><h3>Undo Things<\/h3><\/span>\n<span id=\"Change_or_Redo_Last_Commit\"><h4>Change or Redo Last Commit<\/h4><\/span>\n<pre lang=\"bash\">\r\n# Redo last commit with new message\r\ngit commit --amend -m \"Redo commit\"\r\n\r\n# Redo last commit with original commit message\r\ngit commit --amend -C HEAD\r\n<\/pre>\n<p>* Example:<\/p>\n<pre lang=\"bash\">\r\n$ vi file1.txt\r\n$ git add file1.txt\r\n$ git ci -m \"Add file1.txt\" file1.txt\r\n[master 7e3524a] Add file1.txt\r\n 1 file changed, 1 insertion(+)\r\n create mode 100644 file1.txt\r\n$ git lg\r\n7e3524a2c39be7aa02f6536bb05f86537cc8119f Add file1.txt\r\n$ vi file1.txt\r\n$ git ci --amend -m \"Redo add file1.txt\" file1.txt\r\n[master c25eb50] Redo add file1.txt\r\n 1 file changed, 2 insertions(+)\r\n create mode 100644 file1.txt\r\n$ git lg\r\nc25eb5041b5aa21ae240fbb379a9b8f27cb73f67 Redo add file1.txt\r\n<\/pre>\n<p>* Another Example<\/p>\n<pre lang=\"bash\">\r\ngit commit -m 'initial commit'\r\ngit add forgotten_file\r\ngit commit --amend -C HEAD\r\n\r\n$ vi file1.txt\r\n$ git add file1.txt\r\n$ git commit -m \"Add some files.\"\r\n[master f29da6b] Add some files.\r\n 1 file changed, 1 insertion(+)\r\n create mode 100644 file1.txt\r\n$ git lg\r\nf29da6be0b4f91575d1bedf4d82ce7c7eef1d248 Add some files.\r\n$ vi file2.txt\r\n$ git add file2.txt\r\n$ git commit --amend -C HEAD\r\n[master fdd5863] Add some files.\r\n 2 files changed, 2 insertions(+)\r\n create mode 100644 file1.txt\r\n create mode 100644 file2.txt\r\n$ git lg\r\nfdd58637e0cb8d56ad1a47438e8526afcd10292f Add some files.\r\n<\/pre>\n<span id=\"Discard_Commits\"><h4>Discard Commits<\/h4><\/span>\n<p>* Discard last commit<\/p>\n<pre lang=\"bash\">\r\ngit reset HEAD~\r\n\r\n$ git lg\r\nfdd58637e0cb8d56ad1a47438e8526afcd10292f Add some files.\r\nea9e63cb37606b7901f5baa91c8c8628d967afa7 Add mylib2\r\n$ git reset HEAD~\r\njiali@My \/w\/git\/test3\/my-proj (master)\r\n$ git lg\r\nea9e63cb37606b7901f5baa91c8c8628d967afa7 Add mylib2\r\n<\/pre>\n<p>* Discard last three commits and resetting the branch tip to the fourth commit back:<\/p>\n<pre lang=\"bash\">\r\ngit reset HEAD~3\r\n<\/pre>\n<span id=\"Revert_Changes\"><h4>Revert Changes<\/h4><\/span>\n<p>* Revert a modified file:<\/p>\n<pre lang=\"bash\">\r\ngit checkout -- modified_file\r\n\r\n$ vi file1.txt\r\n$ git diff file1.txt\r\ndiff --git a\/file1.txt b\/file1.txt\r\nindex e212970..75ac157 100644\r\n--- a\/file1.txt\r\n+++ b\/file1.txt\r\n@@ -1 +1,2 @@\r\n file1\r\n+second line\r\n$ git checkout -- file1.txt\r\n$ git diff file1.txt\r\n<\/pre>\n<p>* Revert all modified files:<\/p>\n<pre lang=\"bash\">\r\ngit checkout .\r\n<\/pre>\n<p>* Return to master branch<\/p>\n<pre lang=\"bash\">\r\ngit checkout master\r\n<\/pre>\n<p>* Checkout <em>file1.txt<\/em> from a particular commit number:<\/p>\n<pre lang=\"bash\">\r\ngit checkout commit_number file1.txt\r\n<\/pre>\n<p>* Checkout a particular commit<\/p>\n<pre lang=\"bash\">\r\ngit checkout commit_number\r\n<\/pre>\n<span id=\"Working_with_Remotes\"><h3>Working with Remotes<\/h3><\/span>\n<span id=\"Show_Remotes\"><h4>Show Remotes<\/h4><\/span>\n<pre lang=\"bash\">\r\ngit remote    # list remote short name\r\ngit remote -v    # also show remote url\r\n<\/pre>\n<span id=\"Add_remote_short_name\"><h4>Add remote short name<\/h4><\/span>\n<pre lang=\"bash\">\r\ngit remote add alias_name remote_url\r\n<\/pre>\n<span id=\"FetchPull_from_Remotes\"><h4>Fetch\/Pull from Remotes<\/h4><\/span>\n<pre lang=\"bash\">\r\ngit fetch remote_name\r\ngit pull remote_name    # fetch and merge\r\n<\/pre>\n<p>* Example:<\/p>\n<pre lang=\"bash\">\r\ngit fetch origin\r\n<\/pre>\n<span id=\"Pushing_to_Remotes\"><h4>Pushing to Remotes<\/h4><\/span>\n<pre lang=\"bash\">\r\ngit push remote_name branch_name\r\n<\/pre>\n<p>* Example:<\/p>\n<pre lang=\"bash\">\r\ngit push origin master\r\n<\/pre>\n<span id=\"Inspecting_a_Remote\"><h4>Inspecting a Remote<\/h4><\/span>\n<pre lang=\"bash\">\r\ngit remote show origin\r\n<\/pre>\n<span id=\"Rename_Remotes\"><h4>Rename Remotes<\/h4><\/span>\n<pre lang=\"bash\">\r\ngit remote rename old_name new_name\r\n<\/pre>\n<span id=\"Remove_Remote\"><h4>Remove Remote<\/h4><\/span>\n<pre lang=\"bash\">\r\ngit remote rm remote_name\r\n<\/pre>\n<span id=\"Branching\"><h2>Branching<\/h2><\/span>\n<p>* Default branch: master<\/p>\n<span id=\"Create_New_Branch\"><h3>Create New Branch<\/h3><\/span>\n<pre lang=\"bash\">\r\n# Create a new branch from current commit\r\ngit checkout -b jimmy\r\n\r\n# Show current HEAD pointer\r\n$ git symbolic-ref HEAD\r\nrefs\/heads\/jimmy\r\n\r\n# Create a new branch from specific commit\r\ngit checkout -b jimmy 09d3cfad\r\n<\/pre>\n<span id=\"Show_Branches\"><h3>Show Branches<\/h3><\/span>\n<p>* Show local branches only:<\/p>\n<pre lang=\"bash\">\r\ngit branch\r\n\r\n git branch\r\n john\r\n master\r\n<\/pre>\n<p>* Show remote branches only:<\/p>\n<pre lang=\"bash\">\r\ngit branch -r\r\n\r\n$ git branch -r\r\n  origin\/HEAD -> origin\/master\r\n  origin\/john\r\n  origin\/master\r\n<\/pre>\n<p>* Show remote branches too:<\/p>\n<pre lang=\"bash\">\r\n$ git ls-remote origin\r\n31104cf88172c94b60820d6d90739bd8ee391ccb        HEAD\r\n31104cf88172c94b60820d6d90739bd8ee391ccb        refs\/heads\/john\r\n31104cf88172c94b60820d6d90739bd8ee391ccb        refs\/heads\/master\r\n<\/pre>\n<p>* Show all branches<\/p>\n<pre lang=\"bash\">\r\ngit branch --all\r\n\r\n$ git branch --all\r\n* john\r\n  master\r\n  remotes\/origin\/HEAD -> origin\/master\r\n  remotes\/origin\/john\r\n  remotes\/origin\/master\r\n<\/pre>\n<p>* Show local branches and last commits<\/p>\n<pre lang=\"bash\">\r\ngit show-branch\r\n\r\n$ git show-branch\r\n* [john] Add john1.txt  # Currently at HEAD branch john\r\n ! [master] Add john1.txt # Non-HEAD branch\r\n--\r\n*+ [john] Add john1.txt # Last commit to john branch \r\n<\/pre>\n<p>* Show log graph<\/p>\n<pre lang=\"bash\">\r\ngit log --graph --all --pretty=oneline\r\n\r\n$ git log --graph --all --pretty=oneline\r\n* 31104cf88172c94b60820d6d90739bd8ee391ccb Add file4.txt\r\n* 09d3cfadd21444f2da6d61a353e9da909a89d062 Add file3.txt\r\n| *-.   969e5ede4d747d2256eaaead70c05b0f14647ec4 On master: file4.txt\r\n| |\\ \\\r\n|\/ \/ \/\r\n| | * 7db8a49ab0a76ac253da633aa2741cc6140f2a72 untracked files on master: 4a0f36\r\n| * 5dae492d06bcdeadba4eddadb3ac4908a5688d6f index on master: 4a0f368 Add file2.\r\n|\/\r\n* 4a0f368da6cd21cf90c7281ccdf5bb806af41c7f Add file2.txt\r\n* 195d2c446d948717ab5fbfe882c4ae4c3e1b9e5e Add file1.txt\r\n<\/pre>\n<span id=\"Switch_Branch\"><h3>Switch Branch<\/h3><\/span>\n<p>* When switching,<br \/>\n&#8211; untracked files are ignored<br \/>\n&#8211; switching is aborted if untracked files already exist in target branch<br \/>\n&#8212; use git checkout &#8211;merge option to merge<\/p>\n<pre lang=\"bash\">\r\n# Switch to master branch\r\ngit checkout master\r\n\r\n$ git symbolic-ref HEAD\r\nrefs\/heads\/master\r\n\r\n# Switch to jimmy branch\r\ngit checkout jimmy\r\n$ git symbolic-ref HEAD\r\nrefs\/heads\/jimmy\r\n<\/pre>\n<span id=\"Delete_Branch\"><h3>Delete Branch<\/h3><\/span>\n<pre lang=\"bash\">\r\n# Save delete a branch\r\n# Branch to be deleted needs to be fully merged\r\ngit branch -d jimmy\r\n\r\n# Force delete a branch\r\ngit bran -D jimmy\r\n<\/pre>\n<span id=\"Renaming_a_Branch\"><h3>Renaming a Branch<\/h3><\/span>\n<pre lang=\"bash\">\r\ngit branch -m old new\r\n\r\n$ git branch -m jimmy john\r\njiali@My \/w\/git\/test3\/my-proj (john)\r\n$ git branch -v\r\n* john   09d3cfa Add file3.txt\r\n  master 4a0f368 [ahead 2] Add file2.txt\r\n<\/pre>\n<span id=\"Push_Branch_to_Remote\"><h3>Push Branch to Remote<\/h3><\/span>\n<pre lang=\"bash\">\r\n$ git push origin john\r\nCounting objects: 10, done.\r\nDelta compression using up to 2 threads.\r\nCompressing objects: 100% (6\/6), done.\r\nWriting objects: 100% (9\/9), 801 bytes, done.\r\nTotal 9 (delta 2), reused 0 (delta 0)\r\nTo file:\/\/\/w\/git\/test2\/my-proj.git\r\n * [new branch]      john -> john\r\n<\/pre>\n<span id=\"Merging\"><h2>Merging<\/h2><\/span>\n<p>* Merge branch john to master:<\/p>\n<pre lang=\"bash\">\r\ngit checkout master\r\ngit merge john\r\n\r\n$ git checkout master\r\nSwitched to branch 'master'\r\nYour branch is ahead of 'origin\/master' by 2 commits.\r\n  (use \"git push\" to publish your local commits)\r\njiali@My \/w\/git\/test3\/my-proj (master)\r\n$ git merge john\r\nUpdating 4a0f368..31104cf\r\nFast-forward\r\n file3.txt | 1 +\r\n file4.txt | 1 +\r\n 2 files changed, 2 insertions(+)\r\n create mode 100644 file3.txt\r\n create mode 100644 file4.txt\r\njiali@My \/w\/git\/test3\/my-proj (master)\r\n<\/pre>\n<span id=\"Examples\"><h2>Examples<\/h2><\/span>\n<span id=\"Generate_Patch_Files\"><h3>Generate Patch Files<\/h3><\/span>\n<p>* Generate a single patch file named <em>patch-083018.patch<\/em> in home directory.<br \/>\n&#8211; The patch file contains past 22 commits.<\/p>\n<pre lang=\"bash\">\r\ngit format-patch -22 --stdout > ~\/patch-083018.patch\r\n<\/pre>\n<span id=\"Issues\"><h2>Issues<\/h2><\/span>\n<span id=\"Couldn8217t_set_refsheadsmaster\"><h3>Couldn&#8217;t set refs\/heads\/master<\/h3><\/span>\n<p>* Error message:<\/p>\n<pre lang=\"bash\">\r\n$ git commit -m \"Test\"\r\nerror: Couldn't set refs\/heads\/master\r\nfatal: cannot update HEAD ref\r\n<\/pre>\n<p>* Fix:<\/p>\n<pre lang=\"bash\">\r\ngit fsck --lost-found\r\n# If still no luck\r\n# go to project root directory and issue:\r\necho ref: refs\/heads\/master >.git\/HEAD\r\n<\/pre>\n<span id=\"File_is_far_too_short_to_be_a_packfile\"><h3>File is far too short to be a packfile<\/h3><\/span>\n<p>* Env:<\/p>\n<pre lang=\"bash\">\r\n$ git version\r\ngit version 1.8.1.msysgit.1\r\n<\/pre>\n<p>&#8211; Win 7<br \/>\n* Error message:<\/p>\n<pre lang=\"bash\">\r\n$ git status\r\nGit error: ... is far too short to be a packfile\r\n<\/pre>\n<p>* Fix:<br \/>\n&#8211; Close Git or Git GUI<br \/>\n&#8211; Close all opened folders, especially mounted drives<br \/>\n&#8211; Retry<\/p>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"http:\/\/git-scm.com\/book\">Pro Git<\/a><br \/>\n* <a href=\"https:\/\/www.atlassian.com\/git\/tutorials\/undoing-changes\/git-revert\">Git Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Features * Created in 2005 * Fully distributed * Fast and efficient with large projects * Incredible branching system for non-linear development (thousands of parallel branches) Diff from CVCS (Centeralized VCS) * GIT keeps file snapshots not deltas (such &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=5911\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[236],"tags":[616],"class_list":["post-5911","post","type-post","status-publish","format-standard","hentry","category-git","tag-git"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s8cRUO-git","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/5911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5911"}],"version-history":[{"count":5,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/5911\/revisions"}],"predecessor-version":[{"id":12934,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/5911\/revisions\/12934"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}