A repository of Git commands I find useful.
# 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
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)
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:
- 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
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 filemain.cpp
. - The output of this is
old_main.cpp
.
git rev-parse HEAD
.
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
git show TAG_NAME
git show COMMIT_NUMBER
Some more ways are described in this reference.
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.
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
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:
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)
❗ This is to be done only in severe emergencies.