You are currently viewing Git, Github, and Version Control

Git, Github, and Version Control

  • Post author:
  • Post category:Open Source
  • Post comments:0 Comments
  • Post last modified:September 8, 2023
  • Reading time:13 mins read

Table of Contents

Git Commands

Part 1:

				
					git init            // Initialize Git
git status          // Check the status of tracking files and displays the untracked file(s) in red
git add FileName    // Add all the files to the staging area and start tracking the changes on them. Use dot (.) instead of the filename to specify every file in the current directory -> git add .
git commit -m "Your message" // Write about what changes you made on the file(s) - Write in present tense as a best practice
git log             // Check what commits were made
git checkout        // To revert/roll back to the last position in the git repository.
git diff FileName   // Comapre the two versions of the File
git checkout FileName  // To roll back to the previous version of the file
				
			

What is the working directory?

The working directory is the directory or folder where you initialized your git repository. where you use the “git init” command.

After initializing the git in the working directory, git starts to track the changes between the files in the working directory and the local repository

What is the staging area?

The staging area is the place where you can figure out what files you want to git to ignore and what files you want to track. We push the files to the staging area with the command “git add FileName”.

What is the local repository?

Once we are happy with the changes we made to the files, we can send them to the local repository with the “git commit” command.

Now our files are inside the local repository (.git) and that version gives it a name with the commit message.

 

Git Commits?

A commit in a git repository records a snapshot of all the (tracked) files in your directory. It’s like a giant copy and paste, but even better!

Part 2:

				
					git remote add origin TheUrlOfTheRemoteGithubRepository // Create remote repository. Origin is the name of the remote
git push -u origin main // Push the local repository files to the remote repository. Origin is the name of the remote and main is the branch of the remote. main is the default branch of the remote repository
git pull origin main // Get the latest updates from the remote repository and merge them with your local repository. Change the main to your desired branch.
				
			

Git Ignore

Create a .gitignore file in the root of the working directory to tell the git to ignore which files you don’t want to be tracked and committed.

				
					touch .gitignore        // Create a file named .gitignore
open .gitignore         // Open the file with the default editor
git rm --cached -r .    // To remove all the files in the staging area
				
			

List all the files and/or folders you don’t want to be tracked or committed, one per line:

				
					.DS_Store
secrets.txt

# Use pound sign (#) to write a comment in the .gitignore file
# Use asterisk to define wildcard rules. In the example below git ignores all the files with the .txt extention.
*.txt
				
			

You can find a collection of .gitignore templates from the following link:

Git Clone

How to clone a remote repository into your local machine?

				
					git clone TheUrlOfTheRemoteRepository
				
			

To pull down all the commits, all the versions of a particular remote repository and to store the files into your local working directory.

Git Branch

				
					git branch TheBranchName    // To create a new branch
git branch                  // To checkout what branches do you have and in which branch you are that is marked with an asterisk before the name of the branch.
git switch TheBranchName    // To switch between the branches
git merge TheBranchName     // Merge TheBranchName branch with the current branch. For example, if you are in your main branch, it'll merge TheBranchName to the main branch. After hitting the Enter, a new page will open that you can add commit to it or use :q! to exit.
git branch -m newBranchName   // To change the name of the branch
				
			

If you want to dive deeper into Git, including learning about Cherry-Picking, Git Rebase and more I recommend completing the challenges here:

 

https://learngitbranching.js.org/

The difference between Git Fork and Git Clone

Git Clone is just grabbing of the entirety of the repository and then cloning it to your local working environment. Git Fork is just coping the repository that is hosted on GitHub and keeping that copy on your own GitHub account where you can make changes to it. When you fork a repository, you will own it.

How to change a commit message of project on GitHub?

  1. Clone the project on your local repository.
  2. Go through your cloned project folder.
  3. Type git commit –amend and press Enter.
  4. In the text editor, press i to enter inline insert mode. Type or change the description at the very top.
  5. press esc to exit insert mode.
  6. Type :wq and hit enter to save and exit the editor. w is for “write” (save) and q is for “quit”) or
    Type :x! and hit enter to save and exit.
    (now the cursor is at the bottom)
  7. Check the changes that have made on the commit message by typing git log
  8. Type git push –force origin main and press Enter.
  9. Done!

How to change the URI (URL) for a remote Git repository?

				
					git remote set-url origin new.git.url/here
				
			

How to undo a git add

To undo a git add before a commit

				
					git reset <file>    // to undo add a specific file
git reset           // to undo all added files
				
			

Git Cheatsheet (Free PDF Download)

Git cheatsheet

Leave a Reply