Common Git Commands in Different Scenarios

Here is a collection of git commands that I often use depending on the scenario.
Scenario 1: I want to get a copy of a git repository.

git clone https://some-url-repository

Scenario 2: I want to see the changelog of the repository.

git log

Scenario 3: I want to get the latest version of a repository on its master branch. I don’t have any local commits.

git fetch
git reset --hard origin/master

Warning: git reset destroys your local commits!
Scenario 4: I want to commit my local changes.

git status
git add some-file.js
# See my blog about Tig about alternative to git add
git commit -m "Updated something in the file, but of course this could be more descriptive."

Scenario 4a: And then I want to push my local changes to master in the remote repository.

git push origin master

Scenario 4b: But I get an error saying I don’t have the latest remote changes.

git fetch
git rebase origin master
# Now up to date
git push origin master

Scenario 5: I want to create a new git repository.

git init
git add .
git commit -m "Initial commit"

Scenario 5a: And then push it to the remote repository.

git remote add origin https://some-url-repository.git
git remote -v
git push origin master

Scenario 6: I want to make a new branch.

git checkout -b branch_name

Scenario 7: I want to switch to another branch.

git checkout branch_name

Scenario 8: I want to push my new branch to the remote repository.

git checkout -b branch_name
# edit files, add files, commit...
git push -u origin branch_name

Scenario 9: What branch am I on again, and what other branches are there?

git branch

Scenario 10: I’ve got a commit that I want to place on top of my current head.

git cherry-pick sha_of_commit

Scenario 11: I forgot what I just did. What did I do again?

git reflog

I Dig Tig

If you use Git in the command line, then you may have encountered this tedious chore of typing out all the files you need to add into your commit:

git add directory_A/file_A.js
git add directory_A/file_B.js
git add directory_A/file_C.js
git add directory_A/file_D.js
git add directory_A/file_E.js
git add directory_B/file_A.js
git add directory_C/inner_directory_C/file_A.js

Tig is a great tool in relieving this agony. Tig is a user interface in the command line that allows you to easily add files to your commit by the click of a button.

1. Install Tig (for Mac and homebrew users):

brew install tig

For other users, see for installation instructions.

2. In the command line, bring up Tig:

tig status

3. You’ll be prompted with a screen like so:

To add a file to your commit, use the arrow key to highlight that file, then press “u” to add it. Highlight that file again and press “u” to remove it from your commit.

4. Exit Tig by pressing “q.” You’ll see the normal terminal screen again.

5. Try typing “git status.” The files you added! It’s all there!

6. Continue with your git commit like normal.

git commit -m "Some commit message here"

7. Dig Tig. ¬†ūüôā

SourceTree: A Graphical Interface for Git

Git is becoming more and more widely used by many in the workplace and at home. ¬†While Git is very command-line driven, I think one of the things I miss about SVN are the GUIs available, especially when needing to see the differences between old and new files. ¬†And while I’ve somehow gotten used to status, log, fetch, pull, push, commit, rebase, branch, checkout, cherry-pick, and whatever Git commands are out there, I still needed that extra feeling of security of seeing file changes, and git diff wasn’t enough.

That’s when I discovered SourceTree. ¬†It’s a free graphical interface application for Git. ¬†The topmost panel shows the history of your branch. ¬†The bottom left shows the files that have been changed since your last commit. ¬†The bottom right shows the differences between your working local file, highlighted on the left, and the committed version of the file. ¬†And instead of typing out git commands in your terminal window, you can use the buttons and controls provided all around this app. ¬†Personally, I have not abandoned the terminal and still do my git commands in there, but it’s always nice to be able to clearly see the diffs in a nice paneled layout before doing a commit or a push.