Table of Contents
How can I import a Mercurial repository to GitHub?
How can I rename Git branches?
How can I find the Git commit corresponding to a Subversion revision?
How can I create a patch from uncommitted changes to a Git repository?
How can I apply a patch to a Git repository?
How can I set my username and email address in my Git client?
How can I change the text editor used by Git?
How can I check out a remote branch?
How can I check out a specific commit of a repository?
How can I check out a specific tag of a repository?
How can I retrieve the state of a repository before a specific date?
How can I track all remote Git branches?
How can I push a local branch to a remote Git repository?
How can I push a local Git branch and automatically track it?
How can I unstage something from the Git staging area?
How can I merge Git branches?
How can I tell if a Git branch has not been merged into the master branch?
How can I destroy my last Git commit but keep its changes?
How can I destroy my last Git commit and its changes?
How can I do a pull while my Git repository has outstanding changes?
How can I edit my last Git commit message?
How can I squash two local Git commits into one?
How can I apply a commit from one Git branch to another?
How can I view a particular revision of a file in my Git repository without checking it out?
How can I get the list of tags in a Git repository?
How can I tag a Git commit?
How can I remove a remote Git tag?
How can I delete a remote Git branch?
How can I archive a Git branch?
How can I delete a remote Git commit?
How can I include or exclude certain files from Git?
How can I more easily view diffs from Git?
How can I resolve Git merge conflicts visually?
How can I colorize Git output?
How can I restore a particular version of a file from a Git repository?
How can I create shorthands for Git commands?
How can I create a patch for my last Git stash?
How can I find the common ancestor of two Git branches?
How can I delete all Git branches that have been merged?
How can I add a submodule to a Git repository?
How can I remove a submodule from a Git repository?
How can I check out a Git repository with submodules?
How can I pull changes to submodules?
How can I commit changes to submodules?
How can I install the latest stable release of Git?
How can I import a Mercurial repository to GitHub?
21 Apr 2014
-
mercurial,
git
Run the following commands to install git remote hg.
wget https://raw.github.com/felipec/git/fc/master/git-remote-hg.py -O ~/apps/git-remote-hg
chmod +x ~/apps/git-remote-hg
export PATH=$PATH:~/apps
Clone the mercurial repository as a bare git repository.
git clone --bare "hg::https://code.google.com/p/jsr308-langtools/" bare-jsr308-langtools
Create an empty GitHub repository
git push --mirror https://github.com/reprogrammer/jsr308-langtools.git
Mirror the bare git repository on github.
cd bare-jsr308-langtools
git push --mirror https://github.com/reprogrammer/jsr308-langtools.git
References
- https://github.com/felipec/git/wiki/git-remote-hg
- https://help.github.com/articles/importing-an-external-git-repository
How can I rename Git branches?
13 Dec 2013
-
git
The following command renames a local branch from b1
to b2
.
git branch -m b1 b2
The following command removes the remote branch b1
.
git push origin --delete :b1
Before running the above command, make sure that b1
is not the default branch
of your repository; otherwise, git will give you an error like the following.
remote: error: refusing to delete the current branch: refs/heads/b1
To https://github.com/user/repo.git
! [remote rejected] b1 (deletion of the current branch prohibited)
error: failed to push some refs to 'https://github.com/user/repo.git'
The following command gives b2
the new remote.
git push -u origin b2
References
- http://stackoverflow.com/a/7084316
- http://sebgoo.blogspot.com/2012/04/git-rename-branch-local-and-remote.html
- http://stackoverflow.com/a/2003515
How can I find the Git commit corresponding to a Subversion revision?
26 Nov 2013
-
git,
subversion
Let S be the URL of a Subversion repository that has been migrated to a Git repository at URL G using git-svn. Given the revision number R of a revision of S, the problem is to find a commit C in G that corresponds to R.
For example, let S = http://svn.apache.org/repos/asf/1309183/tomcat/trunk/, G = https://github.com/apache/tomcat.git, and R = 1309183.
The first step is find the largest revision number R2 such that R2 <= R and S is affected by R2. The following command will return R2.
svn list --verbose S@R | grep "\./" | awk '{split($0,a," "); print a[1]}'
In this case, the above command would look as follows.
svn list --verbose http://svn.apache.org/repos/asf/tomcat/trunk/@1309183 | grep "\./" | awk '{split($0,a," "); print a[1]}'
Next, use a command of the form below to find C.
git log | grep "@R2" -C 10
In this case, the above command should be instantiated as follows.
git log | grep "@1307597" -C 10
References
How can I create a patch from uncommitted changes to a Git repository?
07 Oct 2013
-
git
git diff --cached > patch.diff
References
How can I apply a patch to a Git repository?
06 Oct 2013
-
git
git apply patch.diff
References
How can I set my username and email address in my Git client?
14 Sep 2013
-
git
git config --global user.email "username@domain.com"
git config --global user.name "First Last"
References
- https://help.github.com/articles/setting-your-email-in-git
- https://help.github.com/articles/setting-your-username-in-git
How can I change the text editor used by Git?
13 Sep 2013
-
git
git config --global core.editor "vim"
References
How can I check out a remote branch?
12 Sep 2013
-
git
git checkout -b branch-name origin/branch-name
References
How can I check out a specific commit of a repository?
11 Sep 2013
-
git
git checkout -b new-branch SHA1
References
How can I check out a specific tag of a repository?
10 Sep 2013
-
git
git checkout -b new-branch tags/<tag-name>
How can I retrieve the state of a repository before a specific date?
09 Sep 2013
-
git
git checkout `git rev-list -n 1 --before="2009-07-27 13:37" master`
References
How can I track all remote Git branches?
08 Sep 2013
-
git
First, get a list of all remote branches:
git branch -r
Then, check out each of remote branch:
git checkout -t origin/branch1
References
How can I push a local branch to a remote Git repository?
07 Sep 2013
-
git
git push <remote_repository> <local_branch_name>
How can I push a local Git branch and automatically track it?
06 Sep 2013
-
git
git push -u origin <name_of_branch>
Git will by default push all branches that have the same name on the remote. To limit this behavior to just the current branch, set this configuration option:
git config --global push.default tracking
References
How can I unstage something from the Git staging area?
05 Sep 2013
-
git
git reset <file_name>
How can I merge Git branches?
04 Sep 2013
-
git
git merge <branch_1> <branch_2>
The order which you specify the branch has some consequence on how it is done
internally. Graphically it will try to merge <branch_1>
first and then
<branch_2>
even if <branch_2>
is earlier time-wise. So, if you want to have
a nice ordering, try to specify the earlier branch as an earlier argument.
How can I tell if a Git branch has not been merged into the master branch?
03 Sep 2013
-
git
git cherry -v master branch-name
References
How can I destroy my last Git commit but keep its changes?
02 Sep 2013
-
git
git reset --soft HEAD^
References
How can I destroy my last Git commit and its changes?
01 Sep 2013
-
git
git reset --hard ORIG_HEAD
Basically, ORIG_HEAD
is short for the commit before your last commit. It’s
automatically set by pull and merge (but not necessarily other git commands).
It’s also equivalent to HEAD@{1}
meaning the previous commit from head.
References
How can I do a pull while my Git repository has outstanding changes?
31 Aug 2013
-
git
git stash
git pull
git stash pop
How can I edit my last Git commit message?
30 Aug 2013
-
git
git commit --amend
How can I squash two local Git commits into one?
29 Aug 2013
-
git
Use the following commit to start rebase
in the interactive mode. You should
use a <SHA>
that is a ascendant of the commits you want to manipulate.
git rebase -i <SHA>
The interactive rebase
lets you perform a command on each commit that it
lists. If you want to squash commit 2
into commit 1
, you should make sure
that the rebase
editor shows you only the two commits, then you have to use
the pick command on commit 1
and the squash
command on the line of commit
2
.
If for any reason, you have to quit the rebase, close the editor and execute the following command.
git rebase --abort
How can I apply a commit from one Git branch to another?
28 Aug 2013
-
git
Let s
be the SHA of the commit you’d like to apply to branch b
. Execute the
following commands to add commit s
to branch b
.
git checkout b
git cherry-pick s
How can I view a particular revision of a file in my Git repository without checking it out?
27 Aug 2013
-
git
The following command shows the contents of /path/to/file.txt
from 4 revisions
ago.
git show HEAD~4:/path/to/file.txt
References
How can I get the list of tags in a Git repository?
26 Aug 2013
-
git
git tag -l
References
How can I tag a Git commit?
25 Aug 2013
-
git
git tag <tag-name> <commit-sha>
git push --tags
How can I remove a remote Git tag?
24 Aug 2013
-
git
git tag -d <tag-name>
git push origin :refs/tags/<tag-name>
References
How can I delete a remote Git branch?
23 Aug 2013
-
git
git push origin :<branch-name-to-delete>
How can I archive a Git branch?
22 Aug 2013
-
git
Instead of just deleting a branch, it might be a good idea to keep a tag to the old branch.
# Get remote branches verbosely (with SHA and some description)
git branch -a -v
# Pick the old branch that you want to archive and note its SHA
git tag archive/<old-branch-name> <old-branch-SHA>
# Delete the local branch
git branch -d <old-branch-name>
# Push the tag to remote repository
git push --tags
# Delete the remote branch
git push origin :<old-branch-name>
# (Optional for other people monitoring the remote repository)
# This removes outdated branches from their local repository
git remote prune origin
References
How can I delete a remote Git commit?
21 Aug 2013
-
git
If you push a commit to the remote repository. You can remove it from the remote repository using the following command.
git push -f origin HEAD^:master
I’ve tried this command on github and made sure that it works. As a result of this command, your commit on the remote repository doesn’t get actually deleted. Rather, it becomes dangling. And, it seems that Github garbage collects dangling commits every so often.
References
How can I include or exclude certain files from Git?
20 Aug 2013
-
git
Sometimes, you want to exclude all the (for instance) .class
or .jar
files
from git. In that case, you would create a .gitignore
folder at the top of
your repository with contains like this:
*.class
*.jar
On the other hand, sometimes you also want to include certain .class
or .jar
files in only several directories. In that case, you would create a .gitignore
in those particular directories like this:
!*.class
!*.jar
The key symbol here is the !
symbol.
Git operates by reading the repository-level .gitignore
but will override any
settings upon reading a directory-level .gitignore
.
How can I more easily view diffs from Git?
19 Aug 2013
-
git
Say that you want to see what has changed between two different commits. You can do
git diff sha1..sha2
The above command will give you all the differences between sha1 (exclusive) to sha2 (inclusive). However, textual diffs can be hard to understand sometimes, so how would you specify a visual diff tool? The easiest way would be to specify the name of the visual diff tool directly to git difftool i.e.
git difftool -t path_to_tool sha1..sha2
If you find yourself using a particular visual diff tool pretty often, then you
should add it to your global git config file, shown with a hypothetical tool
called awesometool
:
git config --global diff.tool awesometool
git config --global difftool.awesometool.cmd "path/to/awesometool \$LOCAL \$REMOTE"
# The following gets rid of having to hit enter when you run git difftool
git config --global difftool.prompt false
which will create the following entries in your global .gitconfig
[diff]
tool = awesometool
[difftool "awesometool"]
cmd = path/to/awesometool \"$LOCAL\" \"$REMOTE\"
[difftool]
prompt = false
Popular visual diff tools are vimdiff, kdiff3, meld, etc. You can specify several programs (not just a single one) that you use frequently through the global git config.
git difftool sha1..sha2
Once you add your visual diff tool to git configurations, you can simply invoke
git difftool
as follows.
References
How can I resolve Git merge conflicts visually?
18 Aug 2013
-
git
You can use a visual merge tool like
diffmerge. You would use the same
command to git visual diff except that you use git mergetool
instead of git
difftool
.
git config --global merge.tool awesometool
git config --global mergetool.awesometool.cmd "path/to/awesometool --merge --result=\"$MERGED\" \"$LOCAL\" \"$BASE\" \"$REMOTE\""
git config --global mergetool.awesometool.trustExitCode true
References
How can I colorize Git output?
17 Aug 2013
-
git
git config --global color.ui true
The above command adds the following item to your git configuration files.
[color]
ui = true
How can I restore a particular version of a file from a Git repository?
16 Aug 2013
-
git
The following command will replace the current version with the older version.
If you just want to see the older version of the file, use git show
.
git checkout SHA1 path/to/file
References
How can I create shorthands for Git commands?
15 Aug 2013
-
git
Set up aliases in your global ~/.gitconfig
. Here’s a set of useful examples
that you can add to your ~/.gitconfig
file:
[alias]
st = status
ci = commit
br = branch
co = checkout
df = diff
lg = log -p
Now, you can use git st
instead of git status
.
References
How can I create a patch for my last Git stash?
14 Aug 2013
-
git
git stash show -p
References
How can I find the common ancestor of two Git branches?
13 Aug 2013
-
git
There isn’t a command to find the common ancestor per-se, but there is a
plumbing command, git-merge-base
in Git for finding the best ancestor for use
in a three-way merge. Given a series of branches, git-merge-base
attempts to
find the best ancestor to merge. When you provide it with two branches as
arguments, git-merge-base
will naturally locate their common ancestor, if one
exists. This can be useful sometimes when you are trying to trace the common
ancestor between branches.
git-merge-base branchA branchB
How can I delete all Git branches that have been merged?
12 Aug 2013
-
git
git branch -r --merged | \
grep origin | \
grep -v '>' | \
grep -v master | \
xargs -L1 | \
awk '{split($0,a,"/"); print a[2]}' | \
xargs git push origin --delete
References
How can I add a submodule to a Git repository?
11 Aug 2013
-
git
The following commands checkouts the git repository at subproject-url
into the
subproject-path
and updates the .gitmodules
file.
git submodule add subproject-url subproject-path
How can I remove a submodule from a Git repository?
10 Aug 2013
-
git
- Delete the relevant section from the
.gitmodules
file. - Delete the relevant section from
.git/config
. - Run
git rm --cached subproject-path
(no trailing slash). - Commit and delete the now untracked submodule files.
References
How can I check out a Git repository with submodules?
09 Aug 2013
-
git
# to add the subproject to .git/config.
git submodule init
# to pull down the latest changes to the submodule.
git submodule update
References
How can I pull changes to submodules?
08 Aug 2013
-
git
You have to go to the submodule and switch to a specific branch to be able to pull changes.
cd subproject-path
git checkout branch-name
git pull
References
How can I commit changes to submodules?
07 Aug 2013
-
git
If you make changes to the submodule, you have to fist commit and push them. Then, you have to commit and push the changes to the superproject.
How can I install the latest stable release of Git?
25 Feb 2013
-
linux,
git
The following commands install the latest stable release of git.
sudo add-apt-repository ppa:git-core/ppa
sudo aptitude update
sudo aptitude upgrade
References