Git, staging and committing files

  • Thread starter fog37
  • Start date
  • #1
fog37
1,549
107
TL;DR Summary
Git, staging and committing files
Hello,
I started learning Git and I am very clear on some of the steps in the Git workflow. For example, we initialize a folder to be a repository with
Code:
git init

We place a file called file1.txt inside the folder, make some changes to it and stage it, i.e. we make a copy of the file in the staging area using
Code:
git add file1.txt
We can then complete the process making a commit using
Code:
git commit file1.txt
Along the way, we use commands like
Code:
git log
,
Code:
git status
...

I am not sure what do to from here. Is the command
Code:
git diff
with certain options the command to use? I guess we want to compare the file in the working directory with saved/committed versions of it....

Why is it important to use
Code:
git diff --cashed
to compare what is in the staging area with what has been committed?

At the end of the day, once we are happy with the all the changes to a file, do we use
Code:
git checkpoint hash
to take the commit/save version back into the working directory and use it/email it out?

Thank you
 
Last edited:
Technology news on Phys.org
  • #2
After you commit files, the next common command that most people will do is to push it to some remote repository, for both backup/storage as well as sharing code with other people (they can pull down from the remote repository)

git diff alone will show you the changes in all uncommitted files that you currently have. If you specify a file name such a git diff ./path/to/file.java even if it's already been committed, it will show you the difference between the most recent version of that file, and the the version before that, showing you what has changed.

The difference between git diff and git diff --cached are only that one gives you the full diff of un-staged files in the working directory you're in, the other one gives you the diff of any file that are staged to be committed (after a git add filename occurs, but before a commit has occurred)

I've personally never used a git checkpoint command in my 10+ years of software development. Typically after you're done writing code for the day, you'll either commit to the main/master branch directly, or you've committed it into a new branch, then push the changes to a remote repo as I described earlier with a git push.

I hope this helps!

Edit: Github.com has an excellent resource online about how to use git here: https://skills.github.com/
 
Last edited:
  • #3
weller said:
The difference between git diff and git diff --cached are only that one gives you the full diff of the working directory you're in, the other one gives you the diff of any file that are staged to be committed (after a git add filename occurs, but before a commit has occurred)
This isn't quite correct. Once you stage a file to be committed, git diff does not show you differences in that file any longer; only git diff --cached does. So if you have some files that are staged and some that are not, neither of those commands will show you all the differences.
 
  • #4
Ah thanks for the clarification!
 
  • #5
fog37 said:
I guess we want to compare the file in the working directory with saved/committed versions of it....
Per my previous post just now, git diff only looks at changed files in the working directory that are not staged for commit. git diff --cached only looks at changed files in the working directory that are staged for commit. "Changed" for both commands means relative to the last commit.

git commit has the -a option if you want to combine the "staging" and "committing" operations into one. I use this quite a lot for small changes to save time.

fog37 said:
take the commit/save version back into the working directory and use it/email it out?
The commit/save version already is in your working directory. git commit doesn't change your working directory at all. It just updates what is actually committed (stored in the repository) to match your working directory. So you can use the file in your working directory without having to do anything else.

As for emailing files out, the whole point of a distributed VCS like git is to not have to share files that way--instead, just have a clone of your repository in some place where both you and the person you want to share files with can access it.
 

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Computing and Technology
Replies
3
Views
972
Replies
16
Views
2K
  • Computing and Technology
Replies
13
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • General Discussion
Replies
9
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
Back
Top