Git is my latest obsession. A great resource that I've been tearing through is the Pro Git book written by Scott Chacon and Ben Straub.
You can find a free download for the book here:
https://git-scm.com/book/en/v2
Git is a version control system that helps developers track changes made to their code over time. It is a powerful tool for collaboration and ensuring that code changes are made safely and efficiently. Originally coded by Linus Torvalds, also the creator of the Linux Operating System, he referred to Git as "the stupid content tracker". While there is a steep learning curve for Git and some limitations, overall it's a great soultion.
Some of the basics of Git that must be learned are:
To start using Git, you need to create a repository. A repository is a directory where your code and version history will be stored. To create a new repository, navigate to your project directory in the terminal of your own local machine and use the git init command:
This command initializes an empty Git repository in the current directory.
To collaborate with others on your project, you can connect your local repository to a remote repository hosted on a Git host such as GitHub. To do this, you need to create a new repository on Github and obtain its URL.
The steps to create a repository on Github:
Then, use the git remote add command to connect your local repository to the remote repository:
This command adds a new remote named origin and sets its URL to the URL of your remote repository. Now you can add and push changes from your local repository to the remote repository.
Before you can start tracking changes in Git, you need to add files to the repository. To add a file to the repository, use the git add command followed by the name of the file:
You can also add all files in a directory by specifying the directory name:
Once you have added files to the repository, you are ready to commit them. Committing is the process of saving changes to the repository with a commit message that describes the changes you have made. To commit changes, use the git commit command:
The -m option allows you to add a commit message that describes the changes you have made.
Once you have committed changes to your local repository, you can push those changes to your remote repository on GitHub. In Git, a branch represents an independent line of development, and by default, the first branch is named master. It is important to use branches to isolate changes, experiment with new features, and collaborate on code without affecting the main branch. Use the git push command followed by the remote name and the branch name to push your changes:
This command pushes the changes in the master branch of your local repository to the origin remote repository on GitHub.
If changes have been made to the remote repository on GitHub by other team members, you can pull those changes to your local repository using the git pull command:
This command pulls changes in the master branch of the origin remote repository on GitHub to your local repository.
At any point in time, you can view the status of your repository using the git status command. This command shows you which files have been modified and which files are not tracked:
You can find the code from the examples above in one file here:
https://github.com/CodingInGreen/git-basics