Git and GitHub are essential tools for modern software development, enabling version control and collaborative work on code. This guide will walk you through the steps to get started with Git Commands But if you don’t know what is Git and GitHub then you should read this article 1st click here to read
Table of Contents
Step 1: Install Git & Create an Account on GitHub
- Before you can start using Git, you need to install it on your computer. You can download Git from the official website.
- Go to GitHub Signup.
Step 3: Setting Up Your Project
After installing Git, you should use Git Bash to navigate and manage your project. Open Git Bash in your project folder and follow these steps to create and initialize a new project.
Quick Guide: Navigating Your Project Folder in Git Bash
- See the current location:
$ pwd
The pwd
(print working directory) command displays the current directory path.
- List the files and folders in the current directory:
$ ls
The ls
command lists the files and folders in the current directory.
- List all files, including hidden ones:
$ ls -a
The ls -a
command lists all files, including hidden files (those starting with a dot).
- Change directory:
$ cd <directory_name>
The cd
(change directory) command moves you into the specified directory.
- Go back to the previous directory:
$ cd ..
The cd ..
command moves you up one directory level.
Setting Up Git Configuration
- Verify Git installation:
$ git --version
This command checks if Git is installed and displays the installed version.
- Set your user name and email:
$ git config --global user.name "sagar"
$ git config --global user.email "sagar@gmail.com"
These commands set your user name and email, which will be associated with your Git commits.
- Edit global configuration directly:
$ git config --global --edit
This command opens the global Git configuration file in the default text editor. To exit the editor, press ESC
, then type :wq
and press Enter
.
Initialize a Git Repository
- Navigate to your project folder:
$ cd gitlearneasy
Use cd
to change the directory to your project folder.
- Initialize an empty Git repository:
$ git init
This command creates a new Git repository in your project folder.
- List files in the directory to verify repository creation:
$ ls
$ ls -a
ls
lists the files in the directory, and ls -a
includes hidden files, such as the .git
directory created by git init
.
Step 4: Create and Commit Files
- Create a file named
Test.java
and add some code to it. - Check the status of your files:
$ git status
This command shows the status of the working directory and staging area. It displays which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
- Add
Test.java
to the staging area:
$ git add Test.java
This command stages changes to be committed. Staging is like preparing a package to send.
- Verify the status again:
$ git status
This will show that Test.java
is now staged.
- Commit the file to the local repository with a message:
$ git commit -m "first comment"
The commit
command records the changes to the repository. The -m
option lets you add a commit message inline.
- View your commit history:
$ git log
This command shows the commit history. Each commit will have a unique hash code.
Adding More Code
- Modify
Test.java
and stage the changes:
$ git add Test.java
$ git commit -m "second comment"
These commands stage and commit the changes made to Test.java
.
- To add all changes in the project at once:
$ git add .
$ git commit -m "third comment"
git add .
stages all changes in the working directory, and git commit -m "message"
commits them with a message.
Step 5: Reverting Changes
Sometimes, you may need to revert your project to a previous state. This can be done using the commit hash code. Here’s how you can navigate through your commit history and revert to a specific point.
Checkout a Previous Commit
- View your commit history:
$ git log
This command displays the commit history, showing commit hashes, authors, dates, and commit messages.
- Switch to a previous commit:
$ git checkout <commit HASH code>
Replace <commit HASH code>
with the specific hash of the commit you want to revert to. This command changes the working directory to match the state of the specified commit. Note that this puts your repository into a “detached HEAD” state, meaning you are not on any branch.
Example:
$ git checkout bad9c532fa5188e13a539901a0222f9df06ad650
In this example, the working directory is set to the state of the commit with the hash bad9c532fa5188e13a539901a0222f9df06ad650
.
Working in Detached HEAD State
While in this state, you can explore the project as it was at the specific commit. However, any changes you make won’t be reflected in any branch until you create a new branch or switch back to an existing branch and merge the changes.
- Create a new branch from this state (optional):
$ git checkout -b new-branch-name
This command creates a new branch and switches to it, allowing you to preserve the state from the specific commit and continue working from there.
Return to the Latest Commit on the Master Branch
- Switch back to the master branch:
$ git checkout master
This command returns you to the latest commit on the master branch, leaving the detached HEAD state.
- Update your master branch if needed:
$ git pull origin master
This command updates your local master branch with any new changes from the remote repository, ensuring you have the latest version of the project.
By understanding how to revert to previous commits and work with the commit history, you can effectively manage and recover your project’s state at any point in time.
Step 6: Working with Branches
Creating and Switching Branches
- List all local branches:
$ git branch
This command lists all the local branches in the repository.
- Create a new branch:
$ git branch dev
This command creates a new branch named dev
.
- Switch to the new branch:
$ git checkout dev
This command switches to the dev
branch.
- Create and switch to a new branch in one step:
$ git checkout -b ram/teacher
The -b
option creates a new branch and switches to it.
Making Changes in a New Branch
- Create a new file
Teacher.java
and add some code. - Stage and commit the new file:
$ git add Teacher.java
$ git commit -m "add teacher class"
Merging Branches
- Switch back to the
dev
branch:
$ git checkout dev
- Merge changes from
ram/teacher
intodev
:
$ git merge ram/teacher
- To merge
dev
intomaster
:
$ git checkout master
$ git merge dev
Step 7: Pushing to GitHub
- Log in to your GitHub account and create a new repository named
gitlearneasy
. - Add a description if desired and create the repository.
Connecting Local Repository to GitHub
- Back in your project directory, check for connected remote repositories:
$ git remote -v
This command shows the URLs of remote repositories.
- Add the GitHub repository as a remote:
$ git remote add origin https://github.com/sagar/Student_Management_cpp.git
This command adds a remote repository named origin
.
- Rename the default branch to
main
if necessary:
$ git branch -M main
This command renames the current branch to main
.
- Push your local repository to GitHub:
$ git push -u origin main
This command pushes the local changes to the main
branch of the remote repository.
Resolving Push Rejections
If you encounter a rejection error, integrate remote changes before pushing:
- Pull and rebase the remote changes:
$ git pull --rebase origin main
This command fetches and integrates changes from the remote repository.
- Push the local repository to GitHub:
$ git push origin main
By following these steps, you’ll have a solid foundation in using Git and GitHub for version control and collaboration. As you become more comfortable, you can explore additional features and workflows to enhance your development process. Happy coding!