You are on page 1of 8

Detailed list of **all** GitHub commands

### Basic Git Commands

#### `git init`


- Initializes a new Git repository locally. This command creates a new `.git` directory in your
project, turning the current directory into a Git repository.

#### `git clone [URL]`


- Creates a local copy of a remote repository. Replace `[URL]` with the repository URL from
GitHub.

#### `git add [file]`


- Stages changes for commit. Replace `[file]` with the filename you wish to add. Using `git add .`
stages all changed files.

#### `git commit -m "[commit message]"`


- Records staged changes in the repository with a descriptive message. Replace `[commit message]`
with a brief note about the changes.

#### `git status`


- Displays the state of the working directory and staging area, showing which changes have been
staged, which haven’t, and which files aren’t being tracked by Git.

#### `git push [remote] [branch]`


- Updates the remote repository with the changes committed locally in the specified branch.
`[remote]` is typically `origin`, and `[branch]` is the branch you're pushing to.

#### `git pull [remote] [branch]`


- Fetches changes from the remote repository and merges them into the local branch. This command
combines `git fetch` and `git merge`.

#### `git branch`


- Lists all local branches in the repository. With `-a`, it shows both local and remote branches, and
with `-r`, it shows only remote branches.

#### `git checkout [branch]`


- Switches to the specified branch and updates the working directory. The `-b` option creates a new
branch and then checks it out.

#### `git merge [branch]`


- Merges the specified branch’s history into the current branch. This command is used to combine
changes from one branch into another.

#### `git fetch [remote]`


- Downloads objects and refs from another repository. This command fetches branches and tags
from the remote repository, updating your local references.

#### `git rebase [branch]`


- Reapplies commits on top of another base tip. This command is used to move or combine a
sequence of commits to a new base commit.

### Collaboration and Review Commands

#### `git pull request`


- While not a native Git command, creating pull requests is a common action on GitHub, typically
done via the GitHub web interface or the GitHub CLI (`gh`) to propose changes for review.

#### `git stash`


- Temporarily shelves (or stashes) changes so you can work on a different branch without
committing incomplete work.

### Advanced Commands

#### `git cherry-pick [commit]`


- Applies the changes introduced by some existing commits.
#### `git rebase -i`
- Interactive rebase allows you to edit commits, reorder, or squash them easily.

#### `git tag`


- Used to mark specific points in history as important, typically for marking version releases.

### GitHub CLI (gh)

The GitHub CLI (`gh`) extends Git functionality specifically for GitHub, allowing you to create
pull requests, view issues, and more, right from the command line. It's a separate tool from Git but
is incredibly useful for GitHub-specific workflows.

- **Installation and documentation:** For more details on the GitHub CLI, visit the [GitHub CLI
documentation](https://cli.github.com/manual/).

This list covers the basics and some more advanced commands but is far from exhaustive. Each
command has a variety of options and use cases, so consulting the Git documentation (`git help
[command]`) or the Pro Git book is advisable for a deeper understanding.

If error:
PS C:\Users\HP\Desktop\devops> git push origin Dev
To https://github.com/liaquatali000/devops.git
! [rejected] Dev -> Dev (fetch first)
error: failed to push some refs to 'https://github.com/liaquatali000/devops.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
PS C:\Users\HP\Desktop\devops>

Solution:
Step 1: Pull the Latest Changes
Make sure you're on the Dev branch:
git checkout Dev
Then, pull the latest changes from the remote repository:
git pull origin Dev
Step 2: Resolve Any Merge Conflicts
git add .
git commit -m "Resolved merge conflicts"
Step 3: Push Your Changes
git push origin Dev
Next will be next js, react js, angular

Docker:

To check the running Docker containers on your system:


docker ps

Optional - List all containers (including stopped ones): If you also want to see containers that have
been stopped, you can run:
docker ps –a
Optional - Filtering the output: If you have many containers and you want to filter the list, you can
use various options with docker ps. For example, to find containers based on their name, you can
use:
docker ps --filter "name=your_container_name"

Viewing logs of a running container:


docker logs container_name_or_id

To start, stop, or restart a container, use the respective commands followed by the container ID or
name:
Start: docker start <container_id_or_name>
Stop: docker stop <container_id_or_name>
Restart: docker restart <container_id_or_name>

Build the Docker image:

Open a terminal in your project directory (~/myreactapp).


Run the docker build command with a tag for your image. Tags typically follow the format
username/repository:tag. If you don't have a Docker Hub account or a private registry, you can just
use a simple name and version for the tag.
Example command:
bash
Copy code
docker build -t myreactapp:latest .
This command builds a Docker image with the tag myreactapp:latest using the Dockerfile in the
current directory (. signifies the current directory).
Verify the image was created:

After the build process completes, you can verify that your image was created successfully by
listing all Docker images:
bash
Copy code
docker images

if you want to map port 5000 of the container to port 5000 of your host machine, you should run:

bash
Copy code
docker run -p 5000:5000 myreactapp:latest
If you want to use a different port on the host, say 8080, you would run:

bash
Copy code
docker run -p 8080:5000 myreactapp:latest
This means "map port 5000 inside the container to port 8080 on the host." After running this
command, you can access your application by going to http://localhost:8080 in a web browser,
assuming that the Docker host is your local machine. If you're working with a remote server like an
Ubuntu server in Azure, you'd use the server's public IP or domain name instead of localhost.

To stop a container, you need the correct container ID or name. Here's how you can find the running
containers and stop them correctly:

List running containers:

bash
Copy code
sudo docker ps
This will show you all running containers. Note the NAMES and CONTAINER ID columns.
Stop a container using its ID or name:

If you have the container ID, use it directly:


bash
Copy code
sudo docker stop container_id
If you have the container name, use it:
bash
Copy code
sudo docker stop container_name
Replace container_id with the actual ID of the container, and container_name with the actual name.

In your case, since you successfully stopped the container with ID 24b446c59bb1, you don't need to
take any further action for that container. If there are other containers you wish to stop, use the
correct ID or name as explained above. Remember, container names are not automatically the same
as image names unless you explicitly set them to be so when you run the container with docker run
--name my_container_name ....

sudo docker run -d -p 5000:5000 3611f430afb5

If you started this container without the -d flag (which stands for "detached"), your terminal is now
tied to the stdout of the Docker container. To detach from a running container without stopping it,
you can usually use the Ctrl + p, Ctrl + q key sequence. However, this only works if the container
was started with the -it option (interactive terminal).

If you've started the container without -it and it's taking over your terminal, you cannot detach in the
traditional sense. You would need to stop the container (which would stop your application) and
restart it in detached mode using the -d flag like this:

bash
Copy code
sudo docker run -d -p 5000:5000 3611f430afb5
To stop the current container that's running and attached to your terminal, you would open a new
terminal window, find the container ID with docker ps, and then use docker stop <container_id> to
stop it. After that, you can restart it in detached mode as shown above.

You might also like