Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Tuesday, September 6, 2016

git: Using the branches in a remote repository

This is more of a personal note -- something that I need to record in order to use git efficiently.

If you git clone a repository in (e.g. from GitHub), you generally end up on the 'master' or 'origin' branches, assuming that you accept the default nomenclature for referring to a remote repository. To show the other branches available to you:
git remote show [origin]
or
git ls-remote
To show you what you are currently tracking:
git branch -vv
You can then switch to one of the remote branches shown in ls-remote:
git checkout remote_branch
You may find that git warns you about untracked files that are present that may be overwritten by changing the branch.

Assuming you have a bunch of untracked files or cruft that you are certain that don't need and you want to discard:
git clean -d -fx ""
... and then you can proceed with the checkout.

Thursday, July 23, 2009

Git commands I love to forget

Here are the most useful GIT commands that I *always* forget.

To push a local branch to your remote repository:

git push remote_repository_name branch_name

Eg: git push origin my_new_branch

Also:

Remove branch from repo.

git push {repo} :heads/{branch}

Ex: git push origin :somebranch
removes somebranch

This works for removing a tag as well git push origin :sometag

You might also want to check out git-publish-branch‘s ’-d’ option.

Thank you, GitHub, for these handy pages. But for me, these are the handiest of the handy.

Fixing an accidental merge (discarding local commits)

Let's say that you merged branch my_branch into master, when, in fact, you wanted to merge master into my_branch. Check git log:

commit 2a4b206e42360b812d6110c5ae9eeced96d99f0e
Merge: 4147391... dd525b5...
Author: baitisj
Date: Thu Jul 23 13:13:11 2009 -0700
Merge branch 'my_branch' of git@blablobbbla:somewhere/repository


commit 4147391a181a05f9754ddf8eecc6c4398dbb1d17
Author: baitisj
Date: Thu Jul 23 13:12:28 2009 -0700

latest

...
When you type git status, even though you did a single merge, git will say:

# On branch master
# Your branch is ahead of 'origin/master' by 20 commits.

Not to worry. Simply do:

git reset --hard 4147391a181a05f9754ddf8eecc6c4398dbb1d17

Poof! Your local repository has reverted to that version, and your merge has disappeared.