My git cheat sheet

published Oct 30, 2013 09:05   by admin ( last modified Jun 15, 2015 04:38 )

Pulling, fetching & merging

I want to track a new remote branch

Check what branches are available at remote:

git remote show origin

Then do:

git fetch origin remote_branch_name:local_branch_name

There are differences between my branch and the remote branch, but I want to see what would be merged in, before doing a pull

Do a fetch first

git fetch

and then compare the branches with

git diff HEAD...origin

I want to merge in another branch, how do I pull down the new stuff in that branch without switching to it?

Do :

git merge origin/other_branch

while being in your branch.

A

git pull --all

followed by a a

git merge other_branch

won't work. "git pull --all" fetches all tracked remotes and updates them, but only merges new updates on your current branch. So even if other branches are updated locally, HEAD hasn't moved in them and from merge's point of view the updates aren't there.

How do I merge in a branch so that everything in the incoming branch takes precedence?

In subversion you can do “theirs-full” as a merging strategy. You used to be able to do that in git too, but it has been deprecated and outright removed.  Here is one way of doing a theirs-full. You simply move to the incoming branch, branch it into a temporary branch, switch to master and merge that one into yours

git checkout -b temporary origin/incoming
git merge -s ours master         # ignoring all changes from master
git checkout master
git merge temporary                        # fast-forward to tmp HEAD
git branch -D temporary                    # deleting tmp

See more at:
http://stackoverflow.com/questions/4911794/git-command-for-making-one-branch-like-another

If you want the incoming branch to completely take over, there are a couple of ways of doing that. Assuming the incoming branch is called “incoming”, and that you are on the “master” branch:

git reset --hard incoming

-or-

git branch -d master
git checkout incoming
git -b master

-or-

git checkout -f -b master incoming

Before you do any of the above you may want to create a branch for your old work, or it is effectively rendered inaccessible. Note that any files from your original master not present in incoming, will be ignored.

Pushing

How do I initialize a remote repository with my stuff and make my local repository track the remote?

$ git remote add origin git@gitserver:/opt/git/project.git
$ git push origin master

How do I push to a fresh remote repository?

If the remote repository is fresh and does not have any branches in it, it does not know where to store your stuff, even if you are on master. Use:

git push --all

It will create the necessary branches on remote and effectively copy all your branches over, including the one that you are on.

See:
http://stackoverflow.com/questions/6157730/why-cant-i-push-to-this-bare-repository

I have created/edited tags, but "git push" does not seem to include them

git does not push tags to remote servers unless you specify that explicitly

git push --tags

...for all tags.

git push <tagname>

...for a named tag

I made a new branch locally, and I want to push it to remote

You need to use

git push -u origin branchname

It will also set up your local branch to track the remote one.

Do not just do a

git push

, because that will push master to master on remote, even if you are on your new local branch.

git push --all

should work too, if you want to push all branches to remote.

Where are the remotes?

git remote -v

shows you what you’ve got
A local branch can track a remote branch. git clone automatically sets up this

Undoing stuff

How do I revert unstaged files to last commit?

git checkout -- .

How do I unstage a file?

git reset HEAD filename

How do i undo my last commit?

Most drastic

Undo your last commit and bring your workspace and staging area back to the state of the last commit:

git reset --hard HEAD~1

if you want to keep your workspace as it is now:

git reset HEAD~1

Least drastic

if you only want to undo the commit but keep both your index (staging area) and your work space as they are now:

git reset --soft HEAD~1

Any of these resets will erase your most recent commit from the commit history! There are ways of getting it back but it is a bit of work.

See: http://stackoverflow.com/questions/927358/how-to-undo-the-last-git-commit

Aborting a merge when you are in the middle of it

Hit Ctrl-c to exit the merge tool dialog
If your git is 1.7.4 or newer:

git merge --abort

If your git is older than 1.7.4:

git reset --merge HEAD
git clean -f

(but this latter will remove all untracked non-ignored files, may not be want you wanted, however you should had made sure of a clean state before the merge, and then its cool)
One would have thought that git stash would help here, but you cannot stash merge conflicts, so it is not an option

My editor removed a file by mistake, how do I get it back and/or
I want to revert a file to last commit


You may see it staged for deletion. Then do a checkout to the branch you are already on, for that particular file:

git checkout -- file_that_was_deleted

It works without the “--”, but since your file name may correspond with a branch name it is best to put “--” in.

git submodule

You can use sub modules in a fruitful way if you’re on git 1.8.2 or later:

http://stackoverflow.com/questions/9189575/git-submodule-tracking-latest/9189815#9189815

http://www.vogella.com/articles/Git/article.html#submodules

How do I get a version of git that is 1.8.2 or newer?

Github and bitbucket already run by default on newer versions. So does newer versions of Ubuntu.

If you are on an older Ubuntu, use:

sudo add-apt-repository  ppa:git-core/ppa

from: https://launchpad.net/~git-core/+archive/ppa

Adding a submodule to a repository

git submodule add -b branch <url> dirname
git submodule add -b master ssh://git@git.example.com/srv/git/awesome_lib.git awesome_lib

When cloning a repository that has a submodule :

When you clone the  parent repository, the submodule does not get cloned into its sub directory. For that you have to do:

git submodule init
git submodule update

How to to remove a submodule

This seems quite involved, but I have successfully done so, following these instructions:

Remove a Submodule within git

Other

How do I add an empty directory in git?


You can’t. Commit an empty .gitkeep file in there and it works though.

git add --patch

Allows for staging just parts of a file, good when you want to make a commit that does not fix two or more things at once.

git add


git add -u will add all changed files it knows about to the staging area, from where you issue the command and recursively downwards. In other words, if you do git add -u in a subdirectry, only changes within that subdirectory will be staged.