Posts in the “git” category

How-to: Steps to create a Github pull request (git branch, etc.)

I’ve been writing books for ten years — such as the 2021 Scala Cookbook and Functional Programming, Simplified — so I haven’t had the time to work on many open source projects, and therefore, my ability to remember how to:

  • fork a Github project
  • pull it down
  • create a git branch
  • push that branch back, and then
  • submit a pull request

are weak, at best.

That being said, I’ve done it a few times lately, so I’m getting better at it. Today was a very smooth process, so I thought I’d make these notes while they’re still fresh in my mind.

Git FAQ: How to show or change your Git username or email address

Git user FAQ: How do I show or change my Git username or email address?

How to show your Git username

There are at least three ways to show your Git username:

  1. The git config command
  2. The git config --list command
  3. Looking in your Git configuration file

The following sections describe each solution.

Tell Git not to track a file any more (remove from repo)

Git “remove” FAQ: How do I tell Git not to track a file (or files) any more? (i.e., I want to remove the file from the Git repo.)

While working on an application named Sarah yesterday, I accidentally checked some files into Git that I didn’t mean to. These were primarily binary files in my project’s bin and target directories.

Because I didn’t want these files in my Git repository, I needed a way to remove them from the repository but still keep them on disk. Fortunately there’s a Git command for just this situation:

Git: How to compare two different versions of a file

Git FAQ: How do I compare two different versions of a file with git?

This solution depends on what exactly you need to do.

1) You have not run `git add`

If you just modified a file but haven’t run git add on it yet, use this:

$ git diff introduction.md

2) You have run `git add`

To see changes to a file you have run git add on, but have not committed, use this:

$ git diff --cached introduction.md

A git file diff example

In my case, I updated this introduction.md file, and then added it with git add, so I used that second command:

> git diff --cached introduction.md 
diff --git a/_overviews/overview/introduction.md b/_overviews/overview/introduction.md
index eecc4e86..2773a705 100644
--- a/_overviews/overview/introduction.md
+++ b/_overviews/overview/introduction.md
@@ -1,6 +1,9 @@
 ---
 title: Introduction
 description: This page begins the overview documentation of the Scala 3 language.
+num: 1
+previous-page: 
+next-page: scala-features
 ---

The + changes in the last three lines showed the changes I made, so this gave me confirmation that I did what I wanted.

Comparing two file versions in your Git repository

If you’re using Git and need to compare two recent versions of the same file, I can confirm that this git diff command works:

git diff HEAD^ HEAD nodeBlog.scala.html

That command compares the second-most recent version of the file (given by HEAD^) to the most recent version of the file (HEAD). In this example I want to see the detailed differences of these versions of the file named nodeBlog.scala.html. In my case, the result of this command is shown in the image.

Per the excellent book Pro Git, “HEAD^” means, “The parent of HEAD.”

As shown on this SO page, there are other ways to issue this Git command, and similar commands.

How do I Git help?

Git help FAQ: How do I Git help?

Sorry for that title, but I used to live in several southern states in the United States, and people there often pronounce "get" like "git". (I hope they'll take this joke well, lol.)

The Git help command

If you need to see the Git help text, you can start by using the "git help" command, like this:

git help

This command displays the following output:

The Git “topic branch” workflow (pattern)

In this short article, I’ll demonstrate the typical workflow for using a Git topic branch. If you’ve never heard of a topic branch, here’s a description from the excellent book, Pro Git:

“A topic branch is a short-lived branch that you create and use for a single particular feature or related work.

This is something you’ve likely never done with a VCS before because it’s generally too expensive to create and merge branches. But in Git it’s common to create, work on, merge, and delete branches several times a day.”

The basic Git topic branch workflow pattern looks like this:

create a new branch (our 'topic branch')
$ git branch bug1945

switch to the new branch
$ git checkout bug1945

do your work on the branch ...

commit your changes
$ git commit -a -m 'fixed bug 1945'

merge the changes back to the master
$ git checkout master
$ get merge bug1945

delete your topic branch
$ git branch -d bug1945

create another topic branch ...

That’s all there is to the basic Git topic branch workflow/pattern. Here’s a quick review:

  1. Create a new topic branch to work on your next feature
  2. Make your changes to the code
  3. Merge the changes back to the master
  4. Delete your branch

Related Git branch commands

Here’s a short list of Git commands related to the concept of topic branches:

create a branch and check it out in one step
$ git checkout -b bug1945

list your current branches
$ git branch

a little more information about the current branches
$ git branch -v

use the gui merge tool to see your merges
$ git mergetool

Git topic branch workflow: Summary

In summary, I hope this short tutorial on the concept of the Git topic branch workflow has been helpful.

Reporting live from Boulder, Colorado, this is Alvin Alexander.

git status message - Your branch is ahead of origin/master by X commits

git status FAQ: When I issue the "git status" command, I see the following "Your branch is ahead or origin/master ..." git message:

# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.
#
nothing to commit (working directory clean)

What does this "Your branch is ahead of 'origin/master' by X commits" message mean? I keep committing my changes with "git commit", so this message seems to be in error.

git push after git tag problem (everything up-to-date)

If you attempt to do a normal git push origin master after adding a tag, you’ll get an “Everything up-to-date” message from Git. In short, this is because you have to push a tag to the origin just like you push a branch.

In my case I just created a tag named v0.1, so I pushed it like this:

git push origin v0.1

The output from the git push command looks like this:

Git: How do I add an empty directory to a Git project/repository?

Git empty directories FAQ: How do I add an empty directory to a Git repository?

Short answer - you can't. (See below for the workaround.) The design of the Git staging area only accounts for files, as described in the Git FAQ, and other books like Pro Git.

Git empty directories FAQ

Here's the text from the Git FAQ section, "Can I add empty directories to a Git repository":

Git export: How to export a Git project

Git export FAQ: How do I export a Git project, like I would do with a "cvs export" or "svn export"?

There is no "git export" command, so instead you use the "git archive" command. By default, "git archive" produces its output in a tar format, so all you have to do is pipe that output into gzip or bzip2 or other.

Git export example

Here's a simple Git export command I just ran. I moved into the root of my Git project directory, then ran this command to create a new file named "latest.tgz":

Private Git repository hosting services

Private Git hosting services FAQ: What companies offer Git hosting, in particular private Git hosting services?

I recently started looking for a private Git hosting service, and the obvious first place to look is GitHub. They provide free Git hosting for open source projects, and their service has been excellent. But when I looked at their private Git hosting service, I was really surprised by the cost of their plans. Their lowest price private Git hosting plan is $7/month, and that allows only five Git projects, and relatively little disk space. Since I want a private Git hosting service to store all my projects, I'd immediately need to go to one of their paid Git hosting plans, and their Git hosting prices go up quickly from there.

Update: Github has changed their policies significantly since I first wrote this article.

Git shortcuts/aliases - How to create

Git shortcuts FAQ: Can I create Git shortcuts (aliases) so I don't have to type out full Git commands like "git commit..."?

I've been reading the Pro Git book a lot lately, and the short answer is yes, you can create Git shortcuts so you don't have to type out the long Git commands. Here are several Git shortcut commands (referred to as "git aliases") from the Pro Git book: