Skip to content

Instantly share code, notes, and snippets.

@savishy
Last active February 19, 2018 06:30
Show Gist options
  • Save savishy/d8551b854e78cfa8a5ff to your computer and use it in GitHub Desktop.
Save savishy/d8551b854e78cfa8a5ff to your computer and use it in GitHub Desktop.
Useful git commands

Git Cheatsheet

A repository of Git commands I find useful.

Further Reading

  1. Interesting discussions on avoiding merge conflicts
  2. Visual Git Reference
  3. Visual Git Workflow

Basic Git Commands

# create working copy dir from remote URL
git clone http://github.com/USER/spring-petclinic
# info about changes etc.
cd spring-petclinic
echo “hello world” > src/foo.txt
git status
git status src/
# different ways to view logs
git log
git log --name-status
# add a new (or modified) file to version control
git add src/foo.txt
git commit OR
git commit -m “some commit message”
# Push to remote.
git push origin master
# Pull from remote.
git pull
git checkout <file>

# To work with the Git repo you need to configure Git command-line .
git config --global user.name "[GIT USERNAME]"
git config --global user.email "[GIT EMAIL]"

# This allows you to type “git st” instead of “git status”, etc.
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status

To change a local working-copy from HTTP to SSH

Situation: You checked out a repository using its http link, and now want to switch to ssh.

This might be necessary if you (for eg) were using HTTP authentication, and then later switched to SSH-based authentication. In such cases your existing working copies are still using the HTTP link for the remote, and need to be switched.

References:

Existing Remote:

$ git remote -v
origin	http://github.com/savishy/vagrant-boxes (fetch)
origin	http://github.com/savishy/vagrant-boxes (push)

Switch the remote:

$ git remote set-url origin [email protected]:savishy/vagrant-boxes.git

Verify the remote is switched

$ git remote -v
origin	[email protected]:savishy/vagrant-boxes.git (fetch)
origin	[email protected]:savishy/vagrant-boxes.git (push)

view decorated tree of git commits

git log --graph --decorate --oneline
  • To get some color into the Git output you can use this command.
  • This simply decorates and prettifies the output.
  • Many Git GUI clients (e.g. SourceTree or Git GUI) show output like this.

Example output:

image

The difference between git add -A and git add

(reference)

  • To 'stage' all files - New + Modified + Deleted: git add -A
  • To stage all new and modified files only (no deleted files): git add .
  • To stage all modified and deleted files (no new files): git add -u

Open previous revision of a file (for comparison)

git show HEAD^:main.cpp > old_main.cpp
  • In this example you want to compare the previous version of a file with its current version
  • In the command above HEAD^ is the previous revision. The :main.cpp indicates you only want the file main.cpp.
  • The output of this is old_main.cpp.

reference

Interact with ("Show") Commits

Show the commit number for HEAD

reference

git rev-parse HEAD.

Show which Commit (SHA) a tag points to.

There are lots of alternative methods in this link: http://stackoverflow.com/q/1862423/682912

# show which commit a tag points to.
# Here TAG_NAME is the name of your tag
git rev-list -n 1 TAG_NAME

Show the changes in a commit or a tag

git show TAG_NAME
git show COMMIT_NUMBER

Some more ways are described in this reference.

List the commit history between two points

This command will show commit messages between two Tags, or a tag and a commit, or two commits etc. This is useful for automatically populating release notes.

(Reference)

git log --pretty=format:%s TAG1..TAG2
git log --pretty=format:%s SHA1..TAG2
git log --pretty=format:%s TAG1..HEAD
git log --pretty=format:%s SHA1..SHA2

Avoiding a "Detached Head"

In "Detached Head" State you can make commits but will not be able to push to the remote branch. To prevent this state always checkout branch with tracking of the remote branch.

# first time: make origin/branchname locally available as localname
git checkout -b localname origin/branchname 

# othertimes 
git checkout localname 

git push origin

References:

  1. Why does "Detached Head" happen
  2. Switch branches without detaching head

Problem: git status is showing me spurious output

Example

Example output:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   .gitignore
        modified:   README.md
        modified:   docker-host/Vagrantfile
        modified:   docker-host/environments/production/modules/install_docker/manifests/init.pp
        modified:   docker-host/environments/production/modules/install_docker/templates/docker.conf
        modified:   docker-host/environments/production/modules/install_oraclient/manifests/init.pp
        modified:   docker-host/environments/production/modules/install_tools/files/emacs/.elisp/anything.el
        modified:   docker-host/environments/production/modules/install_tools/files/emacs/.elisp/balance.el
        modified:   docker-host/environments/production/modules/install_tools/files/emacs/.elisp/browse-tar.el
        modified:   docker-host/environments/production/modules/install_tools/files/emacs/.elisp/buffer-stack.el
        modified:   docker-host/environments/production/modules/install_tools/files/emacs/.elisp/cg-mode.el
        modified:   docker-host/environments/production/modules/install_tools/files/emacs/.elisp/cmake-mode.el
        modified:   docker-host/environments/production/modules/install_tools/files/emacs/.elisp/column-marker.el
        modified:   docker-host/environments/production/modules/install_tools/files/emacs/.elisp/crypt++.el
        modified:   docker-host/environments/production/modules/install_tools/files/emacs/.elisp/crypt.el
        modified:   docker-host/environments/production/modules/install_tools/files/emacs/.elisp/cweb.el
        modified:   docker-host/environments/production/modules/install_tools/files/emacs/.elisp/diminish.el

Possible Cause

  • If you git diff a single file you might see that the diff reports ^M characters at the end of each line.
  • This means Git is unable to auto-detect which line-ending type to use.

Solution

Set core.autocrlf to true.

  • git config core.autocrlf true (local)
  • git config --global core.autocrlf true (global)

Modifying History

To change the name and email address recorded in all your commits

This is to be done only in severe emergencies.

Read this reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment