2 Mar 2016

Git & Bitbucket Notes

Notes on setting up Bitbucket for the first time. I'm switching from Springloops to Bitbucket because they support more private repositories with the free account.

There's a good overview of using the Git commands here.

Existing Git Repository

To push an existing Git repository to a new Bitbucket repository first create the new repository from the Bitbucket web page. An existing repo will have a .git directory

Follow the instructions to push an existing repo:

cd /path/to/my/repo 
git remote add origin https://<userid>@bitbucket.org/<userid>/<reponame>.git
git push -u origin --all # pushes up the repo and its refs for the first time 
git push -u origin --tags # pushes up any tags

In my case, my repo was pointing to an old service and I had to remove the "orgin" entry from the .git/config file.

Setup New Git Repository

There's a good description here.

cd /path/to/repo
git init
git add .
git commit -m "Initial commit"
git remote add origin https://<userid>@bitbucket.org/<userid>/<reponame>.git
git pull origin master

Cloning an Existing Repository and Staying in Sync

These instructions come from this post. How to clone a repository while keeping up to date with changes in that repository. I'm going to fork a project from kenrogers using the GitHub interface and work with that fork:

git clone https://github.com/rgstephens/impossiblelist
cd impossiblelist
git remote add --track master upstream git://github.com/kenrogers/impossiblelist
git fetch upstream
git merge upstream/master

Now you're ready to create a branch and start updating

Creating a Branch and Updating

Atlassian has a good workflow tutorial here. Switch to a branch off of master and create it if it doesn't exist:

git checkout -b some-feature master

Make changes to your project. Check on the git's monitoring of your changes:

git status
git diff

When you're ready to do a commit, you first have to stage your changes:

git add -A  # stages all changes
git commit -m "comments on changes"
git push origin some-feature

Now, merge this into the master branch. If others have been working on the master branch, first pull it to make sure you're up to date:

git pull origin master

Now, put yourself on the master branch, merge your changes and push them back to the server and delete the branch:

git checkout master
git merge some-feature
git push origin master
git branch -d some-feature  # delete branch

Oops, Made Changes Without a Branch

If you started making changes without creating a new branch, there's a good post here but the short answer is to just create the new branch with the checkout -b option:

git checkout -b newbranch

Oops, Need to Remove File From Commit

Show the details of the commit:

git log # note the reference number
git diff-tree --no-commit-id --name-only -r <ref-number>

Now, temporarily remove the file from the commit and update the commit:

git rm --cached <filename>
git commit --amend