Git Init, Status, Add, Commit and Log. These are basics and the most
used commands of the git ecosystem. It is like you learned 90% of git as these
commands are the most used commands. Well If you are using VS code and some
other that supports the git integration then you don't even need commands.
Git Working Theory
Well git works by dividing its working areas into 2 portion. First is git init to make the working folder into a git repository. then we make changes to files inside the repository/folder then it is on our working directory then we should select those files that are ready to staging area. Only those files kept in staging area is passed on for further processing. After files are staged they are saved by commiting a message. It is like a save point. This is added to git directory. now you can continue your process again and again. Here status is checked and log is checked whenever you like.
So a git file life status becomes as below figure:
Git Init
The first thing to do is to initialize the folder. In git, folder is converted
to repository so that git can now start tracking. So this is used only one
time when you make this folder's file trackable by git.
git init
right click inside the folder which you want to be trackable > more options
> open git bash here. Then you start writing commands.
Initializing git creates .git folder which is hidden.
Git Status
Well this is command to check what is status of tracking. It is for tracking
the state.
git status can be used anytime you want. Above after git init i have used git
status so it says no commits yet and nothing to commit. if it says that for
you then congratulations you have successfully initialized git.
Git Add
As mentioned in theory and concept. After you add files or update or modify
files inside your folder to start tracking you need to add each files to stage
area.
Let's create a file inside the folder then you check status and add file to
the staging area.
git add filename.extension
Above example shows then when you add a new file and check status then it
becomes red color and after you add by filename it becomes staged and tells
can be committed. But what if there are many files? One way is to seperate
file names by spaces but there are also other ways as:
or
These stages all that has been modified or created as you can see in below
example, where i created new file and also modified old file.
Git Commit
Only those files that are staged will be commited. Commit is saving or in
terms of git taking a snapshot. This is done after you stage and unlike stage
you cant choose which file to commit. So carefully stage only those files that
you want to commit. Commit is done like picking a save point in a game where
you also add a message.
git commit -m "two files added"
-m is a flag for message and "{inside this is the actual message}".
Let's now commit all that we have staged previously.
As you can see after commit the git status says working tree clean. That means
there is nothing to add or commit. If you had some files not staged then you
wont see it and that is not a problem as in a production environment you don't
staged all files and commit all at once.
Writing a good commit message is important as it allows you to see what was
the change that you made.
Git Log
So now you have committed now. You need to see what you committed then you use
this command:
there are many flags that help you get proper commits.
git log -3 => to see last three commmits
git log --oneline => oneline log of commits
...
git log is used to see logs and for many commits use down arrow to see more and to quit you simply type Q in your keyboard.
Above i have used 2 flags. git log --oneline made so that i could see commit number and the message. then the git log -2 made it possible for me to see only recent 2 commits, you can change the number like you want.
This is all for Git Basic. remember this is the most commands that you use in git so congrats on learning 90% work you do using git.
0 Comments