How to use Git efficiently in Code management

How to use Git efficiently in Code management

What is git??

First of all, GitHub is not git. Many people understandably confuse the two. GitHub is a website for hosting projects that use git.

Git is a free and open-source distributed version control system Initially designed and developed by Linus Torvalds for Linux kernel development in 2005.

Git’s purpose is to keep track of projects and files as they change over time with manipulations happening from different users. Git stores information about the project’s progress on a repository. All this information is stored in the same folder as the project in a sub-folder called .git and will mostly be hidden by default in most systems.

So basically, Git allows the user to have versions of a project, which show the changes that were made to the code over time, and allows the user to backtrack if necessary and undo those changes.

Now you know git, let’s see some of its features,

  • Distributed System
  • Branching
  • Compatibility
  • Lightweight
  • Open-Source
  • Secure etc

Getting Started

To get started with Git, you need to download it to your machine. Head over to https://git-scm.com/ (git's command-line interface).

Now you have git on your system, Let’s Start with some basics commands:

Below is a series of basic commands with descriptions of what they each do. As you read, feel free to try out the commands yourself before moving on.

Configuring user information used across all local repositories:

  • git config --global user.name “[firstname lastname]”? (Global level configuration)
  • git config --global user.email “[valid-email]”

set's name and email that is identifiable in the version history

Initializing repositories:

  • git init? (initialize an existing directory as a Git repository)

Cloning repositories:

  • git clone [URL]

Using SSH Key is recommended(Link).

Using .gitignore:?

A gitignore file specifies intentionally untracked files that Git should ignore like when you don't want to share particular files with other collaborators.

  • touch .gitignore? (“touch” command creates a file without any content with given name)

Open gitignore file and add file-specific patterns like *.class for adding all .class files,?folderName/ for hiding folder.

Working with Git staging area:

  • git status? (Shows tracked, untracked files, and changes to file)
  • git add -A? (Add all files to the staging area)
  • git diff

Displays changes made according to the staging area.

  • git diff --staged

Displays changes made according to the last commit.

  • git commit -m “description”? (commit changes from staging area)

Understand the concepts described in these images

No alt text provided for this image
No alt text provided for this image

Branching and Merging:

Branches in Git provide a feature to make changes in the project without affecting the original version, Any new feature can be tested and worked upon on the branches, and further, it can be merged with the main branch.

Working with Branches:

  • git branch? (List all branches)
  • git branch [branch-name]? (Creates new branch)
  • git checkout -b [branch-name]? (Creates and switch to new branch)
  • git merge [branch]??

Switch to the main branch and run this command, it will merge this branch, But conflicts may occur during merging,

Conflicts generally arise when changes are on the same lines in a file, or if one developer deleted a file while another developer was modifying it. In these cases, Git cannot automatically determine what is correct. Conflicts only affect the developer conducting the merge, the rest of the team is unaware of the conflict. Git will mark the file as being conflicted and halt the merging process.

How to solve these conflicts?

The most direct way to resolve a merge conflict is to edit the conflicted file. Open the file in your favorite editor.

Note: The ======= line is the "center" of the conflict. All the content between the center and the <<<<<<< HEAD line is content that exists in the current branch main which the HEAD ref is pointing to. Alternatively, all content between the center and >>>>>>> new_branch_to_merge_later is content that is present in our merging branch.

  • git branch -d [branch-name]? (Delete the branch only if merged)
  • git log? (show all commits in the current branch’s history)

Temporary Saving your files:

  • git stash

Temporarily saves changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. By default, running?git stash?will stash changes that have been added to your staging area.

  • git stash pop

Re-apply previously stashed changes

Updating Local repositories and getting updates:

  • git pull? (fetch and merge any commits from the remote branch)
  • git push [remote] [branch]? (git fetch + git merge)

Push the specified branch to? URL (remote repo)

You now know git and its most used commands in Software development and Source Code management. Git has some other concepts like rebasing which are also useful.

Don’t Stop here Explore more :?

Thank you, Let me know in the comments your views and any suggestions or anything I missed.

Ashish Vijaywargiya

VP of Operations at HotWax Systems | Apache OFBiz Committer, PMC Member | ASF(Apache Software Foundation) Member | Empowering College Students, Freshers & Early IT Professionals ??

3 年

Very well expressed Siddharth!! Thank you for sharing.

Palash Bajaj

Enterprise Java Developer (3+ years) || Apache OFBiz || Open Source

3 年

Useful information

Ivan Ganatra

Graduate Analyst @Deutsche Bank | ACM ICPC Regionals'22 | AIR 55 in IICC'22 | Winner @CodeMeNot Hackathon'21 | Global Rank 475 @CreditSuisseChallenge'22

3 年

I read everything, great content!!

要查看或添加评论,请登录

社区洞察

其他会员也浏览了