- Cloning a Git repository:
git clone https://github.com/username/the_repo_name.git
NOTE
: Make sure to copy the url of the repo you want to clone.
- Initialize a Git repository:
git init
- Initialize a new repository and set the default branch to main:
git init -b main
NOTE:
Feel free to change main
into the branch name of your choice
- Add files to the staging area:
git add .
NOTE
: Works for new files that have not been added to Git.
- Unstaging a specific file:
git reset filename
- Commit the files to Git:
git commit -m "<message>"
NOTE
: Replace <message>
with your own message. Ensure the commit message is less than 50 words.
- Add and commit modified files together:
git commit -a -m "<message>"
- Updating your last Git commit:
git commit --amend -m 'message'
- Making a commit after modifing a committed file:
git commit -m "<message>" filename
- Pushing changes to the repo:
git push -u origin main
- Check status of the working tree:
git status
- Check more information about previous commits:
git log
- Check information about previous commits in a more concise listing:
git log --oneline
- Check information on the latest commit made:
git log -n1
- Track changes made on a file:
git diff
- Enter your GitHub username:
git config --global user.name "<USER_NAME>"
NOTE
: Replace <USER_NAME>
with your username.
- Enter your email address used in GitHub:
git config --global user.email "<USER_EMAIL>"
NOTE
: Replace <USER_EMAIL>
with your email address.
- Check the changes made:
git config --list
- Ever been in a situation where you forgot to add a file after making a commit? Here is a simple way to fix that:
git commit --amend --no-edit
- Recover a deleted file using
git checkout
:
git checkout -- filename
NOTE:
git checkout --filename
can also be used to revert a file to it's last committed state.
- Recover a deleted file after accidentally running
git rm
:
git reset HEAD filename
git checkout -- filename
NOTE:
git checkout
updates your files with the present one in the git index while git reset
unstages deleted files.
- Revert a Commit:
git reset --hard HEAD^
- Revert a commit without adding a commit message:
git revert --no-edit HEAD
- After modifing a file and committing it, make a pull request to the original repo:
git request-pull -p origin/main .
- Create a remote on your local machine:
git remote add remote-username repo_url_path
- Merge pull request changes into original repo:
git pull remote-username main
- Merge changes into the current branch
main
:
git pull
- Create a new branch:
git branch branchname
- Create a new branch and switch to it:
git checkout -b branchname
- Switch to another branch:
git checkout branchname
- List the available branches:
git branch -a
- Rename a branch:
git branch -m old_branchname new_branchname
-
Delete a branch:
- Safe Deletion (Checks for merge):
git branch -d branchname
- Force Deletion (Doesn't check for merge):
git branch -D branchname
-
Merge a specific branch such as
branch1
into the current branch such asmain
:
git merge branch1
- Restore the current branch
main
before the attempted merge:
git merge --abort
- Return back to the original state of the current branch
main
before the completed merge:
git reset --hard
- Stash changes:
git stash
NOTE:
git stash
helps in temporarily saving changes that are not ready to be committed (Incomplete work) which helps in switching branches.
- Recovering stashed changes after switching back to the original branch:
git stash pop
- Listing stashes:
git stash list
- Cleaning up the stash:
git stash clear
- Revert a specific git commit:
git revert commitHash
NOTE:
commitHash
refers to the commit ID
of the git commits. Replace it with the correct commitHash
.
- List commitHashes of git commits made:
git reflog
- Soft Reset:
git reset --soft HEAD^
NOTE:
It easily allows to backtrack last commit but preserves the changes in the staging area.
- Mixed Reset:
git reset --mixed HEAD^
NOTE:
It uncommits the last commit and removes it's changes from the staging area.
- Hard Reset:
git reset --hard HEAD^
NOTE:
It completely erases the last commit along with associated changes from the Git history.
- List all the Git tags:
git tag
- Create Git tags for a project:
git tag -a tagname -m "Release tagname"
- View tag data:
git show tagname
- Push one tag:
git push origin tagname
- Push multiple tags:
git push origin --tags
- Deleting a tag:
git tag -d tagname
- Check Git version installed:
git --version
- Get help from Git:
git --help
- Deleting files in Git:
git rm filename
-
Ever found yourself in a situation you need to gitignore files or folders you have committed? Follow these steps:
-
Create the .gitignore file and list the files or folders to be gitignored:
touch .gitignore nano .gitignore vim .gitignore emacs .gitignore
N/B
: Use one of the commands that will work on your machine. -
After listing, remove all tracked files from the Git index:
git rm -r --cached .
-
Add the files to the staging area:
git add .
-
Commit the changes:
git commit - m "commit message"
-
Push the changes to the repo:
git push
-
-
Ever found yourself that you have initialized a project and you need to connect it to github repo? Follow these steps:
-
Copy the URL of the GitHub repo.
-
Add the URL for the remote repo where your local repo will be pushed:
git remote add origin <remote_repo_URL>
- Verify that the remote URL has been added:
git remote -v
- Push the changes to the remote repo:
git add . git commit -m "commit_message" git push origin main
-
-
Ever found yourself that you wanted to gitignore a file/ folder but you have already pushed the commit? Follow these steps:
- Open the terminal and type:
git rm -r --cached filename/ foldername
- Commit and push the changes:
git commit -m "Removed filename/ foldername" git push origin main
-
git rm -r --cached filename/ foldername
removes the filename/ foldername from the repository but will still exist in your local system. -
Now you can finally gitignore the filename/ foldername and push the commit made.