-
Fork the repository
-
Clone the forked repository to your local machine
git clone <forked-repository-url>
-
Add upstream remote if not added
# check if upstream remote is available git remote -v # if not available, add upstream remote git remote add upstream <original-repository-url>
-
Create a new branch for every new feature or bug fix or hotfix
# branch name should be descriptive # if feature: feature/your-feature-name # if bugfix: bugfix/your-bugfix-name # if hotfix: hotfix/your-hotfix-name git checkout -b <branch-name> # create and switch to new branch
-
Do your changes
-
Commit your small changes to your branch
# check your changes git add . #dot means current directory # commit your changes git commit -m "your changes short description"
-
Push your branch to remote origin
git push origin <branch-name>
-
Once you are done with your feature, create a pull request to upstream
main
branch# create pull request from your branch to upstream main branch # if you are using Github/Gitlab/Bitbucket, you can create pull request from Github/Gitlab/Bitbucket website
- Implement new features, create a new branch with name of that feature, select/add all files relate that feature.
- Create and issue on github by assigning own-self and labels
git commit -m ""
andgit push origin branch_name
- Create a pull request
- Don't commit directly to
main
branch. That's totally forbidden and risky. You have to create a new branch for every new feature. - Don't use your name as branch name. Use feature name instead. For example, if you are working on a feature called
login
then your branch name should befeature/login
. If you are working on a bug fix then your branch name should bebugfix/login
. If you are working on a hotfix then your branch name should behotfix/login
. - Don't use
git push
command. Use full commandgit push origin <branch-name>
instead to avoid data loss and confusion. - Don't use
git pull
command. Use full commandgit pull origin <branch-name>
instead to avoid data loss and confusion. - Don't merge branch without review. Ask your team member to review your code and merge it.
- Before pull request, make sure your branch is up to date with
main
branch. If not, then pullmain
branch to your branch and resolve conflicts if any. - while switching branch, make sure your branch is clean. If not, then
commit
your changes orstash
them. - Note: Always Keep origin
main
branch up to date with upstreammain
branch
In a simple words, You can think like it's a browser to browse Codes line by line, project by project. At the same time Github
is Google Drive
for your coding projects. As you browse web pages in a browser, you can store modify and share codes in Github by using Git.
In a more technical words, Git
is Version Control System (VCS). Git is CLI
(Command Line Interface) tools to manage projects simply and efficiently. Github
is a remote. Remote
is like service provider for your project. You can use Github
as a remote for your project. There are many other remotes like Gitlab
, Bitbucket
etc.
- Repository / repo - Repository is like a folder for your project. It contains all the files and folders of your project.
- Add - Add is like
select
. You canselect
files to upload to store onremotes
(Github, Gitlab, Bitbucket etc.) - Commit - A commit is a snapshot of your project where you made changes. You can think like a commit is a version of your project. You can revert back to any commit.
- Staging - Staging is like a waiting area. You can select files to commit in staging area.
- Clone -
Clone
is likedownload
. You can download a project fromremote
to your local machine. - Push -
Push
is likeupload
. You can upload changed files toremote
. - Fetch -
Fetch
is likeRefresh
. You can see the changes of your project onremote
beforedownload
orpull
the changes. - Pull -
Pull
is likedownload
. You can download your project fromremote
. - Merge -
Merge
is likecombine
. You can combine two branches into onebranch
. - Stash -
Stash
is likehide
. You can hide your changes to work on other things. You can unstash to get back your changes. - Remote -
Remote
is likeservice provider
. You can useGithub
,Gitlab
,Bitbucket
etc. as remote for your project. A project can have multiple remotes. - Fork -
Fork
is likecopy
. You can copy a project fromremote
to yourremote
. When you fork a project, your version of project is calledorigin
and the project you forked from is calledupstream
. - Hotfix -
Hotfix
is likeemergency fix
. You can usehotfix
to fix bugs in production.
Download and install the latest version of Git for your platform.
You have you config your username
and email
to use Git. You can use git config
command to configure your username
and email
.
git config --global user.name "<your name>"
git config --global user.email "<your email>"
Choose this git strategy for your project:
git config --global pull.rebase false
git config --global pull.ff only
Configure merge
strategy for your project:
git config --global merge.ff true
You don't any commit directly to main
branch. That's totally forbidden and risky. You have to create a new branch for every new feature. Once you are done with your feature, you can merge your branch to main
branch. You can delete your branch after merging.
- view all branches
git branch -a
- Create a new branch
git checkout -b <branch-name>
- Switch to a branch
git checkout <branch-name>
- Delete a branch
git branch -d <branch-name> # delete local branch
- Push a branch to remote
git push origin <branch-name>
git init
- Create a new git repogit status
- View the changes to your project code example:git status -s
(short status)git add
- Add files to staging area (in nomal words,select
files to commit) example:git add .
(.
means all files of current directory)git commit
- Creates a new commit with selected files from staging area example:git commit -m "your changes short description"
git log
- View recent commits example:git log
(show commits, pressq
to exit)git push
- Push to remote repo (By default,origin
is the remote repo name) example:git push origin <branch-name>
git pull
- Pull latest from remote repo example:git pull origin <branch-name>
(pull from remote branch to local branch)
git remote -v
- View remote repogit remote add <remote-name> <remote-url>
- Add a new remote repo example:git remote add upstream <this-org-project-url>
git remote set-url <remote-name> <remote-url>
- Change remote repo url example:git remote set-url upstream <main url>
git branch
- View all branchesgit branch -a
- View all branches (including remote branches)git branch <branch-name>
- Create a new branch (without switching)git checkout -b <branch-name>
- create a new branch and switch to itgit checkout <branch-name>
- Switch to a branchgit branch -d <branch-name>
- Delete a branch (local) (use-D
to force delete [don't
])
git stash
- Stash your changesgit stash list
- View all stashesgit stash apply
- Apply your last stash without removing itgit stash apply stash@{<stash-number>}
- Apply your selected stashgit stash drop
- Drop your last stashgit stash drop stash@{<stash-number>}
- Drop your selected stashgit stash pop
- Apply your last stash and remove itgit stash pop stash@{<stash-number>}
- Apply your selected stash and remove it
git reset <file-name>
- unchange a file from staging areagit reset --soft HEAD^
- uncommit your last commit [when you mistakenly left some files uncommitted or spelling mistake in commit message]
git reset --hard
- Reset all changes (including staged and unstaged) [Avoid this command]git reset --hard <commit-hash>
- Reset to a commit [Avoid this command]
git rebase <branch-name>
- Rebase a branch to current branchgit rebase --abort
- Abort a rebase
git merge <branch-name>
- Merge a branch to current branchgit merge --abort
- Abort a merge