Skip to content

Instantly share code, notes, and snippets.

@moisoto
Last active May 15, 2024 18:01
Show Gist options
  • Save moisoto/3169efdbd51aaeadb6d7b696901d1595 to your computer and use it in GitHub Desktop.
Save moisoto/3169efdbd51aaeadb6d7b696901d1595 to your computer and use it in GitHub Desktop.
GIT - Workflow Simplified

Git Workflow Simplified

Create a local Repository

If you want to work on your machine and don't need a remote repository, or you want to upload your repository at a later time, you can create a local repository:

# Initialize a new repository
git init

# You should always create a REAMDE for your repo
echo "# My First Repository" > README.md

# Add the new file to the staging area
git add README.md

# Commit changes
git commit -m "Add README.md file."

# Check your initial branch name
git branch

Your new repository is ready but you might want to change your initial branch name before you start making new changes or creating new branches and merging them. Let's say you want your initial branch to be named 'latest'

# Rename your current branch
git branch -m latest

Cloning a remote repository

If you know that you will store your repository online from the start, then a better way to start is creating them online and then cloning them on your local machine.

Let's say your new empty remote repository was create on github:

# Adds a remote repository with the shortname origin
git clone https://github.com/my-user/my-repo.git

Push to an empty remote repository

Sometimes you started work on a local repository and at a later time decide to create an online repository to store the files. In this case, create an empty remote repository on your favorite platform and then push your local repository onto it.

Let's say your new empty remote repository was created on github, and your local repository has a branch named main that you want to push to that remote.

Add the remote repository:

# Adds a remote repository with the shortname origin
git remote add origin https://github.com/my-user/my-repo.git

Push your branch to the remote:

# Pushes the branch main to the remote
git push --set-upstream origin main

Tidbit: The previous push command will push your local main branch to the remote main branch, even if your current branch is not actually the main branch. It's equivalent to: git push -u origin refs/heads/main:refs/heads/main


Create a New Branch and Push to Remote

Create a new branch:

git checkout -b new_branch

Push to remote:

# Check your remote shortnames
# The following command will list them
git remote

# Cloned repositories will have a remote with a shortname called origin.
# If you added the remote manually or want to push to another remote,
# use the appropiate shortname instead of origin.
# (the -u option is a shortcut to --set-upstream)
git push -u origin new_branch

Check your working history

Oneline outputs:

git log --pretty=oneline # Full Commit Hash
git log --online # Shot Commit Hash
git log --pretty=reference # Includes the Date

# This is my custom oneline format:
git log --pretty='format:%C(auto)%h %ad: %s' --date=short

Short Formats:

git log --pretty=short --graph

# My custom short format (basically add file listing to oneline version)
git log --pretty='format:%C(auto)%h %ad: %s' --date=short --name-only

Undo Local Changes

Sometimes after you commit changes, you do modifications to your local files, and decide to return to the state of the files from your last commit.

To reset the files to the state they where after your last commit:

git stash
git stash drop

Or do them both in a single command line:

git stash && git stash drop

Undo Last Commit

If changes have been already pushed, or you want to keep old commit (recommended):

git revert HEAD

If changes are still local and you want to leave no trace of previous commit:

git reset --hard HEAD~;

Check previous version of a file

The Proper Way:

  • First make a new branch where you can go back to a previous commit without putting your current branch in a detached HEAD State:
# This will create (a new branch called temp and switch to it)
git checkout -b temp
  • Check the file history for the file you want to revert to:
# Check commit history of file <your_modified_file>
git log --oneline your_modified_file
  • Take note of the hash corresponding to the commited file you want to revert to. The output of the git log command will be something like this:
d4f6dc2 Last version of the file
d1c0ffc A modified version of the file
d39c7b1 The first commit that contained the file
(END)
  • Let's say you want revert to commit d1c0ffc:
# Revert to commit d1c0ffc
git checkout d1c0ffc
  • Your file now has been restored to the version on that commit. Do whatever you were planning to do with it. For example you may want to copy the file to the clipboard, on macOS you can use the following command:
# The pbcopy command takes input from stdin and stores it in the clipboard
cat your_modified_file | pbcopy
  • After finishing with your work, you can return to your original branch and delete the temporal one:
# Go back to branch <main>
git checkout main

# Delete branch <temp>
git branch -d temp

Squashing your commits

Sometimes you make a branch to work on changes and you make a lot of commits while you work in that branch. When you merge your changes into the main branch you will have those individual commits appearing on the commit log.

This is important in most scenarios since you will be able to revert to any change in the commit history, and you will have detailed info about who did what, and when it was done. However there may be some situations where you want to squash all the changes in a single commit before merging.

As detailed in this wiki you can do the following:

Go to your main branch and create a new branch from it:

git checkout main
git checkout -b new-feature_4merge

Implement your feature in your new branch, making your commits as you normally do.

Now merge your work and squash the commits into a single one:

git merge --squash new-feature
git commit # without -m 

You will be presented with a file containing a summary of all the commit messages on branch new-feature. You can leave it like that, or make modifications, eliminate irrelevant information or add details. Then save and quit.

Finally merge your changes on the main branch:

git checkout main
git merge new-feature-4merge

Checking a Remote Repository

If you just want to check the url of a remote (let's say origin):

git ls-remote --get-url origin

If you want to list your remotes:

git remote -v

Fetch detailed information from a given remote:

git remote show origin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment