Git, A beginners guide
Admittedly Git has been hard to understand , so I took a step back and worked through my old labs and discovered its not so scary and actually rewarding when you have a clear understanding of what Git is and how to use it .
Let’s get a bit technical here an overview back ground on Git .
Git is a “type of “software that version control uses , short for VCS its a type of software called a version control system (VCS). That is, software that controls our collection of versions. The most popular VCS is called “Git.”
There are several benefits we get when we manage our work with Git:
- Automatically create a backup of your work
- Provide an easy way to undo mistakes and restore a previous version of your work
- Document changes, including a log of what’s changed with messages explaining why it was changed
- Keep file names and hierarchies consistent and organized
- Branch work off into multiple “sandboxes” that can be experimented with but won’t impact each other
- Collaborate with others without disturbing each other’s or our own work
Git basics
let’s make a new file to put our new project, This command creates new a directory. I want to do a step by step to show you what it will look like.
mkdir creates a new folder give it a name “my-git-new-project”
We then want to get in to that new directory we
cd my-git-my-new project name
This command goes into the newly created directory.
git init : This command turns a directory into an empty Git repository. This is the first step in creating a repository(repo). After running git init, adding and committing files/directories is possible.
if ever you do git init
in the wrong directory, you can rm -rf .git
and return the directory to a plain-old, unprotected directory.
*Make sure you only type git init
within the directory you want git
to track. here we are in my “new project repo ..
git status
This command returns the current state of the repository. checking the status is a great way to know your current location .
git status will return the current working branch. If a file is in the staging area, but not committed Or, if there are no changes it’ll return nothing to commit, working directory clean.
Usage:
$ git status
IMPORTANT: Whenever we want to check the status of our Git repository — type git status
.
To keep track of changes in our files we use “git add
git add
Adds files into the staging area for Git in that current repo you’r working in Before a file is available to commit to a repository, the file needs to be added to the Git index (staging area). And works only in the current working directory files you are in so please make sure you are in the right files double check the names this was an issue I had naming files similarly and not adding changes to the correct repo . here in this example I added a readme to my repo I, ls into my project name and it lists all the files I have in my current repo I “git add . and then I check the status of my repo and in that status report am also given what branch am in (branch -master) and the new addition to my project a readme file .. looking good so far .
git commit:
Records the changes made to the current files you are working in that repository. every commit has a unique message .
To make our first commit, type: git commit -m "Initial commit"
.
It’s best practice to include a message with each commit explaining the changes made in a commit. Adding a commit message helps to find a particular change or understanding the changes, and please don’t forget to write that in quotes. “double quotes”
Usage:
git commit -m "unique message goes here" enter/return
git clone
To create a local working copy of an existing remote repository, use git clone to copy and download the repository to a computer. Cloning is the equivalent of git init when working with a remote repository. Git will create a directory locally with all files and repository history. We use git clone
to copy someone else's repo from the internet to our local machine.
Usage:
$ git clone (remote_URL)
git remote
is a branch on a (remote) repository stored on a server or a code hosting service like GitHub.
We can copy the whole repository using git clone
. Once we have our own copy of the repository, we can push new local branches to it, fetch updates from remote branches to our local repository, or fetch new branches from the remote repository.To connect your local repository to a newly created GitHub repository, you must add a new remote to a remote name. Adding a remote involves giving git
a "short name" and a "repository path. The default remote short name is called origin
. am adding my newly created repo type “git remote add origin and copy from your github the path link URL to your terminal …enter should look like the image below . to check if it is in your working directory type in the terminal “git remote -v”. We can see a list of all remote branches — that is all the branches at all the remotes — that Git knows about with git branch -a
.
git branch
A local branch is a branch that only we the user can see. It exists only on our computer. The git branch
command can show us our local branches:
- $ git branch
- * master
Git branch checks what branch you are currently in what branch the local repository is in. as in the example i created below I went into my git branch → tells me am in the *master branch ,
I then want to create a new branch “
Usage:
# Create a new branch
$ git branch --> branch_name , (new-branch-name (whatever you like) )
git checkout
To start working in a different branch, use git checkout (directory name) to switch between branches.
# Checkout an existing branch name (this will tell you what current branch you are on)
$ git checkout (branch_name)# Checkout and create a new branch with that name
$ git checkout -b (new_branch)one more tip
As a shortcut, we can switch back to the previous branch using:
"git checkout -
git merge
Before performing a merge there are a couple of preparation steps to take to ensure the merge goes smoothly. Confirm the receiving branch by checking the status of the branch “git status” make sure it in the correct merge branch , then git checkout to switch to the receiving branch, for example
“git checkout master”
Merging allows us to take branches and combines their content into another branch. It finds the differences between the branch you’re on and the branch you’re merging in and merges them in together.
git merge --> other-branch-name
to integrate the changes from one branch to the branch we are currently on. Here are some examples
Usage:
# Merge changes into current branch
$ git merge (branch_name)# Merge changes into current branch
$ git merge new_name
Git Delete
some things to keep in mind
Git will not let you delete the branch you are currently on !!!! so you have to make sure to checkout of that branch that you are NOT deleting. For example: git checkout master
when deleting a local branch use one of the following the difference between small -d and capital -D
$ git branch -d branch_name
$ git branch -D branch_name
The -d
option is an alias for --delete
, which only deletes the branch if it already has been fully merged . You could also use -D
, which is an alias for --delete --force
, which deletes the branch "regardless of its merged status."
Also git branch -d branch_name to be deleted
will fail if you are currently in the branch you want to remove.
if your deleting a remote branch
$ git push <remote_name> --delete ("name of the remote branch ")
Git pull
A git pull
is what you would do to bring a local branch up-to-date with its remote version, while also updating your other remote-tracking branches.
git pull = git fetch + git merge.
Git push
The git push request is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo.
- A remote name, for example,
origin
- A branch name, for example,
main
git push (REMOTENAME) to (BRANCHNAME)
I hope this beginner’s look at git will be helpful to all in understanding the most used git commands . I hope this was helpful .
the resources I have used for this blog post were
https://www.javacodegeeks.com/2018/09/git-pull-git-fetch.html
https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet