Actions

Common Git Commands

From InfoTechPedia

These are some of the most common Git commands and their syntax.

Congfigure Git

First, tell git who you are and set some common info. You should at least configure the author name and email address which will be used with all your commits. Git will strip some characters (for example trailing periods) from user.name.

git config --global user.name "John Smith"
git config --global user.email [email protected]

Create a Bare Repo

Create a directory then change to directory or change to a directory with content already in it. mkdir is valid in Linux and Windows cmd line or powershell.

mkdir /mynewGitRepo
git init

cd /myProject
git init

Download Existing Git Project

There are two methods to download an existing project and create a local work copy. Depending on the host of the git repository, this will change the syntax.

Clone from GitHub

Either make the directory and cd to it and use git clone ... which will add an additional subdirectory ( which may not be desirable ) inside the newly made directory.

gitRepoDirectory
 | cd coolProject
 -- coolProject
    | git clone https://github.com/username/coolProject.git
    -- coolProject (new directory created with all the cloned files and directories)

Better to specify a working directory for better organization and create the directory.

gitRepoDirectory
 | git clone https://github.com/username/coolProject.git coolProject
 -- coolProject (which will have all the cloned files and directories)

The second method is more desirable otherwise you may end up with lots of directories, with only a git project directory in each one. This allows you to have a master git repo directory and then have all projects in that directory.

Cloning from Remote Server

Very similar to a GitHub repo clone only you will need to use ssh to shell into the server over port 22 or whatever the ssh port for the server is. You can use a similar directory structure, mkdir directory and cd to create your structure. Then issue the command to clone with shh. Of course the remote server needs access with ssh.

git clone ssh://[email protected]:22/path/to/repository/newProject/.git newProject

You will be prompted for a password, enter it and press return. If the path is correct and the /.git directory is found in the parent directory ( the example above, /newProject ) then the download and cloning of the git repo will complete. Remember is must be the absolute path to the file.

Adding Files

Adding files whether they are in a directory, multiple directories or at the root level of the directory will need to tracked by git. Tracking? This is the version or changes to a file, additions of directories or removal of files. Back to adding files. By adding a file or files, you are adding them to staging ( the index ). Make sure you are in the root directory of the project.

git add <filename> 

...or add all files in the directory and its subdirectories

git add *

Note: if you use a simple git add <filename> you must use the path to the file from the root directory of the project.

git add includes/form.php

Commit Changes

When you change files in a tracked ( indexed ) git directory you will want to "commit" these changes to be tracked. Committing these changes only commit them locally. You must push your changes to the remote repository. Use of the -m switch allows you to enter a commit message. Omitting it will make you add a message with prompt.

git commit -m "A meaningful commit message"

Added files? Added files and changed a few? Use -am

git commit -am "Changes to tracked files and adding a few new ones"
Git task Notes Git commands
Push Send changes to the master branch of your remote repository: git push origin master
Status List the files you've changed and those you still need to add or commit: git status
Connect to a remote repository If you haven't connected your local repository to a remote server, add the server to be able to push to it: git remote add origin <server>
Connect to a remote repository List all currently configured remote repositories: git remote -v
Update from the remote repository Fetch and merge changes on the remote server to your working directory: git pull
Update from the remote repository To merge a different branch into your active branch: git merge <branchname>
Update from the remote repository View all the merge conflicts:View the conflicts against the base file:Preview changes, before merging: git diff
git diff --base <filename>
git diff <sourcebranch> <targetbranch>
Update from the remote repository After you have manually resolved any conflicts, you mark the changed file: git add <filename>
Tags You can use tagging to mark a significant changeset, such as a release: git tag 1.0.0 <commitID>
Tags CommitId is the leading characters of the changeset ID, up to 10, but must be unique. Get the ID using: git log
Tags Push all tags to remote repository: git push --tags origin
Undo local changes If you mess up, you can replace the changes in your working tree with the last content in head:Changes already added to the index, as well as new files, will be kept. git checkout -- <filename>
Undo local changes Instead, to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it, do this: git fetch origin
git reset --hard origin/master
Search Search the working directory for foo(): git grep "foo()"