Git commands cheat sheet
If you want to learn about Git I highly recommend Pro Git book, written by Scott Chacon and Ben Straub. Online version of the book is available here https://git-scm.com/book/en/v2
Based on the Pro Git book the article here is a quick reference of the most common Git commands mentioned in the book.
All content is licensed under the Creative Commons Attribution Non Commercial Share Alike 3.0 license.
$ cd /u02/git/poc/myproj1
$ git init
$ cd /u02/git/poc/
$ git clone https://github.com//myproj1.git
This will create a directory called myproj1 under which all the git code will be copied. If you want to clone the repository into a directory other than myproj1 add an additional argument of the desired directory name.
$ cd /u02/git/poc/
$ git clone https://github.com//myproj1 my_project
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
$ echo "Test File" >> README.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
File that we created is showing under Untracked files, meaning the Git is not tracking these files for any changes.
Next step is to ask Git to start tracking these files.
$ git add README.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD..." to unstage)
new file: README.md
As you can see the file has moved from Untracked files to Changes to be committed.
Let’s try to change a file that was already tracked by Git.
$ echo "Test File with changes 1" >> EXISTING_FILE.md$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD..." to unstage)
new file: README.md
Changes not staged for commit:
(use "git add..." to update what will be committed)
(use "git checkout --..." to discard changes in working directory)
modified: EXISTING_FILE.md
The EXISTING_FILE.md file appears under Changes not staged for commit which means that a file that is being tracked by Git has been modified in the working directory but not yet staged.
Now lets add the changes we made to EXISTING_FILE.md for Git tracking.
$ git add EXISTING_FILE.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD..." to unstage)
new file: README.md
modified: EXISTING_FILE.md
Now, lets modify EXISTING_FILE.md once again
$ echo "Test File with changes 2" >> EXISTING_FILE.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD..." to unstage)
new file: README.md
modified: EXISTING_FILE.md
Changes not staged for commit:
(use "git add..." to update what will be committed)
(use "git checkout --..." to discard changes in working directory)
modified: EXISTING_FILE.md
EXISTING_FILE.md is listed as both staged (Changed to be committed) and unstaged (Changes not staged for commit) because Git stages a file exactly as when we run the git add command. So the changes we did after git add command are not commit yet.
Example .gitignore file:
# ignore all .tmp files
*.tmp
# but do track lib.tmp, even though you're ignoring .tmp files above
!lib.tmp
# only ignore the tmp file in the current directory, not subdir/tmp
/tmp
# ignore all files in any directory named tmp
tmp/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory and any of its subdirectories
doc/**/*.pdf
$ git diff
to see what you’ve staged that will go into your next commit, you can use git diff --staged
$ git diff --staged
$ git commit -m "Comment"
$ git rm README.md
rm 'README.md'
$ git status
On branch dev
Your branch is up-to-date with 'origin/dev'.
Changes to be committed:
(use "git reset HEAD..." to unstage)
deleted: README.md
If you modified the file or had already added it to the staging area, you must force the removal with the -f option.
If you may want to keep the file on your hard drive but not have Git track it anymore.
$ git rm --cached README
$ git mv file_from file_to
$ git log
With no arguments, git log lists the commits made in the repository in reverse chronological order. Command lists each commit with its SHA-1 checksum, the author’s name and email, the date written, and the commit message.
To see difference in each commit
$ git log -p -2
To see abbreviated stats for each commit
$ git log --stat
The oneline option prints each commit on a single line
$ git log --pretty=oneline
For custom formatting
$ git add *
$ git remote
origin
$ git remote -v
origin https://github.com//my_proj.git (fetch)
origin https://github.com//my_proj.git (push)
$ git remote
origin
$ git remote add usr1 https://github.com/user1/myproj1
$ git remote -v
origin https://github.com/xx/proj_xx (fetch)
origin https://github.com/xx/proj_xx (push)
usr1 https://github.com/user1/myproj1 (fetch)
usr1 https://github.com/user1/myproj1 (push)
$ git fetch usr1
* remote origin
Fetch URL: https://github.com/schacon/ticgit
Push URL: https://github.com/schacon/ticgit
HEAD branch: master
Remote branches:
master tracked
dev-branch tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
$ git remote rename pb paul
$ git remote
origin
paul
If you want to remove a remote for some reason
$ git remote remove paul
$ git remote
origin
$ git tag -a v1.2 9fceb02
To push a specific tag details to remote
$ git push origin v1.5
To push all tag details to remote
$ git push origin --tags
$ git push origin :refs/tags/v1.4-lw
or
$ git push origin --delete
$ git checkout 2.0.0
In “detached HEAD” state, if we make changes and then create a commit, the tag will stay the same, but our new commit won’t belong to any branch and will be unreachable, except by the exact commit hash.
$ git branch testing
Git uses a special pointer called HEAD to know which branch we are currently pointing to.
$ git log --oneline --decorate
* indicates the branch that you currently have checked out
$ git checkout testing
This moves HEAD to point to the testing branch.
To print the history of commits, showing where branch pointers are and how history has diverged.
$ git log --oneline --decorate --graph --all
$ git checkout master
Delete hotfix branch
$ git branch -d hotfix
If you want to see which files are unmerged at any point after a merge conflict, you can run git status:
Git adds standard conflict-resolution markers to the files that have conflicts, so you can open them manually and resolve those conflicts. Your file contains a section that looks something like this:
All content is licensed under the Creative Commons Attribution Non Commercial Share Alike 3.0 license.
Git Repository
Initializing
To add a directory that is currently not under version control to Git, go to that project’s directory and initialize. This creates a new subdirectory named .git that contains repository files.$ cd /u02/git/poc/myproj1
$ git init
Cloning an Existing Repository
If the repository exists and you want to set up on your laptop, easiest option is to clone the whole repository. There are options to clone a single branch if you want to.$ cd /u02/git/poc/
$ git clone https://github.com/
This will create a directory called myproj1 under which all the git code will be copied. If you want to clone the repository into a directory other than myproj1 add an additional argument of the desired directory name.
$ cd /u02/git/poc/
$ git clone https://github.com/
Checking the Status of Your Files
As a best practise always check the status of the files in the Git repository before you start and operation.$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
Add files to git
Now that we know its a clean clone of the master, lets try to add some files to the Git.$ echo "Test File" >> README.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add
README.md
nothing added to commit but untracked files present (use "git add" to track)
File that we created is showing under Untracked files, meaning the Git is not tracking these files for any changes.
Next step is to ask Git to start tracking these files.
Tracking Files
To begin tracking files$ git add README.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD
new file: README.md
As you can see the file has moved from Untracked files to Changes to be committed.
Let’s try to change a file that was already tracked by Git.
$ echo "Test File with changes 1" >> EXISTING_FILE.md$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD
new file: README.md
Changes not staged for commit:
(use "git add
(use "git checkout --
modified: EXISTING_FILE.md
The EXISTING_FILE.md file appears under Changes not staged for commit which means that a file that is being tracked by Git has been modified in the working directory but not yet staged.
Now lets add the changes we made to EXISTING_FILE.md for Git tracking.
$ git add EXISTING_FILE.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD
new file: README.md
modified: EXISTING_FILE.md
Now, lets modify EXISTING_FILE.md once again
$ echo "Test File with changes 2" >> EXISTING_FILE.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD
new file: README.md
modified: EXISTING_FILE.md
Changes not staged for commit:
(use "git add
(use "git checkout --
modified: EXISTING_FILE.md
EXISTING_FILE.md is listed as both staged (Changed to be committed) and unstaged (Changes not staged for commit) because Git stages a file exactly as when we run the git add command. So the changes we did after git add command are not commit yet.
Ignoring Files
Depending on the Windows, Mac, Linux platform being used or the technology there will be requirement to skip some temp files while check-in code to Git. To achieve this you can create a file called as .gitignore with list of file names or patterns to match that should be ignored.Example .gitignore file:
# ignore all .tmp files
*.tmp
# but do track lib.tmp, even though you're ignoring .tmp files above
!lib.tmp
# only ignore the tmp file in the current directory, not subdir/tmp
/tmp
# ignore all files in any directory named tmp
tmp/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory and any of its subdirectories
doc/**/*.pdf
View Staged and Unstaged Changes
To see what you’ve changed but not yet staged, type git diff with no other arguments.$ git diff
to see what you’ve staged that will go into your next commit, you can use git diff --staged
$ git diff --staged
Committing Your Changes
If you are ready to commit your changes to Git$ git commit -m "Comment"
Removing Files
To remove a file from Git, we need to remove it from staging area and then commit. The git rm command does that, and also removes the file from working directory so we don’t see it as an untracked file the next time around.$ git rm README.md
rm 'README.md'
$ git status
On branch dev
Your branch is up-to-date with 'origin/dev'.
Changes to be committed:
(use "git reset HEAD
deleted: README.md
If you modified the file or had already added it to the staging area, you must force the removal with the -f option.
If you may want to keep the file on your hard drive but not have Git track it anymore.
$ git rm --cached README
Moving Files
If you want to rename a file in Git$ git mv file_from file_to
Commit History
To check the history of all the commits for a repository$ git log
With no arguments, git log lists the commits made in the repository in reverse chronological order. Command lists each commit with its SHA-1 checksum, the author’s name and email, the date written, and the commit message.
To see difference in each commit
$ git log -p -2
To see abbreviated stats for each commit
$ git log --stat
The oneline option prints each commit on a single line
$ git log --pretty=oneline
For custom formatting
$ git log --pretty=format:"%h, %an, %ar, %s"
$ git commit -m 'initial commit'
Amending Commit
If you want to amend the commit$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend
Unstaging a Staged File
To remove the changes staged$ git add *
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md
$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
renamed: README.md -> README
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: CONTRIBUTING.md
To revert a file back to what it looked like when we last committed.
$ git checkout -- CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
renamed: README.md -> README
Remote Repository
To see which remote servers we have configured$ git remote
origin
$ git remote -v
origin https://github.com/
origin https://github.com/
Adding Remote Repositories
To add a new remote Git repository as a shortname we can reference easily$ git remote
origin
$ git remote add usr1 https://github.com/user1/myproj1
$ git remote -v
origin https://github.com/xx/proj_xx (fetch)
origin https://github.com/xx/proj_xx (push)
usr1 https://github.com/user1/myproj1 (fetch)
usr1 https://github.com/user1/myproj1 (push)
Fetching and Pulling from Your Remotes
$ git fetch$ git fetch usr1
Pushing to Your Remotes
$ git push origin masterInspecting a Remote
$ git remote show origin* remote origin
Fetch URL: https://github.com/schacon/ticgit
Push URL: https://github.com/schacon/ticgit
HEAD branch: master
Remote branches:
master tracked
dev-branch tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
Renaming and Removing Remotes
You can run git remote rename to change a remote’s shortname. For instance, if you want to rename pb to paul, you can do so with git remote rename:$ git remote rename pb paul
$ git remote
origin
paul
If you want to remove a remote for some reason
$ git remote remove paul
$ git remote
origin
Tags
To list all tags
$ git tag
$ git tag v1.4-lw
You can also search for tags that match a particular pattern.
$ git tag -l "v1.8.5*"
Git supports two types of tags: lightweight and annotated.A lightweight tag is very much like a branch that doesn’t change — it’s just a pointer to a specific commit. Annotated tags, however, are stored as full objects in the Git database.
Annotated Tags
$ git tag -a v1.4 -m "my version 1.4"
Lightweight Tags
To add a tag later
$ git push origin v1.5
To push all tag details to remote
Delete Tag
To delete tag from local repository
$ git tag -d v1.4-lw
To delete a tag from a remote server.
git push :refs/tags/
git push
$ git push origin :refs/tags/v1.4-lw
or
$ git push origin --delete
Checking out Tags
To view the versions of files a tag is pointing to, we can do a git checkout of that tag, although this puts the repository in “detached HEAD” state$ git checkout 2.0.0
In “detached HEAD” state, if we make changes and then create a commit, the tag will stay the same, but our new commit won’t belong to any branch and will be unreachable, except by the exact commit hash.
If we want to make changes, example for fixing a bug on an older version, better option is to create a branch.
$ git checkout -b version2 v2.0.0
Branching
To create a new branch
Git uses a special pointer called HEAD to know which branch we are currently pointing to.
$ git log --oneline --decorate
List Branch
$ git branch
iss53
* master
testing
To see the last commit on each branch
$ git branch -v
iss53 93b412c fix javascript issue
* master 7a98805 Merge branch 'iss53'
testing 782fd34 add scott to the author list in the readmes
To see which branches are already merged into the branch you’re on
$ git branch --merged
iss53
* master
To see all the branches that contain work you haven’t yet merged in
$ git branch --no-merged
testing
List Remote Branches
$ git ls-remote
From https://github.com//mylearning.git
fb047f36ae03cbfd3458f98e23631970e66bbce8 HEAD
fb047f36ae03cbfd3458f98e23631970e66bbce8 refs/heads/master
$ git remote show
origin
Switching Branches
To switch to an existing branch, run the git checkout command.$ git checkout testing
This moves HEAD to point to the testing branch.
To print the history of commits, showing where branch pointers are and how history has diverged.
$ git log --oneline --decorate --graph --all
Merging Branches
To merge a hotfix branch with master$ git checkout master
$ git merge hotfix
Delete hotfix branch
$ git branch -d hotfix
Merge Conflicts
If you change the same part of the same file differently in the two branches you’re merging, Git won’t be able to merge them cleanly.
$ git merge iss53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
If you want to see which files are unmerged at any point after a merge conflict, you can run git status:
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add ..." to mark resolution)
both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
Git adds standard conflict-resolution markers to the files that have conflicts, so you can open them manually and resolve those conflicts. Your file contains a section that looks something like this:
<<<<<<< HEAD:index.html
=======
please contact us at support@github.com
>>>>>>> iss53:index.html
If you want to use a graphical tool to resolve these issues, you can run git mergetool, which fires up an appropriate visual merge tool and walks you through the conflicts:
$ git mergetool
Comments