Git instructions

From Dmz-portal

Jump to: navigation, search


Basic Git help

Useful Git links

Download a new repository

For example LLVM:

 git clone

Switching branches

Git has the ability to multiple different branches both locally and on the server. When you initially clone a repository, you get the master branch from the server. All branches you will use are local to your machine, but can track a branch on the server. To switch from one branch to another, use the command

 git checkout working

where working is the branch you're switching to.

Switch to a branch that is on the server that you haven't used

If the branch is on the server, but you haven't checked it out yet, you'll want to check it out as with the same name as the server and have your local branch track the server branch.

 git checkout -t working

This will create a local branch called working that will track the server branch. Now to switch between this branch and master you just use the command:

 git checkout master

and back

 git checkout working

Switch to a new branch that isn't on the server

If you're about to work on something new, but you want the rest of the group to have to merge your new changes, create a new local branch. Later, you can either merge this branch into a server branch or push the branch onto the server. When the branch doesn't exist yet either locally or on the server, execute the command:

 git checkout -b Reed

where Reed is the name of the new local branch. It will be branched off of where you were working at the time of the command. To push the branch onto the server:

 git push origin Reed

where origin is a remote repository pointing at the server. (origin is the default remote repository when you do a git clone). And, Reed is the name of the branch.

To get all your changes onto the server

 git push --all

Track a newly created branch

You've just created a branch that you just pushed to the server. At this point anyone who clones the repository will see the new branch. However, if you try to pull down new changes in your current repository, you'll get a strange error message that starts "You asked me to pull without telling me which branch you want to merge with...". To fix this execute these two commands changing <BRANCH> to the name of your branch:

 git config --add branch.<BRANCH>.remote origin
 git config --add branch.<BRANCH>.merge refs/heads/<BRANCH>

Note that <BRANCH> should be changed three times and the dots (.) and slashes (/) around it are important. After doing this, your git pull should work normally.

To add a new file or commit changes to the repository

 git add file
 git commit

Rename a branch

If you have a branch you're currently working on that has the wrong name, use the following command to change it's name:

 git branch -m oldbranch newbranch

Delete a branch

If you have a branch you're done with and you want to delete locally and on the server, use the following command to delete in your local repository:

 git branch -d branchname

If you get an error, then you probably hadn't used the branch locally in your repository. To delete it on the server use the command:

 git push origin --delete branchname

Fixing problems

Fixing the "error: failed to push some refs to" problem

The following set of commands will fix the "error: failed to push some refs to" that you get when you do a git push. It will fix the problem and put you back in the same state you were in when you started. So, the next git pull should not give you this error. It assumes you're working with files that aren't part of the repository and other that are part of the repository, but aren't checked in.

Note the state and branch you're in before this starts. You may have untracked or modified files which this assumes you'll want back.

Add all the files to your repository. This will allow you to save your untracked or modified files. When you're done, the untracked and modified files will still be untracked or modified.

This saves your state and returns the current branch back to HEAD. Since you added the untracked and modified files to the repository, these will be stored in the stash.

Master is the branch that is not getting a fast forward merge. This will switch you to that branch.

This will merge your local master with what is on the server. It will generate a lot of output when it fast forward merges the branch. It shouldn't have any conflicts unless you're checking files into the master branch. At this point, the error is fixed. But, you still need to get back to where you were.

This will switch you back to the branch you were on. It assumes you were on the assembler branch. The initial git status command will have told you what branch you were on. If it wasn't assembler, then change this command to the branch you were on.

This will restore any edited or untracked files that you had in your directory before you started. This is the reverse of the git stash command above.

This will stop tracking any files that you got tracked from the earlier git add command. You should now be back to where you were when this started. Doing a git push will no longer generate the error.

This should show the same state you were in when you ran the git status command. You'll be in the same branch. You'll have the same set of untracked files. You'll have the same set of edited files.

Block commits to master branch

Several of the GIT repositories on the dmz-portal machine regularly sync their master branch with another remote upstream repository. Commits to this branch can cause a lot of problems for the rest of the team. The following script if added to your repository's .git/hooks/ directory and called pre-commit hook will stop you from committing changes to the master branch. Whenever you clone one of these repositories, copy this script into .git/hooks/ and call it pre-commit.

 # An example hook script to verify what is about to be committed.
 # Called by git-commit with no arguments.  The hook should
 # exit with non-zero status after issuing an appropriate message if
 # it wants to stop the commit.
 # To enable this hook, rename this file to "pre-commit".
 if git-rev-parse --verify HEAD >/dev/null 2>&1
         # Initial commit: diff against an empty tree object
 # If you want to allow non-ascii filenames set this variable to true.
 allownonascii=$(git config hooks.allownonascii)
 # Cross platform projects tend to avoid non-ascii filenames; prevent
 # them from being added to the repository. We exploit the fact that the
 # printable range starts at the space character and ends with tilde.
 if [ "$allownonascii" != "true" ] &&
         # Note that the use of brackets around a tr range is ok here, (it's
         # even required, for portability to Solaris 10's /usr/bin/tr), since
         # the square bracket bytes happen to fall in the designated range.
         test "$(git diff --cached --name-only --diff-filter=A -z $against |
           LC_ALL=C tr -d '[ -~]\0')"
         echo "Error: Attempt to add a non-ascii file name."
         echo "This can cause problems if you want to work"
         echo "with people on other platforms."
         echo "To be portable it is advisable to rename the file ..."
         echo "If you know what you are doing you can disable this"
         echo "check using:"
         echo "  git config hooks.allownonascii true"
         exit 1
 branchname=`git branch | grep '^*' | cut -d' ' -f2`
 if [ "$branchname" == "master" ]; then
   echo "cannot commit to master branch."
   exit 1
 exec git diff-index --check --cached $against --
Personal tools