04 Configuring Git#

Goal#

To configure the Git version control software on your device, learn basic commands and how to use git.

Prerequisites#

Steps#

1. Introduction#

Why use Git? Git is the most widely used and best version control system in the world, including:

  • Version control for tracking changes in code and documentation

  • Facilitates collaborationg with a clear history of changes

  • Enables experimentation with features using branches

  • Supports non-linear development through branching

  • Seamless integration with GitHub

2. Setup#

Before starting you will need to download Git from the official website.

  1. Download Git

  2. Follow the installation instructions for your operating system

  3. Verify installation running git --version on your terminal

3. Execution#

3.1 Configuring Git#

Set up your name and email (these should be your GitHub credentials)

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

You can check the configuration by running git config --list.

You may also add further configuration details and information, such as your editor or diff tool, if you want to customise your Git experience.

4. Git Commands#

4.1 Basic Git Commands#

  • git init: Initializes a new Git repository in the current directory.

  • git clone [url]: Clones a repository from a given URL.

  • git add [file]: Stages a file for the next commit.

  • git add .: Stages all changes in the current directory and subdirectories.

  • git commit -m "[message]": Commits changes with a meaningful commit message.

  • git log: Displays a log of all commits made to the repository.

  • git status: Displays the status of the repository, including staged and unstaged changes.

4.2 Branching and Merging#

  • git branch [branch-name]: Creates a new branch.

  • git checkout [branch-name]: Switches to a different branch.

  • git merge [branch-name]: Merges changes from another branch into the current branch.

  • git branch -d [branch-name]: Deletes a branch.

4.3 Remote Repositories#

  • git remote add [name] [url]: Adds a remote repository.

  • git fetch [remote-name]: Fetches changes from a remote repository.

  • git push [remote-name] [branch-name]: Pushes changes to a remote repository.

  • git pull [remote-name] [branch-name]: Pulls changes from a remote repository and merges them into the current branch.

4.4 Undoing Changes#

  • git reset [file]: Unstages a file.

  • git reset --hard: Discards all changes and resets the repository to the last commit.

  • git revert [commit-hash]: Reverts a specific commit.

4.5 Other Commands#

  • git diff: Displays differences between the current version and the last commit.

  • git tag [tag-name]: Creates a new tag.

  • git --all: Displays a graphical representation of the commit history.

Next Steps#

Git allows you to organise and control your files and repositories on your local machine, but you are in a collaboration and will need to share your thoughts! That’s why you should continue learning about 05_github.