Git Branch and pushing local branches to Github

I’ve been getting better at using Git in smart ways, but recently I did something a bit dumb. Instead of branching my code to port over to a different chip, I just make a copy of it into a directory not under version control. The issue I have now is how to add it to the repository as a separate branch. Since I’m not too far away from where I started, the answer is simple:

I’m going to create a new branch and replace the controlled files with the ones I previously edited. This is done with the following steps:

  1. Clone a clean copy of the repository (if you don’t already have one)
  2. Create the branch
  3. Switch over to the branch (you’ll still be on ‘master’ after creating it)
  4. Make the edits (or just copy the files over in my case)
  5. Commit
  6. Explicitly push the local branch back to remote (if you’re using a remote repository like Github)
git clone git://RepositoryAddressGoesHere
git branch newBranchName
git checkout newBranchName
nano fileToBeModified  #Modify the files any way you wish here
git commit -a -m "Commit Message"
git push origin newBranchName

That’s it. I made a new branch, altered a file, and commited it to that branch, then pushed the change to the remote repository. There’s a lot more about working with branches at the Git Book.

Git Rebase

It was pointed out to me that I had a typo in my code for the SPI shift register tutorial. I had the definition wrong for the data pin used by SPI (I guess AVR is not too particular about setting MOSI as an output for master SPI mode). Since it is supposed to be a clear example I needed to fix it. But the git repository that I used has a tag to some earlier code as the simple example, then more complex code at the latest revision. I wanted to make the change throughout and for this I decided to use the Rebase command. Here’s the steps I took:

  • Check out a new clone of the repository
  • Do a hard reset to the offending commit
  • Fix the problem and commit it
  • Hard reset back to the most recent version
  • Use the Rebase command to bring that change forward in time
  • Pull from the remote repository (you’ll be told to do this if you try to push first)
  • Push back to the remote repository

Fixing tags

  • You CANNOT move a tag on the remote repository. This is a security feature.
  • Delete the old tag and make a new one at the proper commit.
Take a look at the actual commands after the break.