/git-commands-workflows

🚀 All the git commands and workflows you need to know

Primary LanguageShellMIT LicenseMIT

git-commands-workflows

The git commands & workflows you need to know to work with git and automate your regular commands.


Initialization

# paste this in your terminal to change your current working directory to the project directory
cd your_project_path

# initialize git
git init

Commands

⚡️ The repetitive commands that I (and everyone else) use regularly.

# connect the remote GitHub repo with your local project
git remote add origin [github-repo-url]

# see untracked files
git status

# add all untracked files to the staging area
git add .

# commit the tracked files of the staging area
git commit -m "commit-msg"

# push all the changes to the GitHub
git push -u origin master

🏗 Git Setup if you have never used git before.

# configure git with your github username
git config --global user.name "your_github_username"

# configure git with your github email (email you used to sign up on GitHub)
git config --global user.email "your_email@whatever.com"

🎩 Clone a repository in your computer.

# clone a repo
git clone [repo_url]

🌲 The git commands you need to know to work with branches.

# list all branches
git branch

# create a new branch
git branch [branch_name]

# checkout to the new branch
git checkout [branch_name]

# 	OR

# create AND checkout to the new branch
git checkout -b [branch_name]

# pushing the new branch on GitHub
git push origin [branch_name]

# delete a branch locally
git branch -d [branch_name]

# delete a branch on GitHub
git push origin -d [branch_name]

# pulling changes from some other branch
git pull origin [branch_name]

# merge a branch with the current active branch
git merge [branch_name]

# merge a branch to some defined branch
git merge [source_branch] [target_branch]

📚 Stashing untracked changes — It saves all the new untracked changes and rewind your repo to the last commit.

# stash the untracked changes
git stash

# pop an existing stack
git stash apply stash@{stash_number}

# list all stashes
git stash list

# delete all saved stashes
git stash clear

🍒 Pulling all the new changes from the remote repository on GitHub

# pull changes from master branch
git pull origin master

# pulling changes from some other branch
git pull origin [branch_name]

🎯 Keep your GitHub forked repo in sync with the original repository.

# STEP #1: show URLs of remote repositories when listing your current remote connections
git remote -v

# STEP #2: add upstream
git remote add upstream [source-repo-url]

# STEP #3: fetching all the new changes from the main repository
git fetch upstream

# STEP #4: merging the new changes from the original repo to your forked local repo
git merge upstream/master

# STEP #5: pushing the new changes of the forked local repo to the GitHub
git push origin master

Note: Replace master with main if your primary branch is main.


Workflows

Open your .zshrc or .bashrc file. It is located in your Home directory. Paste the following shellcode there.

# Keep your GitHub forked repo in sync with the original repository with master as the primary branch
function fetchremotems() {
	git fetch upstream &&
	git merge upstream/master &&
	git push origin master
}

# Keep your GitHub forked repo in sync with the original repository with main as the primary branch
function fetchremotemn() {
	git fetch upstream &&
	git merge upstream/main &&
	git push origin main
}

# create new branch and checkout to it
function gcb() {
	git checkout -b "${1}"
}

# checkout to a branch
function gch() {
	git checkout "${1}"
}

# push changes to another branch
function gbp() {
	git push origin "${1}"
}

# add, commit, push changes to github
function gacp() {
	git add . &&
	git commit -m "${1}" &&
	git push
}

# aliases
alias gi='git init'
alias gs='git status'
alias ga='git add '
alias gaa='git add .'
alias gc='git commit -m '
alias gp='git push'
alias gra='git remote add origin '
alias gpm='git push -u origin master'

# create YOUR own git workflows
function [functionName]() {
    # commands to execute when function is called
    # if there are more than one commands, use && between them
    # to use the first output from the terminal, use "${1}"
}

🚀 Usage

Fetching changes from the original repo to your forked repo.

cd your_project_path

# do this only once in every forked local repo to add upstream
git remote add upstream [source-repo-url]

# write the following in the terminal – primary branch: master – whenever you need to fetch the changes
fetchremotems

# write the following in the terminal – primary branch: main – whenever you need to fetch the changes
fetchremotemn

Usage of the rest of the workflows.

# To create a new branch and also to checkout to it
gcb [branch_name]

# To checkout to an existing branch
gch [branch_name]

# To push changes to another branch
gbp [branch_name]

# To add, commit and push changes to the github
gacp "commit-msg"

# initialize git
gi

# check status
gs

# stage untracked file
ga [file_name]

# stage all untracked files
gaa

# commit the changes
gc "commit-msg"

# connect remote repo to the local repo
gra [repo-link]

# push changes to master
gpm

👨🏻‍💻 Contributing

Feel free to add your git workflows in the repository. Just make sure you first read the contributing guidelines before making a PR.

⚡️ Other Projects

I have curated a detailed list of all the open-source projects I have authored. Do take out a moment and take a look.

🔑 License & Conduct