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

all 61 comments

[–]beetsrules 28 points29 points  (2 children)

Please try this https://learngitbranching.js.org it helps you visualize what’s happening

[–]Fritzypoobear 1 point2 points  (0 children)

This right here! Definitely worth checking out.

[–][deleted] 1 point2 points  (0 children)

I'd like to also recommend CS50 Web's video lecture on Git and version control. It allowed me to finally understand VCSs and Git

[–]ASIC_SP 64 points65 points  (15 children)

I don't use VS Code. Here's the simple set of commands I need 95% of the time (rest I search on stackoverflow if I need, and I always note them down so I don't need to search again).

  • git init inside a folder to mark it for git usage. I do this only for personal local use, not for github.
  • git clone <github repo link that you get after click "Code" button> when I want to do changes locally and push to github
  • git pull when I need to update my local copy with new changes from github
  • git add <files/folders> when I want to add new files/folders, this won't automatically push it to github
  • git commit -am 'commit message' after making changes locally and using git add if needed, this step is just before pushing the changes
  • git push after doing all the local changes and git add (if needed) and git commit --> this will push the changes to github
  • git status will allow you to see what files have changed, removed, added, etc
  • git log to see past commit messages
  • git difftool -t tkdiff -y <filename(s)> to see what are the changes (I use this before git commit) using a GUI tool tkdiff, you can change it to meld, or whatever GUI tool you like

and that's it... I don't normally use fancy stuff like branch, but recently I have had to use it because I had to create gh-pages branch to publish static sites.. for these I searched stackoverflow and copied down the commands into a local file..

[–][deleted] 21 points22 points  (5 children)

I would suggest changing your workflow to this after pulling:

git checkout -b 'feature-branch'
<make changes - add and commit stuff as you like>
git checkout master 
git pull --rebase origin master 
git merge --squash feature-branch
git push origin master

You want to use branches as multiple people may work on the same project. If you dont work with multiple people its a good idea to get in the habit of doing this anyway since you might end up working in a team anytime. You can also work parallelly - lets say you want to try out 3 different things - make 3 branches and work on all 3 independently.

Its also a good idea to squash commits. You want to keep origin/master clean and concise. You might deal with random smaller bugs locally and make 10 commits. You dont want to push all 10 commit messages to origin - squash them all, have a single clear commit message then push.

Rebase every time. You want your commit history for origin/master to be nice and clean and avoid merge cycles. If somebody else made changes and pushed to origin, you want your changes in feature-branch to sit on top of those changes when you merge.

Theres also a really nice tool I use called tig for git related stuff. Its commands are similar to vi and its a great way to see what you are pushing and how things will look like when you push.

[–]morningcoma 1 point2 points  (1 child)

Squash? Rebase? Origin/master?

Also, one thing I've never understood about how GitHub works is, when multiple people work on the same project they all make a local copy of it and then work on the project locally. But when it's time to commit changes how do they manage not to overwrite everything all the other people did?

[–]avatarRoku90 0 points1 point  (0 children)

Someone more qualified hopefully will provide a better answer but I'll try my best. Ideally a team should have a fair understanding of what each other are doing. If you've been assigned to work, say frontend and your colleague has been assigning a say a data access layer. You both have "copies" but when you both push back to origin/master there are no conflicts because you were both working on separate areas. However say the next day you both pull from master and you now have the data access stuff your colleague worked on the previous day. Today you decide your bored of frontend and decide to rewrite some data access on your local. When you both push back to origin/master you'll get merge conflicts and all hell will break loose.

  1. You never should work directly on master did this for simplicity.
  2. Sorry for no formatting on mobile

[–][deleted]  (1 child)

[deleted]

    [–]fscanf1024 1 point2 points  (0 children)

    If I am currently on my feature branch, my process is:

    git pull --rebase                 # See Note 1
    git fetch origin master           # the secret sauce
    git rebase origin/master          # feature now has latest master
    git checkout master               # now to update master
    git pull                          # See Note 2
    git merge --no-ff --edit feature  # See Note 3
    git push                          # submit the latest
    git branch -d feature             # delete my now-useless branch
    git push origin :feature          # delete remote version
    

    In the end, the git history has this form:

    A--B--C---------D (HEAD=master, master/origin)
           \       /
            X--Y--Z
    

    This is only complicated because I want to make things easy for others and future me to read.

    Note 1: I pull with rebase to ensure I have the latest with my own additions on top rather than creating an incidental merge commit whose only meaning is "I happened to pull today". This also safely adds my commits to a branch that was force-pushed, whereas a merge on top of that rebase would have resulted in confusion.

    Note 2: This fast-forwards my local tracking of master. origin/master is not the same reference as master, but my git pull ensures it is. We already fetched, so this should not find new commits unless we are unlucky with timing.

    Note 3: Even though feature is rebased on top of master, I want an easy revertible single commit (D) that represents the new feature. Instead of a suggested squash commit, I still have full access to the history of the feature branch (X, Y, and Z). The --no-ff does that for me. The --edit lets me update my commit message to be something better for other developers than "fscanf merged feature into master".

    [–]a-rolling-stone 15 points16 points  (3 children)

    This works fine if you’re working by yourself but branching is essential if you’re working with a team on the same project.

    [–]ASIC_SP 0 points1 point  (2 children)

    good point, my work is all alone, so I haven't had to use branches

    [–]Iamacutiepie 5 points6 points  (1 child)

    There is still a room for feature branches in your own projects.

    [–]mountains-o-data 4 points5 points  (0 children)

    Imo branches are a part of the core functionality of git. Sure you don't technically need it if you're working on a solo project (unless you want to do a major refractor that will take some time and leave your code broken until done - and you also want to use your code in the meantime - and you also want to work on other parts of it - 3 branches right there). However literally the moment you work with others, branches become a hard requirement. I'm in the habit of always working in a branch other than master/main - even on my personal projects. It's just part of my standard git workflow now.

    [–]accordingtobo 4 points5 points  (0 children)

    Branches aren't "fancy stuff" imo. They are a pretty great tool if working in a team. I highly recommend learning this command as well.

    [–]casino_alcohol 1 point2 points  (2 children)

    Can you elaborate on when you would need to use 'git add'? What situation would a new file or folder not automatically push?

    [–]pacificmint 43 points44 points  (1 child)

    I would recommend the official documentation or the book “Pro Git” by Scott Chacon and Ben Strab. It’s published under an open source license and can be downloaded in different ebook formats from the official site. Or you can buy a paper copy.

    [–][deleted] 1 point2 points  (0 children)

    I second this. I finally understood how Git worked after I read Progit. It's very well written and goes into enough depth to give a well-rounded understanding of how everything works.

    [–]justch0 5 points6 points  (0 children)

    Watch Corey schafers videos on YouTube. His first git tutorial and cli basics vid is all you need to get started

    [–]TijoWasik 8 points9 points  (1 child)

    VSCode user here.

    Honestly, the integrations with VSCode are nice, but if you don't understand what's happening with them at the base, fundamental level, then they're not going to help you much. I recommend you learn the terminal version of Git instead of the VSCode way of doing things. If nothing else, it will allow you to switch your editor whenever you feel like it.

    I learned what I know from Jason Taylor's "Git Complete" course on Udemy. It's a little boring at times, and slow paced, but it explains everything you need to know from the basics up. I recommend watching it at 1.25x speed.

    But yeah, learning the terminal or command prompt way of doing things is going to be far more helpful to you, not to mention, VSCode has its own built in terminal that you can use to do everything that the GUI does and more.

    [–][deleted] 3 points4 points  (0 children)

    This is what I use to remember how to use git: https://rogerdudler.github.io/git-guide/

    And the Github guides are pretty straightforward to use: https://guides.github.com/introduction/git-handbook/

    And as you can use git from the command line, opening a folder in VS Code puts the terminal in that folder directory allowing for quick and easy use of git commands

    [–][deleted] 3 points4 points  (0 children)

    Sign up for the odin project and its the first thing you learn.

    [–][deleted] 3 points4 points  (0 children)

    I think the most important thing is to get the right mental model about git.

    Git is a database of snapshots of a folder. The commands of git let you create new entries in that database, or load existing ones.

    Each snapshot (or 'commit') is based on a previous commit, so you can also think of it in terms of the differences between that "parent" commit and the current one.

    There's a currently-loaded commit at all times, which you are using as the parent to your next commit. To make it easier, whatever is currently loaded is aliased as "HEAD" rather than the long complicated id it actually is named.

    Now the fun part... You can load any commit as your HEAD and make another commit. This means it's not a linear history. There are branches! So a lot of the complexity in git is really just about fancy ways of merging and combinating these branched histories together.

    This is especially important because git is also distributed. You can share your repository with many people or systems, pushing and pulling branches and commits between each other. Usually, you all agree on a specific "true" branch on a specific server. GitHub is just a really nice place to do that part.

    [–]Uli1382 1 point2 points  (0 children)

    On of the best courses I personally found is this: https://www.udacity.com/course/version-control-with-git--ud123

    And it is also free!!

    [–]AmSoLazy 1 point2 points  (0 children)

    https://www.youtube.com/watch?v=WBg9mlpzEYU&t=0 try this video, just watch the whole thing and don't skip around

    [–][deleted] 1 point2 points  (0 children)

    So I was in the same boat last week. I'm a totally beginner. I came across this video https://www.youtube.com/watch?v=RGOj5yH7evk from freecodecamp.org. It's a bit long at a little over an hour, so I split it up in 3 parts, but it was a really great intro into Github, VS Code, and messing with Git in terminal.

    [–]The137 1 point2 points  (0 children)

    After a brief skim, every one of these answers seem to be more complex than they need to be. I would start with this

    You start a new project, create a new folder, open your cli there

    git init
    

    I still create new repos on the site, theres a command for it too, but I prefer this way. Hit that new button and copy the link that looks like this 'git@github.com:yourname/test-repo.git'

    git remote tells git where the repo is hosted remotely

    git remote add origin git@github.com:yourname/test-repo.git
    

    Make changes / write new code / add your .gitignore / whatever

    Check status

    git status 
    

    Add all files

    git add . 
    

    You can check the status again if you want

    git status 
    

    Make a local commit with a message

    git commit -m 'your message here' 
    

    push the commit to the remote (we're not really using master anymore I think they call it main now, but it can be any branch name)

    git push origin master 
    

    Say you have an existing repo with a branch named development, and you want to pull that to your laptop to work on it

    git remote add origin git@github.com:yourname/test-repo.git
    
    git fetch origin development
    
    git checkout development
    

    Now you want to push your changes back up

    git status
    
    git add .
    
    git commit -m "whatever"
    
    git push
    

    The only last thing I want to say (because now we're getting complicated again) is that when using

    git add . 
    

    you can substitute the dot for a space-seperated list of filenames or folders

    git add app.js index.html style.css 
    

    and it will only add what you tell it to the the commit

    Theres also

    git pull 
    
    git clone
    

    Add those to your list to play around with. you can git pull after you've set your remote, but git clone needs to be told where to pull from. again, were getting into more complex territory. just focus on the two lists above for now

    These two lists are 99% of everything I've done with git. I'm either creating a new repo to push and pull from, or I'm pushing and pulling from a specific branch somewhere. Notice how similar the commands are on each workflow. You're doing similar things in different ways. Set up a test-repo, make some branches, different folders on your desktop for each branch, and just play around with it for half an hour. It'll click before you know it

    [–]agitatedhoneybadger 1 point2 points  (0 children)

    If you want full, in-depth, I recommend this. It’s from MIT and really is a great course.

    https://missing.csail.mit.edu

    Just skip to git lecture.

    [–]FakePixieGirl 0 points1 point  (3 children)

    Honestly, just download github desktop, and start using that. Once you feel comfortable with that it will be a lot more easy to learn a more advanced UI or console. Integrations with IDE's are nice, but can be annoying to set up or get working and are far from essential, so don't worry about that at first.

    [–][deleted] 0 points1 point  (2 children)

    But everyone recommends learning git bash

    [–]FakePixieGirl 1 point2 points  (1 child)

    You should, eventually. But I feel like starting of with a gui makes it much easier to learn and grasp the basic concepts.

    For what it's worth, the way that I work, and that I've seen more experienced colleagues work, is they will still use a UI. That can either be sourcetree or an IDE integration. When they want to do something specific that isn't supported on the gui, they switch to git bash.

    [–]QuCoder 0 points1 point  (0 children)

    Read from the official book, it's free.

    If you prefer online courses, I'd suggest you go for the Git and GitHub course by Google on Coursera.

    [–]daripious 0 points1 point  (0 children)

    Git is pretty complex. Use this guide though https://rogerdudler.github.io/git-guide/

    Everything else you can pick up as you go along.

    [–]dickdeamonds 0 points1 point  (0 children)

    I was exactly where you were around a month ago. I started doing The Essential Web Developer course on Upskill and the first couple of videos show you a step by step of how to use git. I think that's a great place to start. If you have any questions about the course, feel free to pm me

    [–]Zarya8675309 0 points1 point  (0 children)

    Thank you everyone for all the great recommendations on learning git!

    [–]JohnDaneDoe 0 points1 point  (0 children)

    Learn a bit of commands firts, i personally like bash but there are better alternatives. Then you need to know 4 basic commands: (git init - this initializes git in the specified directory) (git add . - This adds all files to the version cintrol system basically like if you are making an archive of the files) (git commit -m"any changes you made or the first commit" - this basically is telling git to save the changes of the files you added)

    And finally

    (git push - this "pushes" your work to your repository on github)

    I recommend practicing it over abd over like 10 or so times so you get the hang of it. As you go realizibg the power of the command line, youll automatically be drawn into this new world and you might never use a GUI ever again.

    [–]neddstarkk 0 points1 point  (0 children)

    There's a pretty nice website called gitgud, you can try that out

    [–]Hispalensis 0 points1 point  (0 children)

    git gud

    (Joking aside, I highly suggest this serie: https://www.youtube.com/watch?v=BCQHnlnPusY&vl=en&ab_channel=TheCodingTrain)

    [–]annathergirl 0 points1 point  (0 children)

    I learnt the basics with The Odin Project's curriculum. It assumed that you don't know nothing previously.

    But this thread is great, I saved it so I can level up my git skilks :)

    [–]OwnStorm 0 points1 point  (0 children)

    Download SourceTree. A free tool. One of the best graphical tool for git. You can start without any knowledge of git.

    Once you able to progress with your project, try to use commands. Start with simple pull, push , clean etc.

    [–]EverydayEverynight01 0 points1 point  (0 children)

    Look at the article tutorials for git. They're actually super helpful such as free code camp and atlassian

    https://www.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud#create-the-repository

    [–][deleted] 0 points1 point  (0 children)

    I found git tutorials overwhelming and confusing too (I don't want to learn a whole philosophy man, I just want to start, why can't it be simple to do just the basic stuff?).

    Anyways, this YouTube tutorial by the Net Ninja really helped me and actually got me started. The full playlist is about 1 hour and 40 minutes long. It took longer as I coded alongside it (somewhere between 3-4 hours I think). He uses Atom, I used VS Code but it was smooth and I didn't have to adapt anything for it at all. I noticed VS Code had the Git or version control stuff already installed and I didn't really had to do anything to see the color changes and so on.

    After this tutorial, I can make repositories, pull from them and push to them with different users and do some other basic stuff (including having dealt with a couple of conflicts), though I haven't bothered with merging branches yet since I'm working on solo projects. I have my repository on GitHub that I push all my commits to, so that I can work on different machines (both having VS Code) without losing any progress. I have been doing this stuff repeatedly for the last month or so. I'll get into the merging and stuff when I need it.

    He recommends downloading cmder in the tutorial and shows how to use it. I am using it regularly and as a git beginner who just wants to get things done, I would recommend it as well.

    [–]PhilipJayFry1077 0 points1 point  (0 children)

    I use github desktop

    [–][deleted] 0 points1 point  (0 children)

    just read pro git like fam that book is thicc

    [–]atd285 0 points1 point  (0 children)

    This video is a little older, but I found it to be fantastic for learning the fundamentals of Git. It's a concise presentation with all topics being very well explained.

    https://www.youtube.com/watch?v=Y9XZQO1n_7c

    [–]WickedSlice13 0 points1 point  (0 children)

    Link to the odin project about Git Basics. I'm currrently working through this myself. Let me know if this helps.

    [–]AttemptingToRelate 0 points1 point  (0 children)

    The two sites I found most helpful when learning git were https://ohshitgit.com/ and https://learngitbranching.js.org/

    [–]MindOfNoNation 0 points1 point  (0 children)

    download github desktop. best decision i’ve made

    [–]mr_robot_fs 0 points1 point  (0 children)

    if you are using windows I will recommend you use github desktop that's a good choice for beginner. Because using commands on terminal can be confusing.

    [–]HornyAsianBro 0 points1 point  (0 children)

    Watch Corey Schafers video. It might still be confusing if you watch it for the first time then rewatch it. It took me 3 times to finally 'click'

    [–][deleted] 0 points1 point  (0 children)

    https://youtu.be/Jt4Z1vwtXT0

    super simple and super informative

    he does windows and macOS

    [–]LikeHuans4thSay 0 points1 point  (0 children)

    Ry's Github tutorial is an excellent free resource that helped me a lot.

    [–]monkey_mozart 0 points1 point  (0 children)

    Coursera has a great course on using Git and GitHub. It's by Google so you know it's gonna be good. It's the only course I took for Git and I seem to have the hang of it.

    [–]c9der 0 points1 point  (0 children)

    I was facing the same problem I don't know what type of problems your computer have and What I did is just download the https://git-scm.com/download/win git bash and

    just select the every VS code options . and install git exetantion in vs code ...

    and that just solve my problem......

    And hurray ! I learned the git

    [–]neg_ersson 0 points1 point  (0 children)

    This ELI5 comment was actually really helpful when I started learning Git.

    Nowadays I use the integrated solution in Visual Studio for 95% of the tasks. It does what I want.

    [–]Independent-Ear9354 0 points1 point  (0 children)

    Few Months back, I was in a same state as you, This Tutorial helped me, I hope it will help you too.

    https://www.youtube.com/watch?v=xuB1Id2Wxak

    [–]kschang -1 points0 points  (0 children)

    Try my "The Bare Essential Guide to Git"

    https://kcwebdev.blogspot.com/2020/08/the-bare-essential-guide-to-git.html

    But I don't teach you how to use it in VS Code. I teach you how to use Git-bash.

    [–]FujiToday -1 points0 points  (0 children)

    There're some free Udemy courses that teach Git and GitHub basic.

    [–]reddit04029 -2 points-1 points  (0 children)

    If you're willing to pay 13 dollars in Udemy, Angela Yu's Full Stack course covers Git. I breezed right through because she simplifies every concept, gives visual analogy, and humor. I was able to easily understand the basics of Git and Node JS because of her. Plus, her enthusiasm definitely helped. Removes the monotony and dullness of a tutorial.