Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

LANL Git FAQ

Douglas Jacobsen edited this page Oct 14, 2015 · 11 revisions

This page will be used to house the LANL Git FAQ. Please post questions here, and eventually they will be answered.

Q) How do I create a branch on my fork?

A) To create a branch on your fork, you first need a local branch.

To create a local branch, you can use any of: git branch <new_branch_name> git branch <new_branch_name> <branch_point> git checkout -b <new_branch_name> git checkout -b <new_branch_name> <branch_point>

When <branch_point> is omitted from the command, it is replaced with HEAD.

After a local branch is created, you can push it to your fork to create a branch on your fork. This is done using:

git push <remote_name> <new_branch_name>

Where <remote_name> is one of the aliases in the left most column of git remote -v and <new_branch_name> is the name of the branch you just created.

Q) How do I switch branches on my fork?

A) Switching branches is not something that happens on a fork.

Instead, you switch branches in a local repository.

To switch branches in your local repository, you can:

git checkout <branch_name>

Q) How do I find out someone's alias on github and check out their branch?

A) The alias used for a remote is something you choose.

To figure out which remote aliases you have created, and what they point to you can use:

git remote -v

The left column is the alias name, while the right column is the URL for where the alias points to.

Q) How do I check out a particular branch from a particular fork?

A) The answer depends on the use case:

For example, if you only want to test a branch, or see the code but not commit to it, it is sometimes easiest to work in what is called a detached HEAD.

To 'checkout' a version of code in a detached head from someones fork you first need to:

  1. Add a remote for the fork: git remote add ...
  2. Fetch the remote history: git fetch ...

After you have fetched the history of the fork, you can checkout their branch in one of two ways:

  1. Detached HEAD: git checkout <remote_name>/<branch_name> (e.g. git checkout MPAS-Dev/MPAS/ocean/develop will checkout ocean/develop from MPAS-Dev/MPAS)
  2. Local branch: git checkout -b <new_branch_name> <remote_name>/<branch_name> (e.g. git checkout -b ocean/feature MPAS-Dev/MPAS/ocean/develop will make a new local branch named ocean/feature off of MPAS-Dev/MPAS/ocean/develop and check it out.

If you already have a local branch, you can simply check it out using:

git checkout <branch_name>

Q) How do I pull changes from an external branch on someone else's fork into my branch?

A) This also kind of depends on the use case.

If you want to pull in a specific commit, you can use git cherry-pick to pull it onto your branch.

If you want to pull in a set of commits, there are two options:

  1. Rebasing your branch off of the commits you want to incorprate into your branch
  2. Merging the other branch into your branch

However, often times when you bring in changes someone else made into your branch, you'll need to remove them at a later point in time. So, keep this in mind before deciding if it's work doing in the first place.

Q) How do I roll back a file to a previous commit?

A) If what you want is the exact version of a specific file in a different commit you can:

git checkout <commit> -- <file_list>

Where <file_list> can be a list of files you want to get from the other commit. NOTE This will throw away the versions you currently have of these files. So any changes will be lost forever.

Q) How do I roll back an entire branch to a previous commit?

A) To change a branch to point to a previous commit you can:

git reset --hard <new_commit>

NOTE: This changes the branch you are on (i.e. what git branch reports you as being on) to point to <new_commit> rather than whatever it points to now. Additionally, it will change all the files in your current directory to be this version as well, so any local changes will be lost.

If you want to change a branch that you are not currently on (i.e. it is not checked out) you can:

git branch -f <branch_name> <new_commit>

Which will move the branch <branch_name> to point to <new_commit> but not change anything in your local directory.

Clone this wiki locally