-
Notifications
You must be signed in to change notification settings - Fork 0
LANL Git FAQ
This page will be used to house the LANL Git FAQ. Please post questions here, and eventually they will be answered.
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.
Instead, you switch branches in a local repository.
To switch branches in your local repository, you can:
git checkout <branch_name>
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.
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:
- Add a remote for the fork:
git remote add ...
- 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:
- Detached HEAD:
git checkout <remote_name>/<branch_name>
(e.g.git checkout MPAS-Dev/MPAS/ocean/develop
will checkoutocean/develop
fromMPAS-Dev/MPAS
) - 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>
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:
- Rebasing your branch off of the commits you want to incorprate into your branch
- 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.
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.
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.