-
Download the latest Git for Windows installer.
-
When you've successfully started the installer, you should see the Git Setup wizard screen. Follow the Next and Finish prompts to complete the installation. The default options are pretty sensible for most users.
-
Open a Command Prompt (or Git Bash if during installation you elected not to use Git from the Windows Command Prompt).
-
Run the following commands to configure your Git username and email using the following commands, replacing Emma's name with your own. These details will be associated with any commits that you create:
$ git config --global user.name "Pi Innovations"
$ git config --global user.email "sushantshah@pi-innovations.com.np"
To check the configured username and user email in your Git configuration,
git config user.name
git config user.email
- From your shell, install Git using apt-get:
$ sudo apt-get update
$ sudo apt-get install git
OR
$ sudo dnf install git
- Verify the installation was successful by typing git --version:
$ git --version
git version 2.9.2
- Configure your Git username and email using the following commands, replacing Emma's name with your own. These details will be associated with any commits that you create:
$ git config --global user.name "Emma Paris"
$ git config --global user.email "eparis@atlassian.com"
To check the configured username and user email in your Git configuration,
git config user.name
git config user.email
Install Git with Homebrew
If you have installed Homebrew to manage packages on OS X, you can follow these instructions to install Git:
- Open your terminal and install Git using Homebrew:
$ brew install git
- Verify the installation was successful by typing which
git --version
:
$ git --version
To check the configured username and user email in your Git configuration,
git config user.name
git config user.email
List Files and Directories
ls
Change Directory
cd <directory-path>
Create a Directory
mkdir <directory-name>
Create a File
touch <file-name>
Remove (Delete) a File
rm <file-name>
Remove (Delete) a Directory
rm -r <directory-name>
Copy File
cp <source-file> <destination-file>
Move/Rename File or Directory
mv <source> <destination>
Display File Content
cat <file-name>
Edit a File
nano <file-name>
Print Working Directory
pwd
To create a new repo, you'll use the git init
command.
git init is a one-time command you use during the initial setup of a new repo.
Executing this command will create a new .git
subdirectory in your current working directory. This will also create a new main branch.
cd /path/to/your/existing/code
git init
To check the status of your repository you will use the command git status
.
git status provide the status of any track and untrack files in your repository.
git status
To stage the changes, you will use the git add
command.
git add is a command you will use to track the changes so that you can commit in the next commit. git add allows git to keep track of the changes that you are making in the files. Here we can conclude that staging a changes is a way of keep track of changes by git that you desire and make in the files.
git add 'filename'
To commit the changes that you have staged using git add
command, you will use git commit
command.
While commiting the changes you need to also include the logical commit message that provides clear insight of what changes that you committed in this very commit using flag -m
as:
git commit -m 'commit message'
Use the git commit
command to create a commit.
This opens a text editor for you to write a commit message.
git commit
To unstage specific files, you can use the command git reset
git reset <file1> <file2>
To unstage all changes, you can use git reset
as
git reset
To unstage and discard the changes in your working directory
git reset --hard
To see a list of commits, you can use the git log
command. This command displays information about each commit, including the commit hash, author, date, and commit message.
git log
To view a more condensed log,
git log --oneline
To see a list of branches and their last commit,
git branch -v
To view the changes introduced by a specific commit,
git show <commit-hash>
To see a list of remote branches,
git branch -r
To see the changes between two commits,
git diff <commit1 hash> <commit2 hash>
To see a reference log that shows the history of branch references,
git reflog
git revert
command creates a new commit that undoes the changes introduced by a previous commit. This is a safer option, especially if the commit has been pushed to a shared repository because it does not alter the existing history.
git revert <commit-hash>
git reset
command is more powerful but should be used with caution, especially if the commit has already been pushed to a shared repository. It allows you to move the branch pointer to a specific commit, effectively discarding all commits after that point.
git reset --hard <commit-hash>
To move the branch pointer to a specific commit and add to staging the changes made in previous commit, you can use
git reset --soft <commit-hash>
To create a new branch, use the git branch
git branch <branch-name>
To switch to the newly created branch,
git checkout <branch-name>
git switch <branch-name>
A more concise way to create and switch to a new branch
git checkout -b <branch-name>
To see a list of existing branches and identify the current branch,
git branch
After making changes in a branch, you can merge those changes back into the main branch. First switch back to the branch you want to merge into then,
git merge <branch-name>
To delete a branch,
git branch -d <branch-name>
git branch --delete <branch-name>
To rename the current branch,
git branch -m <new-branch-name>
Ensure remote repository is properly configured,
git remote -v
It's a good practice to pull changes from the remote repository before pushing to avoid conflicts,
git pull <remote-name> <branch-name>
Use the git push
command to push your local changes to the remote repository,
git push <remote-name> <branch-name>
Using flag -u
allows you to set up tracking, which means that your local branch is associated with a remote branch.It's often used the first time you push a branch to a remote repository, after the first time you can simply use command git push
.
git push -u <remote-name> <branch-name>
In some cases, you might need to force-push changes using flag -f
if the remote branch has diverged from your local branch. However, use force-push cautiously, especially on shared branches, as it rewrites the history.
git push -f <remote-name> <branch-name>
To stash your changes,
git stash save "stash message"
If you want to include untracked files in the stash,
git stash -u
To view the list of stashes,
git stash list
To apply the most recent stash and remove it from the stash list,
git stash apply
If you have multiple stashes and want to apply a specific one, you can specify the stash by its ID,
git stash apply stash@{}
To apply the most recent stash and remove it from the stash list in a single command,
git stash pop
If you want to create a temporary branch from a stash,
git stash branch <branch-name>
To remove a stash without applying it,
git stash drop stash@{2}
To remove all stashes,
git stash clear