Skip to content

Latest commit

 

History

History
265 lines (168 loc) · 3.5 KB

Commands.md

File metadata and controls

265 lines (168 loc) · 3.5 KB

Git Basic Commands:

1. Configure Git:

#Configuring the username
git config --global user.name "first_name last_name"


#Configuring the userEmail
git config --global user.email "[email protected]"


2. Initialize an empty git repository:

#Initialise git
git init


3. Staging the file:

#Add a single file
git add <file_name>


#Add all the files in the current directory and any sub-directories
git add .


#Add all the files in the entire repository
git add -A


4. Commit the staged files:

#Create a new commit for the staged files
git commit -m "commit message"


#Skipping the staging area
git commit -a -m "commit message"


#Connect to the last commit
git commit --amend


5. Check status:

#Show status
git status


#Show short status
git status -s


6. Check commit history:

#List of commits
git log


#-p or -- patch shows the difference introduced in each commit. -2 for 2 log entries
git log -p -2


7. Check unstaged changes:

#Compare unstaged changes to staged changes
git diff


#Compare staged changes to the last commit
git diff --staged
git diff --cached


8. Removing files:

#Stop tracking of file
git rm <file_name>


#Remove the file from the staging area
git rm --cached <fiel_name>


9. Moving files:

#Move from source to destination
git mv <source> <destination>


10. Unstaging a staged file:

#Remove the file from the staging area
git reset HEAD <file_name>


11. Unmodifying a modified file:

#Single file
git checkout -- <file_name>


#All files
git checkout -f


Git Branching Commands:

1. Creating a branch:

#Creates a branch with the given name
git branch <branch_name>


2. Switching to a branch:

#Switches to the given branch
git checkout <branch_name>


3. Listing the branches:

#List all the branches
git branch <branch_name>


4. Creating and Switching to a branch:

#Creates a branch and then switches to that branch
git checkout -b <branch_name>


5. Renaming a branch:

#Renames the branch from <old_name> to <new_name>
git branch -m <old_name> <new_name>


6. Deleting a branch:

#Deletes the branch
git branch -d <branch_name>


Remote Repositories:

1. Adding a remote:

#Adds the remote to our machine by naming the remote <short_name>
git remote add <short_name> <url>


2. View remote links:

#show the remotes
git remote


#Show the remotes with the links
git remote -v


3. Cloning a repository:

#Make a copy of a remote repository and initialize git
git clone <remote_path>


4. Getting contents of the remote repository to the local repository:

#Fetch the content from the remote repository to the local repository
git fetch <remote_name>


#Fetch and merge the content in the local repository
git pull <remote_name>


5. Pushing to your remotes:

#Push the changes in the local repository to the remote repository
git push <remote_name> <branch_name>


6. Renaming and Removing remotes:

#Rename a remote
git remote <old_name> <new_name>


#Remove a remote
git remote remove <remote_name>