Let's Git Started!

Git is a powerful and widely-used version control system that allows developers to track changes in their code and collaborate on projects more effectively. With Git, you can keep a complete history of your codebase, revert to previous versions, and collaborate with other developers with ease. Whether you're working on a small personal project or a large team effort, Git offers a flexible and reliable solution for managing your code and ensuring that every iteration is captured and saved. Its popularity and widespread adoption have made it an essential tool for developers, making it a must-know for anyone looking to work in the tech industry.

Configuring Git

Before doing anything with Git, it it wise to configure your global user information first.

Set global username and email address

git config --global user.name "FIRST-NAME LAST-NAME"; # Sets up username git config --global user.email "EMAIL@EXAMPLE.COM"; # Sets up email address

Optionally, you can configure your repository-specific user information as well.

Set global username and email address

git config user.name "FIRST-NAME LAST-NAME"; # Sets up username git config user.email "EMAIL@EXAMPLE.COM"; # Sets up email address cat .git/config; # Verifies configuration by displaying your config file

Creating repository

There are two main ways to start a new local Git repository:

Option 1: Create a new local repository

cd ~/PROJECT-DIRECTORY/; # Moves to project directory git init; # Creates local Git repository git add .; # Adds all files in directory to repository git commit -m 'COMMIT-NAME'; # Makes initial commit git remote add origin git@github.com:ACCOUNT-NAME/REPOSITORY-NAME.git; # Add remote repository to local repository git remote -v; # Verifies the new remote URL git push -u origin main; # Pushes commit to remote repository
If you haven't created an empty repository on github yet:
Create an empty remote repository on Github.

Option 2: Clone an existing remote Github repository

cd ~/PATH_TO_PARENT_DIRECTORY/; # Moves to parent directory git clone https://github.com/USERNAME/REPOSITORY-NAME.git; # Clones remote repository into local repository

Committing changes

Committing changes is the process of recording changes to the repository's history. Commits serve as snapshots of your project at a given point in time and allow you to track changes and revert to earlier versions of your project if necessary.

Commit changes and push them to remote repository

git checkout BRANCH-NAME; # Moves to specific branch. git add .; # Adds all files to git (staging). git commit -m "COMMIT-MESSAGE"; # Commit changes to local repository. git push origin BRANCH-NAME; # Pushes the commit to remote repository
Please check this out for best practises:
Commit message conventions.

Changing latest commit

In Git, it is possible to make changes to previous commits in your repository. This is a valuable feature that enables you to correct mistakes or make updates to your code history, ensuring that your repository remains organized and up-to-date. Whether you need to fix a typo in a commit message, or want to combine several small commits into a single larger one, the ability to modify previous commits allows you to shape your repository's history in a way that best represents your work and makes it easier to collaborate with others. With this feature, you can maintain a clear and accurate record of the evolution of your code over time.

Correct a small mistake by amending

git checkout BRANCH-NAME; # Moves to specific branch. git commit --amend -m 'NEW_COMMIT_MESSAGE'; # Amends latest commit in branch with new commit message.
Only use the 'git commit --amend' command, when the commit isn't pushed to the remote repository yet! If someone has already pulled the previous commit and then you use 'git commit --amend' to change it, you'll create a conflict that can be difficult to resolve.
If don't need to change the commit message, use the --no-edit flag:

git commit --amend --no-edit; # Amends latest commit without changing commit message.

Git reset and git revert are two important commands in Git version control system used to manage changes in the codebase. Git reset allows you to reset the current branch head to a specific commit, effectively "rolling back" the project history to a previous state. On the other hand, Git revert is used to undo a specific commit by creating a new commit that reverses the changes made in the previous commit. Both Git reset and Git revert allow you to modify the project history, but they do so in different ways, so it's important to understand the difference between the two before using them.

Option 1: Remove latest commit from local and remote repository with revert

git checkout BRANCH_NAME; # Moves to correct branch. git revert HEAD; # Creates new commit to undo all changes of last commit. git push origin BRANCH_NAME; # Pushes new commit to remote repository.

Option 2: Remove latest commit from local and remote repository with reset

git reset --hard HEAD~1; # Removes latest commit from local repository. git push origin HEAD --force; # Removes latest commit from local repository.
Be cautious when using 'git push --force' as it permanently rewrites the history of the branch and can lead to data loss if other people have cloned the repository. A safer way to undo a commit is reverting the commit.

Branches

Rename local and remote repository

git branch -m OLD-NAME NEW-NAME; # Renames local branch git push origin -u NEW-NAME; # Creates new remote repository with new name git push origin -d OLD-NAME; # Removes remote branch