Skip to content

Instantly share code, notes, and snippets.

@stevekm
Last active September 21, 2016 18:25
Show Gist options
  • Save stevekm/e57c4c36d1af3b5f215187713ababdb0 to your computer and use it in GitHub Desktop.
Save stevekm/e57c4c36d1af3b5f215187713ababdb0 to your computer and use it in GitHub Desktop.
A quick guide on how to commit changes to a git repository

Set up a new git repository (if it doesn't already exist, otherwise skip this step)

$ cd git_test/

$ ll
total 36K
drwxr-s--- 2 kellys04 kellys04   0 Sep 21 13:43 .
drwxr-s--- 4 kellys04 kellys04 350 Sep 21 13:43 ..

$ git init
Initialized empty Git repository in /ifs/home/kellys04/testdir1/git_test/.git/

$ ll
total 38K
drwxr-s--- 3 kellys04 kellys04  22 Sep 21 13:44 .
drwxr-s--- 4 kellys04 kellys04 350 Sep 21 13:43 ..
drwxr-s--- 7 kellys04 kellys04 193 Sep 21 13:44 .git

Make some files

$ echo 'this is a test dir' > README.md

Check the repo status

$ git status
On branch master

Initial commit

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

        README.md

nothing added to commit but untracked files present (use "git add" to track)

Stage the first file that we want to (eventually) add to the repo

$ git add README.md

$ gst
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   README.md

Commit the changes (this adds them to the repo; make sure to include a message here with -m or you'll likely end up in vim)

$ git commit -m "add base files"
[master (root-commit) 8759350] add base files
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

$ git status
On branch master
nothing to commit, working directory clean

$ ll
total 96K
drwxr-s--- 3 kellys04 kellys04  49 Sep 21 13:44 .
drwxr-s--- 4 kellys04 kellys04 350 Sep 21 13:43 ..
drwxr-s--- 8 kellys04 kellys04 270 Sep 21 13:45 .git
-rw-r----- 1 kellys04 kellys04  19 Sep 21 13:44 README.md

Make a new file in a dir, update existing file

$ mkdir new_dir

$ echo "another test file" > new_dir/test_file1.txt

$ echo "more test text" >> README.md

Check the git status to see changes


$ git status
On branch 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:   README.md

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

        new_dir/

no changes added to commit (use "git add" and/or "git commit -a")

Stage changes to files that were previously in the repo

$ git add -u

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md

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

        new_dir/

Stage new files

$ git add new_dir/

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md
        new file:   new_dir/test_file1.txt

Oops made a mistake, remove changes from the staging area without changing the files! First check the file:

$ cat README.md
this is a test dir
more test text

Now remove all changes and new files from the staging area:

$ git reset HEAD
Unstaged changes after reset:
M       README.md

$ git status
On branch 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:   README.md

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

        new_dir/

no changes added to commit (use "git add" and/or "git commit -a")


$ cat README.md
this is a test dir
more test text

You can see that the git staging area is cleared but the files have not been affected. Let's start over and add them to the staging area again:


$ git add -u; git add new_dir/

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md
        new file:   new_dir/test_file1.txt


Oops we made another mistake, this time lets clear the staging area AND remove changes to the files. WARNING: this will remove ALL changes to the repo since the last commit, including deleting new files and changes to old files.


$ git reset HEAD --hard
HEAD is now at 8759350 add base files

We can see that the new dir and file have been removed, and the previous file has lost its changes:

$ ll
total 72K
drwxr-s--- 3 kellys04 kellys04  49 Sep 21 13:47 .
drwxr-s--- 4 kellys04 kellys04 350 Sep 21 13:43 ..
drwxr-s--- 8 kellys04 kellys04 297 Sep 21 13:47 .git
-rw-r----- 1 kellys04 kellys04  19 Sep 21 13:47 README.md

$ cat README.md
this is a test dir

Lets repeat the changes one more time

$ mkdir new_dir; echo "another test file" > new_dir/test_file1.txt; echo "more test text" >> README.md

$ ll
total 98K
drwxr-s--- 4 kellys04 kellys04  74 Sep 21 13:48 .
drwxr-s--- 4 kellys04 kellys04 350 Sep 21 13:43 ..
drwxr-s--- 8 kellys04 kellys04 297 Sep 21 13:47 .git
-rw-r----- 1 kellys04 kellys04  34 Sep 21 13:48 README.md
drwxr-s--- 2 kellys04 kellys04  32 Sep 21 13:48 new_dir


And stage the changes

$ git status
On branch 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:   README.md

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

        new_dir/

no changes added to commit (use "git add" and/or "git commit -a")

$ git add -u

$ git add new_dir/

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md
        new file:   new_dir/test_file1.txt

Everything looks good, commit the changes


$ git commit -m "Add new dir, update README"
[master 126676b] Add new dir, update README
 2 files changed, 2 insertions(+)
 create mode 100644 new_dir/test_file1.txt

At this point, assuming you have your git remote set for a remote repository (e.g. GitHub, BitBucket), you can git pull and git push to pull down the most recent copy of the remote repository and push your new changes, respectively.

Reference:

http://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add

$ git --version
git version 2.6.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment