• Home
  • Blog
  • About Me

Box 1663

Passes must be presented.

  • Developer
  • Transition to PC

A Brief Intro to Command Line Git

August 11, 2017 By Scott Leave a Comment

Why command line Git?

There are several GUI tools, such as Tower and Github Desktop available for Git and it has basic support in Xcode, Visual Studio, and Android Studio, so why would you want to use Git on the command line? Git is very powerful and the more you understand it, the easier it is to use. Additionally, if you ever find yourself working on code in terminal or remotely via ssh, it might be your only option. A basic understanding of command line Git doesn’t preclude you from using GUI tools, but it just might help you out of a sticky situation down the line. Also, some of the GUI’s are open source and free, but many are on the expensive side. Command line Git is always free.

This guide assumes you are using terminal on Linux or macOS or the Git Bash prompt on Windows.

Git Setup

Download Git for your platform from https://git-scm.com/ and install.

Do this in the terminal before anything else!

Set up your username with git config --global user.name = "First Last"

Set up your email with git config --global user.email = "user@example.com"

.gitignore

A .gitignore file allows you to filter files that you don’t want added to your repository. Some resources to create a .gitignore file are:

  • http://www.gitignore.io/
  • https://github.com/github/gitignore

Place the .gitignore file in the root directory of your Git repository. It is the same directory that you type git init and has the hidden .git directory. If you type ls -al you will see hidden directories in the directory list.

Basic Commands

git init : Start a new Git repository

git status : Get the status of your Git repository (Run this often)

git add <filename> : Add a file to version control

git commit -m "<message>" : Commit using message

git add '*.txt' : Add all .txt files in the directory

git log : See a log of all commits

git remote add origin https://github.com/<username>/<project_name>.git : Add a remote repository

git push -u origin master : Push the local commits to the remote repository.

git pull origin master : Pull remote changes to local repository

git diff HEAD : See what has changed since the last commit

git diff --staged : See the git differences that are staged (ready to commit)

git reset <filename> : Remove a file from staged

git checkout -- <filename>: Get rid of all changes for since last commit

git branch <branchname> : Create a branch called

git checkout <branchname> : Switch to branch called

Branch and checkout can be combined with git checkout -b <branchname>

git rm <filename> : remove the file AND stage the removal

Once all changes are made in your branch and you are satisfied, merge the changes back into the master.

git checkout master git merge <branchname>

If you want to delete a branch, you can type git branch -d <branchname>.

Don’t forget to push your code. When you are on master, type git push.

A Very Basic Git Workflow Using Existing Code

  1. Change directory to the root of your source code
  2. Type git init to create the repository
  3. Type git status to get the status of the repository. You should see a listing of Untracked files
  4. Create a .gitignore file. Here’s a really basic one for macOS from https://www.gitignore.io:
    # Created by https://www.gitignore.io/api/macos
    
    ### macOS ###
    *.DS_Store
    .AppleDouble
    .LSOverride
    
    # Icon must end with two \r
    Icon
    
    # Thumbnails
    ._*
    
    # Files that might appear in the root of a volume
    .DocumentRevisions-V100
    .fseventsd
    .Spotlight-V100
    .TemporaryItems
    .Trashes
    .VolumeIcon.icns
    .com.apple.timemachine.donotpresent
    
    # Directories potentially created on remote AFP share
    .AppleDB
    .AppleDesktop
    Network Trash Folder
    Temporary Items
    .apdisk
    
    # End of https://www.gitignore.io/api/macos
    
  5. Add all files (excluding those from .gitignore) with git add .
  6. Commit the files with git commit -m "Initial Commit". The -m tells git to use the following string as the commit message. If you omit this, you will get a vim screen prompting you for a commit message. Your commit will not be successful if you do not save the commit message in vim. To do that, type i to enter insertion mode. Type your commit message at the top of the file (do not worry about the rest of the message). Once you are done with the message, hit esc once to exit insertion mode. To save and quit, type :wq (write and quit). If you want to simply exit without committing, type :q!. Vim will exit without saving and git will abort the commit.
  7. Type ‘git status’ once again and verify that you see “nothing to commit, working tree clean”.
  8. It is best to work on branches and then merge them with the master branch after you are satisfied with your changes. That way, if all else fails, you can revert back to your last commit where the code was working. To create a new branch, type git checkout -b "new-feature". You should see “Switched to a new branch ‘new-feature'”.
  9. Make some changes to your source code and then type git status. You should see the file(s) you worked on now say “modified”.
  10. Either add the files individually with git add <filename> or add all the modified files at once with git add .
  11. Commit the files with git commit -m "updated files" (your commit messages should be more descriptive that this, but it’s just an example). Now you can type git status to verify the commit happened. You can also type git log to see the history of your commits with the most recent commit appearing at the top of the list.
  12. At this point, you can switch back to the master branch and see that your code changes in your new branch aren’t reflected there. Type git checkout master and then take a look at one of the files you changed. Don’t worry, you haven’t lost anything. Switch back to your new-feature branch using git checkout new-feature and you’ll see your changes are back.
  13. To merge the new-feature branch with your master branch, switch to master with git checkout master and merge the new-feature branch with git merge new-feature. Now all the changes you made in your new-featurebranch are reflected in your master branch.
  14. Repeat this step as you go along.

Stash

Sometimes you might accidentally make some changes, say to the master branch, before checking out a new branch and you get “error: Your local changes to the following files would be overwritten by checkout:” when you try to switch to another branch or create a new one. You have a few options if this happens.

  1. Commit the files using add and commit. You can take a shortcut and quickly commit all the changes in one command with git commit -am "Updated files" or do it in two steps like we did previously. Once the files are committed, you can checkout a new branch.
  2. You can delete all the changes using git reset HEAD. This will throw away any changes that you have made since the last commit. Use this with caution. It will not ask if you are sure.
  3. Use stash. Stash is a temporary stack that you can store changes on and then apply them later after you switch branches. To use stash do the following:
    • Type git stash
    • Type git checkout new-feature
    • Type git stash pop to apply the stashed changes to the new branch.

Diff

If you want to see what has changed between your current branch and the master (or any other branch for that matter), you can type git diff master. This will produce a diff file, which is a standard way of viewing insertions and deletions. If the file is long and you want to use a text editor to browse it, just type git diff master > changes.txt. You can use any filename you want instead of changes.txt, but the diff will be saved to a file that you can then open in another text editor to view.

What about Github, Bitbucket, or Gitlab?

Each of these services provide a remote repository where you can store your code in the cloud and retrieve it later or share it with other people. Functionally, each works the same for git and uses standard Git commands. The main difference is the pricing structure and how you plan to use them, but each has a free option for personal use.

Rather than re-invent the wheel, I encourage you to visit their websites and explore their documentation. Each has excellent guides on how to set your project up on their server securely.

Github

Sign up at https://www.github.com/ and then visit https://guides.github.com/activities/hello-world/

They also have an excellent Git Cheat Sheet that you can download and print for quick reference.

Bitbucket

Sign up at https://bitbucket.org/ and then visit https://www.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud

You can also download and print Bitbucket’s Git Cheat Sheet

Gitlab

Gitlab can be found at https://about.gitlab.com/

If you decide you want to host your own git server, check out their Community Edition Omnibus Package

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

Filed Under: Developer, Featured

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Set up Virtualenv and Virtualenvwrapper on Ubuntu 18.04

July 10, 2019 By Scott Leave a Comment

Metasploitable 3 Without Vagrant

September 7, 2018 By Scott Leave a Comment

A Brief Intro to Command Line Git

August 11, 2017 By Scott Leave a Comment

Copyright © 2025 · Box 1663 · Log in