Actions

Difference between revisions of "Common Git Commands"

From InfoTechPedia

(new page)
 
m (TomHutchison moved page Git Common Commands to Common Git Commands without leaving a redirect: better name)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
These are some of the most common Git commands and their syntax.
 
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.
 +
 +
<pre>
 +
git config --global user.name "John Smith"
 +
git config --global user.email [email protected]
 +
</pre>
 +
 +
== Create a Bare Repo ==
 +
Create a directory then change to directory or change to a directory with content already in it. <code>mkdir</code> is valid in Linux and Windows cmd line or powershell.
 +
 +
<pre>
 +
mkdir /mynewGitRepo
 +
git init
 +
 +
cd /myProject
 +
git init
 +
</pre>
 +
 +
== 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 <code>cd</code> to it and use <code>git clone ...</code> which will add an additional subdirectory ( which may not be desirable ) inside the newly made directory.
 +
<pre>
 +
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)
 +
</pre>
 +
 +
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, <code>mkdir directory</code> and <code>cd</code> to create your structure. Then issue the command to clone with shh. Of course the remote server needs access with ssh.
 +
 +
<pre>
 +
git clone ssh://[email protected]:22/path/to/repository/newProject/.git newProject
 +
</pre>
 +
 +
You will be prompted for a password, enter it and press return. If the path is correct and the <code>/.git</code> directory is found in the parent directory ( the example above, <code>/newProject</code> ) 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.
 +
 +
<pre>
 +
git add <filename>
 +
 +
...or add all files in the directory and its subdirectories
 +
 +
git add *
 +
</pre>
 +
 +
'''Note:''' if you use a simple <code>git add <filename></code> you must use the path to the file from the root directory of the project.
 +
 +
<pre>
 +
git add includes/form.php
 +
</pre>
 +
 +
== 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 <code>-m</code> switch allows you to enter a commit message. Omitting it will make you add a message with prompt.
 +
 +
<pre>
 +
git commit -m "A meaningful commit message"
 +
</pre>
 +
 +
Added files? Added files and changed a few? Use <code>-am</code>
 +
 +
<pre>
 +
git commit -am "Changes to tracked files and adding a few new ones"
 +
</pre>
 +
 
{| border="1" class="sortable"
 
{| border="1" class="sortable"
 
!Git task!!Notes!!Git commands
 
!Git task!!Notes!!Git commands
|-
 
|Tell Git who you are||Configure the author name and email address to be used with your commits.Note that Git strips some characters (for example trailing periods) from user.name.||git config --global user.name "Sam Smith"git config --global user.email [email protected]
 
|-
 
|Create a new local repository||||git init
 
|-
 
|Check out a repository||Create a working copy of a local repository:||git clone /path/to/repository
 
|-
 
|Check out a repository||For a remote server, use:||git clone username@host:/path/to/repository
 
|-
 
|Add files||Add one or more files to staging (index):||git add <filename><br>git add *
 
|-
 
|Commit||Commit changes to head (but not yet to the remote repository):||git commit -m "Commit message"
 
|-
 
|Commit||Commit any files you've added with git add, and also commit any files you've changed since then:||git commit -a
 
 
|-
 
|-
 
|Push||Send changes to the master branch of your remote repository:||git push origin master
 
|Push||Send changes to the master branch of your remote repository:||git push origin master
Line 24: Line 89:
 
|-
 
|-
 
|Connect to a remote repository||List all currently configured remote repositories:||git remote -v
 
|Connect to a remote repository||List all currently configured remote repositories:||git remote -v
|-
 
|Branches||Create a new branch and switch to it:||git checkout -b <branchname>
 
|-
 
|Branches||Switch from one branch to another:||git checkout <branchname>
 
|-
 
|Branches||List all the branches in your repo, and also tell you what branch you're currently in:||git branch
 
|-
 
|Branches||Delete the feature branch:||git branch -d <branchname>
 
|-
 
|Branches||Push the branch to your remote repository, so others can use it:||git push origin <branchname>
 
|-
 
|Branches||Push all branches to your remote repository:||git push --all origin
 
|-
 
|Branches||Delete a branch on your remote repository:||git push origin :<branchname>
 
 
|-
 
|-
 
|Update from the remote repository||Fetch and merge changes on the remote server to your working directory:||git pull
 
|Update from the remote repository||Fetch and merge changes on the remote server to your working directory:||git pull
Line 59: Line 110:
 
|Search||Search the working directory for foo():||git grep "foo()"
 
|Search||Search the working directory for foo():||git grep "foo()"
 
|}
 
|}
 +
 +
[[Category:Git Commands]]

Latest revision as of 10:32, 1 March 2018

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()"