A Git CLI Reference for Beginners

Hopefully, no one needs to sell you on GitHub – the world's largest open source community. GitHub is home for most developers – a fast flexible social environment to build personal projects, support enterprises and collaborate on open source technologies.

The underpinnings of GitHub is Git – a free, open source, cross-platform and highly productive distributed version control system. GitHub conveniently wraps all of Git's features into polished UI tools for your chosen development platform, namely:

But you are a geek. And what appeals more to your inner nerdiness than pure text on a bland terminal window. Nothing like passer-by's not having a clue as to what you are up to! Everything you do through the GitHub UI tools, first began life as command line tools via the the Git CLI. And it is incredibly powerful.

This article doesn't have screenshots and memes. Instead it aims to be a straight up cheat sheet of Git CLI commands. The best news is that all of the commands work the exact same way on Linux, Mac OS/OSX and Windows.

Command Prompt Basics

First, let's get a basic command line refresher under our belt – most commands work consistently across the Bash and DOS command prompts.

  • ls

    List Directory contents – provides a list of all files/folders in given working directory.

  • cd <DirectoryName>

    Change Directory – navigates to given folder as working directory. cd / navigates to root folder and cd .. traverses one level up to the parent directory of the current working directory.

  • mkdir <DirectoryName>

    Make Directory – creates a new folder in place.

  • open -a "TextEdit" .bash_profile

    Open the bash_profile file in TextEdit on a Mac. If you're on Linux/OSX systems, the bash_profile is the user's personal initialization file, executed by login shells as the terminal command prompt starts up.

  • PS1="\u @ \t: 🤘 "

    Also on Linux/OSX, the Bash shell command prompt can be customized using PS1-4 Prompt Statements – essentially environment variables. You could add PS variables in your bash_profile to customize the command prompt. Above is mine with an Emoji in my command prompt – because why not!

  • clear

    Clears terminal window of past commands and outputs – provides fresh command prompt.

Git Commands

Now let's dig into the Git CLI. This is not an exhaustive list, but focuses on the most frequently used commands.

  • git config --global user.name "<UserName>"

    Sets the name you want attached to your commit transactions.

  • git config --global user.email "<UserEmail>"

    Sets the email you want attached to your commit transactions.

  • git init <ProjectName>

    Initializes a new Git repository. Transforms a regular folder into a directory that can accept Git commands.

  • git clone <RemoteGitHubURL>

    Copies down a remote GitHub repository along with all the version history to a local working directory. Maps the directory for further commands.

  • git status

    Checks the status of a given repository. Lists th working branch and any new/changed files that need to be committed.

  • git diff

    Shows the file differences not yet staged.

  • git add <FileName>

    Brings new files in your repository to Git's attention for tracking. Includes the file in the repository snapshot for versioning.

  • git add .

    Adds a batch of files to Git's tracking in a local working directory. Adds everything in one swoop. This command could also add files with a specific extension or other filters.

  • git reset <FileName>

    Unstages the named file from the repository snapshot, but preserves its contents.

  • git branch

    Shows all the local branches in the current repository.

  • git branch <BranchName>

    Creates a new branch in the current repository.

  • git checkout master/<BranchName>

    A navigational command that switches the working directory to master/named branch. The files are representative of the state of master/branch.

  • git commit -m "<CommitMessage>"

    Commit changes that you have made in your working directory with a descriptive message.

  • git merge <BranchName>

    Merge changes made in a given branch to the master branch. Makes updates visible to all repository collaborators.

  • git branch -d <BranchName>

    Deletes a branch after changes have been merged with master branch.

  • git remote add origin <RemoteURL>

    Introduces local Git to remote repository (typically GitHub). Adds hooks for pull/push for syncing with remote source.

  • git push --set-upstream origin master

    Pushes local repository changes to a remote master branch in a linked repository (again, probably GitHub). Syncs remote with local.

  • git pull origin master

    Pulls down a master/named branch from a linked remote repository to a local working directory. Syncs local with remote.

  • git help

    Fogot something? Pull up the Git CLI Help to look up commands with the option to dig further into each command.

Pro Tips

As you collaborate more and more on GitHub projects, a few tips may be helpful:

  1. Delete branches after the corresponding pull request has been merged onto master. Orphaned branches only cause confusion.

  2. Fork a project, branch off and make a pull request sooner rather than later. This announces your intent to work on something to the rest of the collaborators. A pull request does not have to wait until all the work has been finalized before merging it onto master.

  3. Don't keep working for too long on your own local branch. Things can get way out of sync quickly.

  4. GitHub does not magically solve merge conflicts between branches. Diff tools are your friend.

  5. A project being on GitHub does not automatically become open source. GitHub repositories need to pick a valid license to be truely OSS – make your pick as you're setting up your repository.

  6. Please play nice with others and have meaningful ReadMe files and descriptive commit messages. Emojis are ok.

Telerik Open Source on GitHub

We at Telerik, love open source and love GitHub. You'll find some very popular GitHub repositories maintained by Telerik – and we are happy to share. So jump in, clone it, use it, fork it and contribute back.

  1. Kendo UI Corehttps://github.com/telerik/kendo-ui-core
  2. NativeScripthttps://github.com/NativeScript/NativeScript
  3. VS Code Extension for NativeScripthttps://github.com/NativeScript/nativescript-vscode-extension
  4. JustCode Extensionshttps://github.com/telerik/justcode-extensions
  5. JustDecompile Enginehttps://github.com/telerik/JustDecompileEngine

Conclusion

A cheat sheet of commands does not need a conclusion, right? So keep that terminal window open, reference this list and get productive on your GitHub projects. Happy coding!

Related Resources

Comments