Advanced Git and GitHub Commands

advanced-git-and-github-commands

Hello there guys! in this post, we will look at advanced commands in Git and GitHub. In the last post, I wrote about basic git commands you can use.  So let’s dive into this one…

Steps

Create a Repository

1. Create a GitHub account

2. Create a repo, make it public, then initialize the repo with README, you can use gitignore to (hide certain files) and name the repo acme_project

3. In the README file you can make changes like a description of your project then click commit to update the changes

4. Create a local repo with git bash (am using vscode in the terminal) run

mkdir acme_project

using whichever drive (cd F:\) etc.

5.

cd acme_project

then run

git init 

here you will have (.git file) created inside the acme_project

Synchronizing the Repos

6. Link the repos,  run the command

git remote add origin "https link"

the “https link” is from GitHub

7. Run this command

git pull origin main

(fetches all files from the central repo to your local repo)

8. You can check what has been updated in your folder (acme_project)

Making Changes

Create a file within your folder e.g index.html (write something)

you can use the touch command within the vscode terminal command line

touch index.html

then  run

code .

to open in vs code

1. Run the

git status

(tells you which files are added to the index and are ready to commit) it also finds:
untracked files (files that have not been added to the index yet)

2. Run

git add index.html

(lets you add files to your index)

check the status again by running

git status

3. Run the

git commit -m "first commit"

(refers to recording snapshots of the repo at a given time you can commit changes to your local repo)

create more files

touch about.html (add something)
touch post.html (add something)
touch contact.html (add something)

4. Modify index.html and run

git status

5. Remember we have created three more files so we need to add the multiple files together

Run this command

git add -A

6. Run

git status

again

7. Commit all files

git commit -a -m "added 3 files"

8. To get a summary of what you have been doing Run

git log

stores all commits

Parallel Development

In this section, we are going to look at branches and how we can work with them.

Branches are pointers to a specific commit.

Branches are of two types:
Local branches
Remote-tracking branches

Steps

1. Let’s start by running this command

git branch branch1

contains all files that were in the master 

2. Run

git checkout branch1

move from one branch to another

3. Create a file in acme_project like

touch policy.html

in branch1

4. Run

git add policy.html

5. Run again

git commit -m "making changes in branch1"

6. List all the files

ls

7. Run this command

git checkout master

moving to the master branch

8. When you run

ls

again you will find there is no policy.html in the master branch coz the changes were made in the branch1

Merging

It is a way to combine the work of different branches together.

Allows to branch off, develop a new feature & combine it back in.

1. Let’s merge policy.html to the master branch

run on the master,

git merge branch1

2. Run

ls

policy.html has been added

3. if you want to make changes in a branch you can do so and come back and merge

git checkout branch1

go to policy.html (edit, write something again)

run

git commit -a -m "modified policy.html"

this file is already a track file no need to run the git add command.

Run

cat policy.html

still in the branch1

Run these two commands as follows…

git checkout master
cat policy.html

modification has not been affected in the master branch

You can run

git merge branch1

so the file will be updated and added to the master branch.

Rebasing

This is a way of combining the work between different branches.

git checkout branch1

create more files

touch services.html (edit)
touch FAQ.html (edit)
git add -A

(untracked files) Adding multiple files

git commit -a -m "adding for rebasing"
ls
git checkout master
ls
git checkout branch1
git rebase master

there were no new files to be added

git checkout master
git rebase branch1

work of branch1 has been attached to the master branch, linearly no set of new commits

ls

Push changes

You Generate an ssh key for a secure connection.

run

git checkout branch1

push your own branch separately

git push origin branch1

Go to GitHub and click on branch1. You will see new files pushed there.

git checkout master
git push origin master

Now the files have been pushed to your GitHub account.

Demo Revert to the previous file contents

Let’s say you want to revert back to the previous file contents just like undo in Microsoft office etc. This is how you can do it in Git.

create a new file

touch revert.html (edit)
git add revert.html
git commit -m "revert1"

In the revert.html (edit again add something)

git commit -a -m "revert2"
cat revert.html

Run the

git log 

copy the commit hash first 8 digits

run

git checkout

paste the first 8 digits with revert.html

cat revert.html

(takes you back to the previous version of the file)

Sign up for free tutorials in your inbox.

We don’t spam! Read our privacy policy for more info.

Leave a Comment

Your email address will not be published. Required fields are marked *