At one point a codenewbie, I found myself thoroughly confused by git commands, how to use them, when to use them, and so on. While there's many online resources outlining the commands, most of what I found wasn't 'beginner-friendly' or robust enough for my liking. In short, the documentation confused me even more. It seemed to me as if the people who wrote the docs were simply trying to help themselves remember the commands, as opposed to teaching a newbie what the commands meant.
For any code beginner stumbling across my blog, this post is for you! This is git, simplified.
Git Clone
git clone is used to target an existing Git repository and make a clone of that to exist on your local machine. You are able to sync the local clone to the remote with a few commands (which I will go over later in the post). After cloning it, you can cd into the directory to begin coding.
Git Checkout
Speaking of new branches, git checkout is used to create a new branch. The command for that is git checkout -b "your-branch-name-here"
Git Branch
git branch is a command used to check which branch you are currently working out of. A use case for this is when you're working on multiple features with other collaborators, you don't necessarily want to be working off the same branch. To keep things from getting mixed up or from breaking, you want to checkout a new branch to code your portion of the project. In this case, it's good practice to check what branch you're in just in case before pushing any changes.
Git Add
git add isn't quite the same as a traditional 'save' command. git add sends all the changes made to the staging area, which then is able to be saved with a different command. There are different types of add commands that stage certain pieces of information:
Git Commit
git commit is the command used to save your local copy of the project. The most commonly used command is git commit -m "your-message-here" where you include a message of any changes your made.
Git Push
git push is the command used to upload the local saved changes to the remote repository. After you push, it allows other collaborators to pull your branch, merge your changes with theirs, and work with the modifications you made.
Git Pull
git pull fetches any commits from the remote branch and allows you to merge it with your branch. When you're collaborating with others, the command you'll be using frequently will be git pull origin "name-of-branch-you-are-wanting-to-pull-from
In this example, my project partner's branch is called 'testbranch.' I want to merge what they have with my portion of the project in my own working branch, so I call testbranch at the end of the pull command.
This pull command only merges the work on your local copy. To share the merged work with the rest of your team, you will then add, commit, and push this newly merged branch for others to pull from.
These are just the basics git commands; while there's many more that you can utilize, having a good understanding of the basics will help any beginner get a jump start into collaboration. Happy coding!
References:
-https://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add?rq=1
-https://www.atlassian.com/git/tutorials/saving-changes
Top comments (1)
Great summary of the most common Git commands. Git has been a tedious hurdle for me since my apps have so far been solo projects. Will definitely save to reference later!