Common Git Commands

Here is a bare-minimum set of commands that will cover roughly 90% of your version control needs.

Command What it Does Example Output
git clone git@github.com:<username>/<project>.git Creates a local copy of the project repository on your laptop
> git clone git@github.com:davdar/cs225-demo.git
Cloning into 'cs225-demo'...
remote: Counting objects: 35, done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 35 (delta 8), reused 33 (delta 6), pack-reused 0
Receiving objects: 100% (35/35), done.
Resolving deltas: 100% (8/8), done.
git status Shows the current status of your local repository
> git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   afile.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	some-new-file.txt

no changes added to commit (use "git add" and/or "git commit -a")
git diff Show changes between current working copy and last committed version
> git diff
diff --git a/afile.txt b/afile.txt
index 78b4675..ee5e6ec 100644
--- a/afile.txt
+++ b/afile.txt
@@ -1,4 +1,4 @@
-This is an old file!
-(The end of the semester is just a little bit *NOT* fun!)
-But I still love this file...
+This is a pretty old file!
+(The end of the semester is just a little bit fun!)
+But I still love this particular file...
 (  I am so sleepy all the time... :(  )
git diff --word-diff Like git diff but shows changes per-word rather than per-line
> git diff --word-diff
diff --git a/afile.txt b/afile.txt
index 78b4675..ee5e6ec 100644
--- a/afile.txt
+++ b/afile.txt
@@ -1,4 +1,4 @@
This is [-an-]{+a pretty+} old file!
(The end of the semester is just a little bit[-*NOT*-] fun!)
But I still love this {+particular+} file...
(  I am so sleepy all the time... :(  )
git add <file> Tell git your intent to commit changes to this file
> git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   afile.txt

no changes added to commit (use "git add" and/or "git commit -a")
> git add afile.txt 
> git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   afile.txt

git add . Tell git your intent to commit all changes in the current directory analogous to above
git add --dry-run . Ask git what would happen if you did git add ., but without actually performing the action
> git add --dry-run .
add 'afile.txt'
add 'another-new-file.txt'
git commit -m "<message>" Commit the changes to files that you told git about from prior git add commands
> git commit -m "changed the file to be moar better"
[master e423001] changed the file to be moar better
 1 file changed, 3 insertions(+), 3 deletions(-)
git push Push the changes that have been committed locally to the repository origin (e.g., GitHub)
> git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 392 bytes | 392.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:davdar/cs225-demo.git
   664cc23..e423001  master -> master
git pull Incorporate changes that a friend pushed to GitHub into your local view of the repository
> git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:davdar/cs225-demo
   e423001..b208d88  master     -> origin/master
Updating e423001..b208d88
Fast-forward
 afile.txt | 1 +
 1 file changed, 1 insertion(+)

Last updated April 19, 2018