Git and Github: a fast sequence

devfromlegacy

Dev From Legacy

Posted on September 17, 2020

Git and Github: a fast sequence

You can practice the steps in sequence:

  • Starting: download and install Git, then you can open Git Bash.

  • Creating a repository: access a diretory with some source code (example: test.html) and type git init.

  • Adding files: at same directory, you can type: git add . . Then all files in that directory will be added.

  • Checking status: type git status and you will see the message: Changes to be committed.

  • Configure your user: execute the commands bellow changing data between quotation marks.

git config --global user.email "you@example.com"
git config --global user.name "Your Name"
  • Committing: execute the commands bellow changing message between quotation marks.
git commit -m "Your message."
  • Visualizing logs: type git log .

  • Ignoring files: you can create a empty file and it adds filename in a file called .gitigonore at same directory.

  • Starting with Github: Create a account in Github and create a new repository.

  • Adding remote repository: type code bellow changing your repository link.

git remote add origin https://github.com/YOURNAME/NAMEREPOSITORY.git
  • Pushing your code: type git push origin master and enter your login and password.

  • Cloning from Github: you can access a different directory in your computer and execute the command bellow.

git clone https://github.com/YOURNAME/NAMEREPOSITORY.git
  • Pulling from Github: you can change a file on Github page, then execute the command bellow.
git pull https://github.com/YOURNAME/NAMEREPOSITORY.git master
  • Creating new branch: git branch NAMEBRANCH.

  • Switching branch: git checkout NAMEBRANCH.

  • Committing on branch: change any file and execute commands bellow.

git add NOMEFILE
git commit -m "Changing file for commit branch test."
  • Merging: execute commands bellow.
git checkout master
git merge NAMEBRANCH
  • Rebase: change any file and execute steps bellow.
git add NOMEFILE
git commit -m "Changing file for commit master for rebase test."
git checkout NAMEBRANCH

Change any file again.

git add NOMEFILE
git commit -m "Changing file for commit branch for rebase test."
git checkout master
git rebase NAMEBRANCH
  • Reseting and restoring: change any file and execute steps bellow.
git add NOMEFILE
git reset HEAD NOMEFILE
git checkout -- NOMEFILE
  • Reverting: change any file and execute steps bellow.
git add NOMEFILE
git commit -m "Changing file for revert test."
git log

You should get the hash related the last commit.

git revert HASHLASTCOMMIT
  • Stash: change any file and execute steps bellow, the command git stath pop will remove from stash list and apply, then you can commit.
git stash
git stash pop
git add NOMEFILE
git commit -m "Changing file for stash test."
  • Switching to a different commit and creating a branch: execute steps bellow.
git log --oneline

Get a hash any past commit.

git checkout HASHPASTCOMMIT
git checkout -b NOMENEWBRANCH
  • Difference between commits: return to master git checkout master and execute steps bellow.
git log --oneline

Get two hash from any commits.

git diff HASH1..HASH2
  • Creating tag: execute steps bellow.
git tag -a VERSIONNAME -m "Creating a tag."

You can push commits and tag to Github.

git push origin master
git push origin VERSIONNAME 

Any feedback is welcome!

💖 💪 🙅 🚩
devfromlegacy
Dev From Legacy

Posted on September 17, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related