Git
Git is a distributed version control system that helps track and manage changes in code over time. It allows multiple developers to work on the same project, view previous versions, and restore changes when needed.
Features of Git
Version Control System: Git keeps track of every change you make to your project files. You can go back to previous versions is any requirement arises.
Repositories: A Git repository (or repo) is like a project's central hub where everything related to your work is stored and managed. There are two main types:
- Local Repository: This is a copy of the repository which is stored in your computer. You can work on your project and make changes here.
- Remote Repository: This is stored on a server, like GitHub, where you and others can share and work on a project.
Commits: Every time you make changes and save them in Git, these are called commits. The repository keeps track of all the commits you have made.
Branches: A repository allows you to create Branches and work on new features and fixes. For example, you can have a "main" branch for stable code and a "feature" branch for new features you're developing.
Merging: Once you are done with the branches you have made, you can merge those branches into main branch. This adds the changes you have made in the rest of the project.
Cloning: Cloning is a process of making a complete copy of Git repository. It's like copying the entire project from central location to your own computer.
Why Git?
- Over 70% of developers use Git!
- Developers can work together from anywhere in the world.
- Developers can see the full history of the project.
- Developers can revert to earlier versions of a project.
Introduction to GitLab
GitLab is a web-based DevOps platform that enables teams to manage the entire software development lifecycle in a single application. It combines version control with built-in tools for automation, collaboration, and deployment.
Common Git commands
git add
Use git add to files to the staging area.
You can recursively stage changes from the current working directory with git add ., or stage all changes in the Git repository with git add --all.
git add <file_path>
git pull
Use git pull to get all the changes made by users after the last time you cloned or pulled the project.
git pull <optional_remote> <branch_name>
git push
Use git push to update remote refs.
git push
git remote add
Use git remote add to tell Git which remote repository in GitLab is linked to a local directory.When you clone a repository, by default the source repository is associated with the remote name origin.
git remote add <remote_name> <repository_url>
git merge
Use git merge to combine the changes from one branch with another.
git merge
git status
Use git status to show the status of the working directory and staged files. When you add, change, or delete files, Git can show you the changes.
git status
git clone
Use git clone to copy an existing Git repository.
git clone <repository>
git blame
Use git blame to report which users changed which parts of a file. You can use git blame -L <line_start>, <line_end> to check a specific range of lines.
git blame <file_name>
git bisect
Use git bisect to use binary search to find the commit that introduced a bug. Start by identifying a commit that is “bad” (contains the bug) and a commit that is “good” (doesn’t contain the bug). git bisect then picks a commit in between the two points and asks you identify if the commit is “good” or “bad” with git bisect good or git bisect bad. Repeat the process until the commit is found.
git bisect start
git bisect bad # Current version is bad
git bisect good v2.6.13-rc2 # v2.6.13-rc2 is known to be good
git checkout
Use git checkout to switch to a specific branch. To create a new branch and switch to it, use git checkout -b <branch_name>.
git checkout <branch_name>
git commit
Use git commit to commits staged changes to the repository. If the commit message contains a blank line, the first line becomes the commit subject while the remainder becomes the commit body. Use the subject to briefly summarize a change, and the commit body to provide additional details.
git commit -m "<commit_message>"
git commit --amend
Use git commit --amend to modify the most recent commit.
git commit --amend
git diff
Use git diff to view the differences between your local unstaged changes and the latest version that you cloned or pulled. You can display the difference (or diff) between your local changes and the most recent version of a branch. View a diff to understand your local changes before you commit them to the branch.
git diff
git diff <branch> #To compare your changes against a specific branch
In the output:
Lines with additions begin with a plus (+) and are displayed in green. Lines with removals or changes begin with a minus (-) and are displayed in red.
git init
Use git init to initialize a directory so Git tracks it as a repository. A .git file with configuration and log files is added to the directory. You shouldn’t edit the .git file directly. The default branch is set to main. You can change the name of the default branch with git branch -m <branch_name>, or initialize with git init -b <branch_name>.
git init
git log
Use git log to display a list of commits in chronological order.
git log
git reset
Use git reset to undo a commit and rewind the commit history and continue on from an earlier commit.
git reset

Top comments (0)