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 15, 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.

Q) What is the best way to split up different edits to a file into separate commits?

A) Do an interactive commit.

git add -i and then select 5, patch, selecting files and individual changes that should be staged for the commit.

Q) If it is possible to undo a merge commit, what is the best workflow?

A) Yes it is possible to undo any commit in a repository. There isn't a single "best" workflow. It all depends on context.

If the merge commit has been pushed onto a public branch (let's say ocean/develop for example) best practice is to not remove it. However branches contained in people's forks are entirely theirs. They can merge and remove as they see fit, as long as they properly communicate any changes with other developers that may be affected.

An example of a developer that may be affected would be if one person did some work (call them the base developer) that another wanted to base their work off of (call them the branch developer). If the base developer modifies their branch (i.e. rewrites history) they should inform the branch developer, so the branch developer does no try to submit a pull request with incorrect history (i.e. history the base developer doesn't want in the repository).

Q) What is the best way to incorporate new features into a given development branch that are not yet on ocean/develop?

A) Similarly with a lot of git related things, there isn't a single "best" way to do things. There are options, and which is best depends on the situation.

The "easiest" way to incorprate changes from a single other commit is to use git cherry-pick. However, you need to make sure you remove the commit later.

The "easiest" way to incorporate multiple commits is to use git merge. However, again you need to make sure you remove the commit later, but additionally subsequent commits might only work after the merge, so you might have to do trickier things like rebuilding the merge commit, or migrating your branch.

The final way to incorporate multiple commits is to use git rebase. However here, you're changing the base of your branch. So the commits you previously built / tested may need to be updated to work again.

Q) When is it best to do a merge vs. a rebase?

A) A rebase is easily the most useful tool for modifiying your own history, but both have downsides for incorporating other changes.

Merge commits can be really difficult to remove from history. However, rebasing can cause a lot of work in updating a branch.

Rebasing a branch on to it's base (i.e. git rebase -i $(git merge-base HEAD MPAS-Dev/MPAS/ocean/develop)) is incredibly useful for modifying a branches history.

Clone this wiki locally