Month: March 2017

How to write a Bash or PowerShell script to quickly create test repos – 086

TFW you learn something that saves you a ton of time.

Our GitHub services team has on-demand content for learning Git, including a Git out of Trouble section. On page 2 git-set-up they show you how to create a script to create several files, commit them to your repo, one file at a time.

I’m using Git Bash, so here’s the Bash script

for d in {1..6};
 do touch file$d.md;
 git add file$d.md;
 git commit -m "adding file $d";
 done

First, you’ll want to do `git init` otherwise, you’ll get yelled at by your shell for not being in a git repository.

Just copy and paste that into your shell (terminal or whatever),

script pasted into a Git Bash command prompt

Press Enter,

 git messages shown as script is run

and enjoy your Git out of Trouble explorations!

git status showing a commit for each file

How to revert changes in Visual Studio – 085

Yesterday’s tip talked about how to use `git revert` from the command line. Today’s tip describes the functional equivalent in Visual Studio.

TBH I clicked the wrong command in VS initially when I started writing this post. I clicked “reset ” then the “–hard” option, which should have been a hint. Revert doesn’t have the soft, mixed, or hard options. But, I was able to fix my git history (by going to the command line) to write this tip without deleting my .git folder and starting over, which is a first for me. So perhaps all this work writing out these tips is working!

Let’s say you have a console application that shows a blue background. And you’re like “no.”

console app with blue background

You want to remove this commit altogether. If you’ve been committing in small, atomic chunks of code, you should be able to revert this background color change. But don’t take my word for it. I wouldn’t know because I’m still trying to train myself to do small commits.

Going to the history shows where this change was introduced, i.e. “added ChangeColors()”. You can revert this commit by right-click and selecting “Revert.”

History - Revert on selected commit

Click Yes on the confirmation prompt.

If you refresh History, you’ll see the new commit.

Revert "added ChangeColors()"

You can double-click to open that commit’s details. If you edit the commit message, the Amend Message option will become available.

reverted commits details

Double-clicking on the Program.cs file listed under Changes for the Commit Details pane shows what’s been removed or “reverted” from the codebase.

Changes for Program.cs showing the ChangeColors method removed

And to verify we are back to our familiar console application background, let’s run the project.

default black background for console app

How to use git revert to undo a previous commit – 084

“undo”, “reset”, “revert” – ah English! Perhaps the author of The Giver used Git in the past, since one of their rules was “use precision of language.”

What do we mean by “undo a previous commit?” Let’s say we had 3 commits A, B, and C. And we realize ‘B’ was a mistake. Perhaps we changed a color to red, and it should go back to white. But we don’t want to pretend that ‘B’ never happened. That would mean modifying Git history, and Git really hates changing history if you have the repo shared with others.

So yeah, you could rewrite the code to manually undo the changes introduced in ‘B’, or you could let Git do that for you by running `git revert <commitID>`

git revert <commitID>

As you see in the above graph, using this git visualization tool, a new commit is made with the changes introduced from ‘A’ removed. The take home message is that a new commit is made, instead of “commit A” vanishing.

I like this Stack Overflow answer:

If I found B I committed before is wrong, and I want to “undo” its change, git-revert-ing B will cause:

A <- B <- C <- B'
               ^ HEAD

for which B' is reversing the change done in B

Now to the command line to see this in action!

Say you’ve added three files: A.txt, B.txt, and C.txt for commits A, B, and C, respectively. (One day, I’m going to blog about a list of files being out of order, “dis-respectively “)

We want to “undo” the changes introduced in B.

git log showing commits A, B, C

—STOP!!—

If you are following along at home, let’s pause for a second and make sure your core.editor is configured to use your preferred text editor.

The read between the lines: you’ll want to know how to exit whatever editor comes up.

git config --global core.editor showing notepad

Right now I’m using notepad to give me a fighting chance instead of the vi editor, aka the destoryer of CS careers. YMMV, so check out this GitHub help article on setting up an preferred text editor. Hint: it’s `git config –global core.editor notepad` if you want to use notepad across all your repos.

Okay with that taken care of, let’s do the revert. We want to “undo” the B.txt commit, so let’s party w `git revert <commit id for B>` and suddenly a wild notepad appears!

image

Since I’m cool with the default commit message “Revert ‘B’ for this demo (remember, git revert creates a new commit so it has to get a commit message from you), simply close notepad (File – Exit or ‘x’).

Now if you do your git log –oneline, you’ll notice the new commit. If you do a `ls` or `dir` and the git-equivalent to see what files git is tracking `git ls-files`, you’ll see that B.txt is no where to be found.

git log, ls, and git ls-files not showing B.txt

Once again to recap what we did. We could have simply deleted the file B.txt and committed those changes. But for a more complex scenario where you need to remove a feature that consists of more than a single file or line change, you should consider using `git revert`.

How to use the VS status bar buttons as a shortcut to Team Explorer panes – 083

Something new in Visual Studio 2015 and still there in Visual Studio 2017 is the ability to click buttons(!!!) in the status bar O_O.

You can switch branches without having to touch the Team Explorer pane.

branches button shown in status bar w list of branches to checkout

You can jump to the Connect pane, which will save you time when you’re debating “is it the Home icon or the green Plug icon?”

repo button in status bar showing Connect pane

Yeah, I guess I need to get around to deleting deletemetoday one of these days…

You can jump to the Changes pane, regardless whether you have uncommitted changes (or staged changes as shown in my screenshot – yeah SDET skills die hard).

This button won’t automatically commit your changes, but only takes you to the pane.

pen or pencil icon button for Changes pane

And last but not least, you can jump to the Sync pane. Again, it doesn’t perform any commands other than just navigating to the pane.

up arrow button for Sync pane

Looks like you can click the Line, Column, and Character status bar “buttons” and new UI appears for Visual Studio 2017!! (provided you have a file opened).

Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips
Must focus on Git and GitHub tips

Okay, it looks like if you double-click on any of those status bar buttons, you’ll get a new Go To Line dialog, which seems to be part of a global search window…

new Go to Line window in VIsual Studio 2017

but it doesn’t seem to let you specify columns or character positions.

Yeah, old habits die hard, even when trying to focus.