Table of Contents
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.
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!
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.
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:
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 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.
If you want to dive deeper into Git, including learning about Cherry-Picking, Git Rebase and more I recommend completing the challenges here:
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?
- Clone the project on your local repository.
- Go through your cloned project folder.
- Type git commit –amend and press Enter.
- In the text editor, press i to enter inline insert mode. Type or change the description at the very top.
- press esc to exit insert mode.
- 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)
- Check the changes that have made on the commit message by typing git log
- Type git push –force origin main and press Enter.
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 // to undo add a specific file git reset // to undo all added files