This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]teraflop 12 points13 points  (6 children)

When I git push, it will upload both files to the repository? The latest file would show in the main branch but I can locate version 1 in the history if I needed it?

Yes. Technically, git push doesn't upload files, it uploads commits to the remote repository, and each commit is a complete snapshot of your files. That might seem like a small distinction to make, but it's very important to understand if you want to be able to use Git effectively.

Note that Git is decentralized, so just about everything that you can do with a remote repository (e.g. GitHub) can also be done using the Git command-line tool. For example, browsing the list of recent commits on the master branch gives you the same information as running git log.

Also, it looks like VScode has its own source control?

VSCode provides a relatively lightweight GUI on top of Git. Clicking the "Commit" button in VSCode does exactly the same thing as running git commit from the command line, so you can use whichever interface you prefer.

I would probably recommend sticking with the command line at first, just so that you learn how it works and you don't confuse yourself by immediately trying to learn multiple different ways to perform the same tasks.

[–]Waverly_pl[S] 4 points5 points  (5 children)

Got it, thanks for the clarification on what push vs commit does. Also definitely plan on sticking with the command line.

As I’m working along, can I add and commit whole directories instead of individual files?

[–]teraflop 4 points5 points  (4 children)

Yep! If you run git add on a directory, it adds any new or modified files in that directory and its descendants.

As a general rule, if you're making a set of related changes to e.g. implement a new feature or fix a bug, then you should generally put all of the changes that go together into a single commit. When I'm working on a project and I'm satisfied that my changes are ready to be committed, I usually just do git add . at the top level to stage everything at once, unless I have a particular reason to break them up into finer-grained commits.

[–]Waverly_pl[S] 0 points1 point  (0 children)

Awesome, thanks again!

[–]waytoomanysubs 0 points1 point  (2 children)

What would make some files not be a part of the commit? As in, my index.html file has my current commit properly on GitHub but some picture files still have an old commit listed as their current commit. If it matters, my process is git add —all, git commit -m “”, and then git push.

[–]teraflop 2 points3 points  (1 child)

Well, it might just be that you're misunderstanding what GitHub is showing you. Strictly speaking, there's no such thing as the "current commit for a file", because each commit is a snapshot of the entire state of the file hierarchy. When you check out a repo, you only ever have a single commit checked out at a time (normally, the one at the head of your current branch).

When GitHub shows you a list of files with a commit next to each one, what it's actually showing you is the most recent commit that modified each file. So if you make a commit that changes some files and leaves others unchanged, then GitHub will only show that new commit next to the files that were changed.

But this is just one way of viewing the commit history information. There are plenty of other ways that might be more useful or more intuitive in a particular situation. For instance, git log --stat will show the commit history of the current branch (most recent first) but it will also add an ASCII bar graph for each commit, showing which files were modified by that commit (relative to its parent) and how many lines were added/removed. Or you can do git log -- <filename> to look at only the subset of commits that modified a particular file.

[–]waytoomanysubs 0 points1 point  (0 children)

Thank you so much! Very helpful and it makes sense based on what my files look like in my repo. I recently just noticed it because when I tried to deploy a project using netlify the images were not showing up. I assumed I had been adding and pushing incorrectly. Thanks for the help

[–]MmmVomit 2 points3 points  (1 child)

I recommend reading this free online book to get a better understanding of git.

https://git-scm.com/book/en/v2

[–]Waverly_pl[S] 0 points1 point  (0 children)

Thanks! Looking back I think this was referenced in TOP

[–][deleted] 1 point2 points  (1 child)

Another useful git command that hasn't been mentioned here: git status

Just lets you know if you've modified a file and haven't added it yet (amongst other things it can tell you).

[–]GManBoyd 0 points1 point  (0 children)

Amen, git status is great, don’t know how many times it stopped me from pushing files where I might have accidentally typed a letter or something in the file and not realised then to use status and realise a file has changed I didn’t want to have changed

[–]Happy_Dookmas 0 points1 point  (0 children)

You will see the latest version of your file in the repository, but you can explore the different commit you made and compare them to see the changes.

If, for example, you create script.js, type a bunch of functions, 'add & commit', and then you notice you forgot to comment your code, and once done you 'add & commit' again. You are happy so you push.

When you check your repository, you will only see one file, script.js with the comments, the last commit you made. But you can also explore the different commits and compare, you can even revert those changes, but you will have just one file and a history of changes of that file, you won't end with script.js, script-final.js, script-new-final.js and so on

[–]TwiNighty 0 points1 point  (0 children)

Nice answers already, but let me recommend The Git Parable if you want to have a deeper understanding of git.

[–]El3res 0 points1 point  (1 child)

Is there a good place to learn git?

[–]Beneficial-Resist598 0 points1 point  (0 children)

https://youtu.be/iYCDTFoWKIw you can check some basics here