GitHub key points — part 3

Lamai Anthony
3 min readApr 19, 2023

--

GitHub common commands

In earlier parts of this series, we considered how to set up a GitHub free account and authenticate to GitHub from a local Linux machine. In this section, we will focus on common GitHub commands to get you started on pushing and committing your code to the GitHub remote master repository. We assume that Git has been installed on the local machine. Click here to get a guide on how to install Git.

  1. To install Git on Red-Hat Linux and Debian-based distros respectively.
sudo yum install git
sudo apt install git

2. Create a folder on your local computer and run the command below to initialize the folder. Briefly, it transforms the current directory into a Git repository.

Git init

3. Configure your Git username and email using the following commands

git config --global user.name "John Doe"
git config --global user.email "JohnD@email.com"

4. In case the repository has already been created on GitHub, use the command below to clone the repo to your local working directory. The single branch flag ensures that only the main branch is accessed, and the depth flag clones the latest revision.

git clone --single-branch -b main --depth 1 https://GitHubURL.git

5. To add files to the staging area for future commit, use the command below. The all or A flag adds all the files within the current directory and subdirectory. You can also add a particular file to be added to the staging area by specifying the file name.

git add --all

or

git add -A
git add <filename>

6. Committing your code to the local repo is like saving a snapshot of your repository at a point in time. It moves the code from the staging area to the local repo. Following best practices, it is recommended to add a commit message using the m flag.

git commit -m "commit message"

7. To add a remote repo URL alias, use the git remote add command on the terminal, in the directory your repository is stored in.

git remote add <alias name> git@github.com:User/UserRepo.git

8. Upload commits from your local repository to your remote repository

git push -u <alias name> <branch>

9. To check the status of your repository files to verify whether or not they are tracked and staged.

git status

10. View details of your commit history can be done by using the command

git log

11. Developers use branches to manage their code. For example, a repo can have a main branch, a development branch, and a staging branch. To view, create, delete, and checkout to a branch, use the following commands:

git branch

git branch <branch-name>

git branch -d <branch-name>

git checkout <name-of-your-branch>

12. To undo a commit, you can use the revert command, but first ensure that you know the commit ID by running git log first.

git revert <commit ID number>

13. To copy changes made from a feature branch to your main branch, you can use the rebase command. It takes all the changes that were committed on one branch and replays them on a different branch. For example, say you have a main branch and a feature branch. You have made changes on the feature branch, and you want to copy those changes to the main branch. You can checkout to the feature branch and then execute git rebase main.

git rebase <branch name>

14. Another option to copy changes from one branch to another is to use the merge command. You must be in the branch you want to merge into and call the branch you want to merge from. If you want to merge a feature branch to your main branch, checkout to main, and then run the git merge feature.

git merge <branch name>

15. Git tags are metadata or human-readable references added to a git commit. Hence, instead of using a long commit ID, you can use a tag as a reference.

git tag <tag name>

--

--

Lamai Anthony

Technical Engineer passionate about continuous learning and evolution. Always surfing the net and trying projects in search of new ideas and perspectives