Using Git 1

Vignesh Muthukumaran - Aug 12 '23 - - Dev Community

Git

Git is a source control program. It can be used to,

  • Version control (maintain all version of a project)
  • Revert to older version if something breaks
  • Collaborate with people (allowing multiple people work on a single project)
  • Track all the changes done by multiple people

In this article, we just explore all the features we can use locally.

Using Git

Create a git repo

The way to create a git repo is to run the git init command in the project folder

git init
Enter fullscreen mode Exit fullscreen mode

Git will create a folder called .git in the current working directory.

Start tracking

From now on, all the changes done in the folder will be tracked. Try adding a file and just check it out.

git status
Enter fullscreen mode Exit fullscreen mode

This will show unstaged files i.e files not yet committed in red, committed files in green.

Commiting the changes

If we commit the changes, it will create a snapshot of the version. But before that the changes need to be added to staging.

git add .
Enter fullscreen mode Exit fullscreen mode

To add the files to staging using the above command. Now git status will return the files in green. Now the changes can be committed.

git commit -m "Changes done"
Enter fullscreen mode Exit fullscreen mode

This will now create a snapshot with the commit.

Tracking changes

To checkout the history of the changes, we can use git log.

git log
Enter fullscreen mode Exit fullscreen mode

Reverting a commit

To revert to an older version, we choose the version needed from git log and use git reset to go back to previous commit. This will revert the changes and unstage the changes done as well.

git reset $prev_commit_hash
Enter fullscreen mode Exit fullscreen mode

A git status should show us the changes done after the reverted hash in red.

If we use the flay --hard, it removes the files as well from the working directory, so use with caution if required.

git reset --hard $prev_commit_hash
Enter fullscreen mode Exit fullscreen mode

Stash changes

Suppose we need to temporarily move the changes and get it to previous committed version, we can use git stash.

git stash
Enter fullscreen mode Exit fullscreen mode

To bring back from stash, we can bring it back using git pop

git stash pop
Enter fullscreen mode Exit fullscreen mode

We can stash and remove the changes as well.

git stash clear 
Enter fullscreen mode Exit fullscreen mode

To remove just the top of stash changes we can use git stash drop

git stash drop
Enter fullscreen mode Exit fullscreen mode

Getting the changes back after a git reset --hard

There is another set of logs in git called reference logs. The command to access reference logs is reflog

git reflog
Enter fullscreen mode Exit fullscreen mode

We can use this to checkout a branch from here to an older before a hard reset was done to get the changes.

In part 2, we will explore the feature we can use to collaborate.

. . . . . . . . . . . . . . . . . . .