A Not-So-Complicated Git Manual Handbook
Git for time and time again — is something I would consider a weird software. I obviously use it a lot since I’m a software engineer, but then again — if someone were to ask me if I’m an expert at git, I would probably say
Of course not!
A little bit of introduction to Git, Git is basically a version control — a tool to manage folders and its version based on time and changes. Majority of software engineer use Git as their primary version control tool when developing. But some of them doesn’t even come close to knowing even half of what git can offer.
Git offers a lot of features that if used correctly will provide a much smoother experience when handling with developing especially when it’s a collaboration work. But coming from someone who only just knew a handful amount of git features after 2 years of using it day to day, I can tell you that the concept of these git features sounds tiring and needlessly complicated that’s why some don’t bother learning it (at least back then I didn’t).
It’s funny because if I could go back in time and tell my 2-years-ago self how wrong I am for not learning about git features, I would! Because oh boy, back then I used to be so scared of losing the changes I’ve made to a certain work, I literally copy and paste it locally on my file manager (You definitely know what I’m talking about).
In this article I will cover a simplified explanation of git features one by one while also providing examples to better understand it. And it starts with
GIT CLONE
One of two ways to initiate a git repository on our local computer. Basically, using git clone allows you to clone all the folders on a certain remote repository to your local computer.
git clone <CLONE URL>
The clone URL is available on your top right repository page as a blue dropdown button.
GIT INIT
Another way to initiate a git repository on our local computer. But this time, you’re basically starting from the bottom as the only folders provided is a .git folder to use git on the local path.
git init
yes, as simple as that.
GIT CONFIG
A simple way to understand this feature is as an ID configuration. It is used to set the identity we would want to be recognized with when making changes to our work.
git config --global user.name “<Name>”
git config --global user.email "<Email>"
the flag after git config command is used to specify the scope of our configuration.
- local : the configuration is used on current repository only
- global : the configuration is used on all repository in the current user tree
- system : the configuration is used on all repository and on all users
GIT BRANCH
Branches in a git repository each represent a line of development. In which one branch can be a derivation of other branch.
git branch command basically lets you create, delete, and view branches on your repository.
git branch #list all branches on your repository
git branch <branchname> #create a new branch
git branch -d <branchname> #delete a specified branch
git branch -m <branchname> #rename current branch to branchname
GIT CHECKOUT
this command lets you stroll around other branches you’ve created and manage using git branch.
git checkout <target-branch>
the command lets you switch from current branch to target-branch.
git checkout -b <new-branch>
#create a new branch forked from current one
git checkout -b <new-branch> <head-branch>
#create a new branch forked from a branch specified
you can also create a new branch using git checkout by specifying the flag -b on the command.
GIT ADD
Basically, after you made changes to your work, you need to call this command to say
hey git, I’ve done my changes, just to let you know
by using this command git will start tracking the changes you’ve made on your work, ready to be committed by git to the local git head. There are two ways that you can do this
git add <filename>
If you want git to “recognize” changes on certain files only
git add .
If you want git to recognize changes on all files on your local repository.
GIT COMMIT
After git recognizes our changes using git add, we need to actually move the changes to our local repository using git commit
git commit -m "commit message"
The commit message should be descriptive of the changes you’ve made to the work.
GIT REVERT
Think of the command like the undo of git repository. But it doesn’t just delete current commit until the past-commit desired, the command actually creates a new commit of past commit if that makes sense. It does that so git won’t lose history on all changes made and also reverted.
git revert <id commit>
to get the commit id we can use the git log command, more about it here
GIT STASH
Git stash allows you to shelve away works you’ve made but haven’t committed as you work on other features. Using this command, you basically save your uncommitted work somewhere else and revert back to the latest commit.
git status
On branch master
Changes to be committed:
new file: style.css
Changes not staged for commit:
modified: index.htmlgit stash
Saved working directory and index state WIP on master: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage
git status
On branch master
nothing to commit, working tree clean
You can go back to the shelved uncommitted work by using the command
git stash pop
That’s probably all about git commands regarding local repositories, now come commands involving online repositories
Brief explanation to online repositories, as the name suggest it’s basically a repository put on the internet — available to other people (GitHub, GitLab, etc.).
GIT REMOTE
This features manages connection by either creating, viewing, or deleting the connection to other online remote repository — A common repository used by all team members.
git remote #used to see connections to other repository
git remote add #used to add connections to other repository
git remote remove <name> #remove connection
git remote rename <old> <new> #rename a connection created
for more detailed explanation and examples see the documentary.
GIT MERGE
Git merge is used to combine different commits with different development history into one of the same. Usually used to combine contents from different branches.
git checkout <branch-a>
git merge <branch-b>
In this example, the contents of branch-b will be matched and merged by git into our current branch that is branch-a.
GIT REBASE
Merge and rebase often described doing the same thing by some people, but it’s actually pretty different. To put it simply, rebase allows you to change your “base” branch in which your current branch was created, changing your current branch history as if it was created from the new “base branch”.
in this example, the branch feature is rebased with master as if it was created from the latest master commits. Rebase is usually considered a beginner-don’t-touch command as it rewrites history unlike merge.
GIT FETCH
A command to download or fetch folders from online repository to your local repository with a detached head — basically a state where you can do anything without affecting other branches (except if you decided to merge).
git fetch <remote> #only download the remote repo specified
git fetch <remote> <branch> #branch specified
git fetch --all #download all registered remote
It’s considered a safe approach to downloading new changes from remote repositories as it does not merge with current changes.
GIT PULL
Now that you know about git fetch, what git pull does is basically calling git fetch and merge the downloaded repositories to the current local repositories.
git pull <remote>
As git pull tries to forcefully match the state of our local repository to the remote repository, it’s usually considered not safe because of conflicts that may appear on the process.
GIT PUSH
This command allows you to push or upload all the work and changes you’ve made on your local repository to your online repository connected, it pushes all your commits and files to your remote repository.
git push <remote> <branch>
git push <remote> --all
When using the command, you can specify which branch you wants to push or — by using the flag “all” you can push all of your local repository branches to the remote repository.
Well — That’s probably all that I can offer about git and its commands. Of course this article isn’t enough to cover all git commands and its implementations, I suggest you check the official documentation of git in here. Again — I’m by far an expert of git and I’m still learning about it myself. But I believe that these features is the basic of what people should know about git command. So yeah! by knowing this you now doesn’t have to copy and paste your repository in an attempt to make a backup — You can use git!