Actions

EmSys

Git - Changes not Files

From EdWiki

Revision as of 09:58, 23 June 2015 by Jshankar (Talk | contribs) (1 revision imported: EdWiki -- From Shukra)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

GitLab - Changes, not Files

  • Goals
    Learn that git works with changes, not files.
  • Most source control systems work with files. You add a file to source control and the system will track changes to the file from that point on.
  • Git focuses on the changes to a file rather than the file itself.
  • When you say git add file, you are not telling git to add the file to the repository. Rather you are saying that git should make note of the current state of that file to be committed later.
  • We will attempt to explore that difference in this lab.
  • First Change: Allow a default name
    Change the “Hello, World” program to have a default value if a command line argument is not supplied.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
    if( argc > 1 ) {
        puts(argv[1]);
    } else {
        puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
    }
    return EXIT_SUCCESS;
}
  • Add this Change
    Now add this change to the git’s staging area.
git add hello.c
  • Second change: Add "Hello" and command line argv
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
    if( argc > 1 ) {
        puts("Hello ");
        puts(argv[1]);         /* prints !!!Hello argv[1] */
    } else {
        puts("Hello World!!!"); /* prints !!!Hello World!!! */
    }
    return EXIT_SUCCESS;
}
  • Check the current status
git status
  • Notice how hello.c is listed twice in the status. The first change (adding a default) is staged and is ready to be committed. The second change is unstaged. If you were to commit right now, the second change would not be saved in the repository.
  • Committing
    Commit the staged change (the default value), and then recheck the status.
git commit -m "Added a default value"
git status
  • The status command is telling you that hello.c has unrecorded changes, but is no longer in the staging area.
  • Add the Second Change
    Now add the second change to staging area, then run git status.
git add .
git status
  • Note: We used the current directory (‘.’) as the file to add. This is a really convenient shortcut for adding in all the changes to the files in the current directory and below. But since it adds everything, it is a really good idea to check the status before doing an add ., just to make sure you don’t add any file that is not intended.
  • Now the second change has been staged and is ready to commit.
git commit -m "Added a comment"