Last update on .

Git is an open source version control system which allows you to create a history of changes that you can refer to when needed.

The full list of commands that Git uses can be confusing and maybe a bit overwhelming when you are just starting to use it. This articles lists some of the most commonly used commands with basic examples.

git add - tells Git which files to track by adding a file to the index of files to be committed.

    # Add all current changes to the next commit
    $ git add .

    # Add only the changes in the specified file
    $ git add -p filename

git branch – shows a list of the available branches to choose from as well as which one is current.

git branch -d branch name - Deletes a branch from the repository. It is best practice to clean up old branches that are no longer being used.

    $ git branch -d styling

git checkout id - allows you to go back to a previous commit (or branch). The format is git checkout id where id is either the commit id or the branch name.

git clone URL - Clone or copy an existing repository from the URL specified. This also clones the remotes that are configured so you will need to update those using git remote.

git commit -m message - stores the current contents of the designated items in a new commit along with a log message from the user describing the changes; Traditional syntax is to use the present tense for messages ('add' file, not 'added file').

    $ git commit -m 'Update README.md file.'

git diff - shows the changes to tracked files.

git fetch remote - Downloads changes from a remote without integrating them into the HEAD.

git init - Creates a hidden directory that tracks all of our changes in the directory (.git/) that it is created in (or reinitializes an existing one) in your working directory. It also creates the master branch of your local repository.

git help command - Displays help information on Git commands.

git log - Shows a list of all commits (from newest to oldest) that have occurred showing the following items associated with each commit:

  • the commit identifier
  • the author
  • the date and time
  • the commit message
    kc@LAPTOP MINGW64 ~/projects/myproject (master)
    $ git log
    commit 896b7b88b292ff62934a3a4554d9b8f95881502a (HEAD -> master)
    Author: Username 
    Date:   Sun May 13 15:41:34 2018 -0500

    Update .gitignore

    commit d6c34073d5e64dce7f35b8dbb87088ee352e67c0 (github/master)
    Author: Username 
    Date:   Sun May 13 15:34:31 2018 -0500

    First commit

git merge branchname – Joins another branch into the current branch.

    # Merges the 'style' branch into the 'master' branch
    ~/projects/myproject (master)
    $ git merge style

git pull remote branch - Download changes and directly merge/integrate into the current HEAD on your local machine.

git push remote branch - Publish local changes to a remote. If no branch is specified, git push defaults to the 'master' branch.

git remote -v – Lists the remote repositories that are configured in this repo.

    kc@LAPTOP MINGW64 ~/projects/myproject (master)
    $ git remote -v
    github  https://github.com/username/myproject.git (fetch)
    github  https://github.com/username/myproject.git (push)

git remote add remotename URL - Adds a new remote with the remote URL (option A). If you need to specify the user name and password, use Option B (below).

Option A git remote add origin https://github.com/username/myproject.git
Option B git remote add kudunet http://username:password@kudu.net-demo.com/net-demo.git

git remote remove remotename - Deletes the reference to an existing remote.

git remote set-url origin URL from github - Change the existing remote named ‘origin’ to a new URL. (Use this when cloning someone else’s repository so that it points to your own.)

git rm [filename] – Removes files from the working directory and stages the deletion.

git rm --cached [filename] – Removes the file from version control, but preserves the file locally.

git reset – Depending on the options that you choose, git reset can remove files that were added to the staging area OR it can be used to remove previous commits or even merges. Use caution with this command.

git stash – Records the current state of the working directory and the index, but reverts back to a clean working directory matching the HEAD commit.

git status - tells you the status of your project; shows you what commits have occurred, what files are being tracked and any files that are NOT being tracked.

Each of these commands have multiple options that are available. The complete Git reference guide can be found at www.git-scm.com. You can also find a nice succinct cheat sheet here.

Additional Information about Entry

Comments

No comments yet.

Post your comment